Skip to content

Commit

Permalink
Make creation wizard use faster path for updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Sep 20, 2024
1 parent e7adeb5 commit 7f98ae3
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 74 deletions.
4 changes: 1 addition & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ const {
npmPackagesPath,
npmPackageNames,
prettierignorePath,
rootPath
relNpmPackagesPath
} = require('@socketregistry/scripts/constants')

const relNpmPackagesPath = path.relative(rootPath, npmPackagesPath)

const {
engines: { node: nodeRange }
} = require('./package.json')
Expand Down
47 changes: 28 additions & 19 deletions scripts/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ const gitignorePath = path.resolve(rootPath, '.gitignore')
const prettierignorePath = path.resolve(rootPath, '.prettierignore')
const templatesPath = path.join(__dirname, 'templates')

const npmPackagesPath = path.join(rootPackagesPath, 'npm')
const npmTemplatesPath = path.join(templatesPath, 'npm')

const testNpmPath = path.join(rootPath, 'test/npm')
const testNpmPkgJsonPath = path.join(testNpmPath, PACKAGE_JSON)
const testNpmPkgLockPath = path.join(testNpmPath, PACKAGE_LOCK)
const testNpmNodeModulesPath = path.join(testNpmPath, NODE_MODULES)
const testNpmNodeModulesHiddenLockPath = path.join(
testNpmNodeModulesPath,
PACKAGE_HIDDEN_LOCK
)
const testNpmNodeWorkspacesPath = path.join(testNpmPath, NODE_WORKSPACES)

const yarnPkgExtsPath = path.join(rootNodeModulesPath, '@yarnpkg/extensions')
const yarnPkgExtsJsonPath = path.join(yarnPkgExtsPath, PACKAGE_JSON)

const relPackagesPath = path.relative(rootPath, rootPackagesPath)
const relNpmPackagesPath = path.relative(rootPath, npmPackagesPath)
const relTestNpmPath = path.relative(rootPath, testNpmPath)
const relTestNpmNodeModulesPath = path.relative(
rootPath,
testNpmNodeModulesPath
)

const LICENSE_CONTENT = fs.readFileSync(rootLicensePath, 'utf8')

const { compare: localCompare } = new Intl.Collator()
Expand All @@ -65,27 +89,9 @@ const whichSyncOptions = {
const whichSync = cmd => which.sync(cmd, whichSyncOptions)

const npmExecPath = whichSync('npm')
const npmPackagesPath = path.join(rootPackagesPath, 'npm')
const npmTemplatesPath = path.join(templatesPath, 'npm')

const runScriptParallelExecPath = whichSync('run-p')
const runScriptSequentiallyExecPath = whichSync('run-s')

const testNpmPath = path.join(rootPath, 'test/npm')
const testNpmPkgJsonPath = path.join(testNpmPath, PACKAGE_JSON)
const testNpmPkgLockPath = path.join(testNpmPath, PACKAGE_LOCK)
const testNpmNodeModulesPath = path.join(testNpmPath, NODE_MODULES)
const testNpmNodeModulesHiddenLockPath = path.join(
testNpmNodeModulesPath,
PACKAGE_HIDDEN_LOCK
)
const testNpmNodeWorkspacesPath = path.join(testNpmPath, NODE_WORKSPACES)

const workspacePath = path.join(testNpmPath, NODE_WORKSPACES)

const yarnPkgExtsPath = path.join(rootNodeModulesPath, '@yarnpkg/extensions')
const yarnPkgExtsJsonPath = path.join(yarnPkgExtsPath, PACKAGE_JSON)

const ecosystems = Object.freeze(readDirNamesSync(rootPackagesPath))

const ignores = Object.freeze([
Expand Down Expand Up @@ -288,6 +294,10 @@ module.exports = {
npmTemplatesPath,
packageExtensions,
prettierignorePath,
relPackagesPath,
relNpmPackagesPath,
relTestNpmPath,
relTestNpmNodeModulesPath,
rootPath,
rootLicensePath,
rootManifestJsonPath,
Expand All @@ -305,7 +315,6 @@ module.exports = {
testNpmNodeModulesHiddenLockPath,
testNpmNodeWorkspacesPath,
tsLibs,
workspacePath,
yarnPkgExtsPath,
yarnPkgExtsJsonPath
}
52 changes: 24 additions & 28 deletions scripts/make-npm-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ const spawn = require('@npmcli/promise-spawn')
const { default: didYouMean, ReturnTypeEnums } = require('didyoumean2')
const fs = require('fs-extra')
const { glob: tinyGlob } = require('tinyglobby')
const validateNpmPackageName = require('validate-npm-package-name')

const {
LICENSE_CONTENT,
LICENSE_GLOB_PATTERN,
PACKAGE_JSON,
execPath,
npmExecPath,
npmPackagesPath,
npmTemplatesPath,
rootPath,
runScriptParallelExecPath,
tsLibs
} = require('@socketregistry/scripts/constants')
const {
existsInNpmRegistry
} = require('@socketregistry/scripts/utils/packages')
const { naturalSort } = require('@socketregistry/scripts/utils/sorts')

const templates = Object.fromEntries(
Expand All @@ -36,14 +37,6 @@ const templates = Object.fromEntries(
].map(k => [k, path.join(npmTemplatesPath, k)])
)

async function isInNpmRegistry(pkgName) {
try {
await spawn(npmExecPath, ['view', pkgName, 'name'], { cwd: rootPath })
return true
} catch {}
return false
}

function modifyContent(content, data = {}) {
let modified = content
for (const { 0: key, 1: value } of Object.entries(data)) {
Expand All @@ -55,11 +48,11 @@ function modifyContent(content, data = {}) {

;(async () => {
const pkgName = await input({
message: 'The name of the package override to create',
validate: value => validateNpmPackageName(value).validForNewPackages
message: 'What is the name of the package to override?',
validate: existsInNpmRegistry
})
const templateChoice = await select({
message: 'Choose the package template',
message: 'Pick a package template to use',
choices: [
{ name: 'default', value: 'default' },
{
Expand Down Expand Up @@ -163,19 +156,22 @@ function modifyContent(content, data = {}) {
)
)
)
await spawn(
execPath,
[
runScriptParallelExecPath,
'update:package-json',
...((await isInNpmRegistry(pkgName))
? ['update:npm:test-package-json -- --force']
: [])
],
{
cwd: rootPath,
stdio: 'inherit'
}
)
console.log('All done 🎉')
try {
await spawn(
execPath,
[
runScriptParallelExecPath,
'--continue-on-error',
'--aggregate-output',
'update:package-json',
`update:npm:test-package-json -- --add ${pkgName}`
],
{
cwd: rootPath,
stdio: 'inherit'
}
)
} catch (e) {
console.log('✘ Package override finalization encountered an error:', e)
}
})()
66 changes: 43 additions & 23 deletions scripts/update-npm-test-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ const {
npmExecPath,
npmPackageNames,
npmPackagesPath,
rootPath,
relNpmPackagesPath,
relTestNpmPath,
relTestNpmNodeModulesPath,
testNpmNodeModulesHiddenLockPath,
testNpmNodeModulesPath,
testNpmNodeWorkspacesPath,
testNpmPath,
testNpmPkgJsonPath,
testNpmPkgLockPath
} = require('@socketregistry/scripts/constants')
const { arrayChunk } = require('@socketregistry/scripts/utils/arrays')
const {
arrayChunk,
arrayUnique
} = require('@socketregistry/scripts/utils/arrays')
const {
isSymbolicLinkSync,
move,
Expand All @@ -35,6 +40,7 @@ const {
const { parsePackageSpec } = require('@socketregistry/scripts/utils/packages')
const { splitPath } = require('@socketregistry/scripts/utils/path')
const { pEach, pEachChunk } = require('@socketregistry/scripts/utils/promises')
const { localCompare } = require('@socketregistry/scripts/utils/sorts')
const { Spinner } = require('@socketregistry/scripts/utils/spinner')
const { isNonEmptyString } = require('@socketregistry/scripts/utils/strings')

Expand Down Expand Up @@ -105,6 +111,8 @@ const readCachedEditablePackageJson = async filepath_ => {
return result
}

const toWorkspaceEntry = pkgName => `${NODE_WORKSPACES}/${pkgName}`

const testScripts = [
// Order is significant. First in, first tried.
'mocha',
Expand All @@ -128,41 +136,41 @@ const testScripts = [
}

// Chunk package names to process them in parallel 3 at a time.
const allPackageNameChunks = arrayChunk(npmPackageNames, 3)
const npmPackageNameChunks = arrayChunk(npmPackageNames, 3)
const packageNames = cliArgs.add ?? npmPackageNames
const packageNameChunks = cliArgs.add
? arrayChunk([...npmPackageNames, ...cliArgs.add], 3)
: allPackageNameChunks
? arrayChunk(cliArgs.add, 3)
: npmPackageNameChunks

const relTestNpmPath = path.relative(rootPath, testNpmPath)
const relTestNpmNodeModulesPath = path.relative(
rootPath,
testNpmNodeModulesPath
)
let modifiedTestNpmPkgJson = false
let testNpmEditablePkgJson = await readPackageJson(testNpmPkgJsonPath, {
editable: true
})

// Initialize test/npm/node_modules
// Refresh/initialize test/npm/node_modules
{
const spinner = new Spinner(
`Initializing ${relTestNpmNodeModulesPath}...`
`${nmExists ? 'Refreshing' : 'Initializing'} ${relTestNpmNodeModulesPath}...`
).start()
if (nmExists) {
// Remove existing packages to re-install.
const pkgNames = cliArgs.add ?? npmPackageNames
// Remove existing packages to re-install later.
await Promise.all(
pkgNames.map(n => fs.remove(path.join(testNpmNodeModulesPath, n)))
packageNames.map(n => fs.remove(path.join(testNpmNodeModulesPath, n)))
)
}
try {
await installTestNpmNodeModules()
testNpmEditablePkgJson = await readPackageJson(testNpmPkgJsonPath, {
editable: true
})
spinner.stop(`✔ Initialized ${relTestNpmNodeModulesPath}`)
spinner.stop(
`✔ ${nmExists ? 'Refreshed' : 'Initialized'} ${relTestNpmNodeModulesPath}`
)
} catch (e) {
spinner.stop('✘ Initialization encountered an error:', e)
spinner.stop(
`✘ ${nmExists ? 'Refresh' : 'Initialization'} encountered an error:`,
e
)
}
}

Expand All @@ -177,19 +185,21 @@ const testScripts = [
const nmPkgPathExists = fs.existsSync(nmPkgPath)
// Missing packages can occur if the script is stopped part way through
if (!devDepExists || !nmPkgPathExists) {
// A package we expect to be there is missing or corrupt. Reinstall it.
// A package we expect to be there is missing or corrupt. Install it.
if (nmPkgPathExists) {
await fs.remove(nmPkgPath)
}
const spinner = new Spinner(`Reinstalling ${pkgName}...`).start()
const spinner = new Spinner(
`${devDepExists ? 'Refreshing' : 'Adding'} ${pkgName}...`
).start()
try {
await installTestNpmNodeModules(pkgName)
testNpmEditablePkgJson = await readPackageJson(testNpmPkgJsonPath, {
editable: true
})
spinner.stop(
devDepExists
? `✔ Reinstalled ${pkgName}`
? `✔ Refreshed ${pkgName}`
: `✔ --save-dev ${pkgName} to package.json`
)
} catch {
Expand Down Expand Up @@ -324,6 +334,10 @@ const testScripts = [
const spinner = new Spinner(`Linking packages...`).start()
await pEachChunk(packageNameChunks, async pkgName => {
const pkgPath = path.join(npmPackagesPath, pkgName)
if (!fs.existsSync(pkgPath)) {
console.log(`⚠️ ${pkgName}: Missing from ${relNpmPackagesPath}`)
return
}
const nmPkgPath = path.join(testNpmNodeModulesPath, pkgName)
const nmPkgJsonPath = path.join(nmPkgPath, PACKAGE_JSON)
const nmEditablePkgJson =
Expand Down Expand Up @@ -429,6 +443,7 @@ const testScripts = [
// We can go from CJS by creating an ESM stub.
const uniquePath = uniqueSync(`${destPath.slice(0, -3)}.cjs`)
await fs.copyFile(targetPath, uniquePath)
await fs.remove(destPath)
await fs.outputFile(
destPath,
createStubEsModule(uniquePath),
Expand Down Expand Up @@ -506,9 +521,14 @@ const testScripts = [
`Installing ${relTestNpmPath} workspaces... (☕ break)`
).start()
// Update "workspaces" field in test/npm/package.json.
testNpmEditablePkgJson.update({
workspaces: npmPackageNames.map(n => `${NODE_WORKSPACES}/${n}`)
})
const existingWorkspaces = testNpmEditablePkgJson.content.workspaces
const workspaces = cliArgs.add
? arrayUnique([
...(Array.isArray(existingWorkspaces) ? existingWorkspaces : []),
...cliArgs.add.map(toWorkspaceEntry)
]).sort(localCompare)
: npmPackageNames.map(toWorkspaceEntry)
testNpmEditablePkgJson.update({ workspaces })
await testNpmEditablePkgJson.save()
// Finally install workspaces.
try {
Expand Down
1 change: 1 addition & 0 deletions scripts/update-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
const workspaces = []
for (const eco of ecosystems) {
const ecoPackagesPath = path.join(rootPackagesPath, eco)
// No need to sort because readDirNames returns names sorted by default.
const packageNames = await readDirNames(ecoPackagesPath)
for (const pkgName of packageNames) {
workspaces.push(`packages/${eco}/${pkgName}`)
Expand Down
7 changes: 6 additions & 1 deletion scripts/utils/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ function arrayChunk(array, size = 2) {
return chunks
}

function arrayUnique(array) {
return [...new Set(array)]
}

module.exports = {
arrayChunk
arrayChunk,
arrayUnique
}

0 comments on commit 7f98ae3

Please sign in to comment.