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 7f98ae3 commit e3235c3
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 44 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
"test:unit": "tap",
"test-ci": "run-s test:*",
"make:npm-package": "node ./scripts/make-npm-package.js",
"setup": "run-s --continue-on-error update:package-lock update:npm:test-package-json",
"update": "run-p --continue-on-error --aggregate-output update:*",
"setup": "run-s update:package-lock update:npm:test-package-json",
"update": "run-p --aggregate-output update:*",
"update:browserslist-db": "update-browserslist-db",
"update:empty-files": "node ./scripts/update-empty-files.js",
"update:license": "node ./scripts/update-license.js",
"update:manifest": "node ./scripts/update-manifest.js",
"update:package-json": "node ./scripts/update-package-json.js",
"update:package-lock": "node ./scripts/update-package-lock.js",
"update:npm": "run-s --continue-on-error update:npm:*",
"update:npm": "run-s update:npm:*",
"update:npm:package-json": "node ./scripts/update-npm-package-json.js",
"update:npm:test-package-json": "node ./scripts/update-npm-test-package-json.js"
},
Expand Down Expand Up @@ -72,6 +72,7 @@
"npm-run-all2": "^6.2.3",
"oxlint": "^0.9.5",
"packageurl-js": "^2.0.1",
"picomatch": "^4.0.2",
"prettier": "3.3.3",
"semver": "^7.6.3",
"tap": "^21.0.1",
Expand Down
131 changes: 100 additions & 31 deletions scripts/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const { packageExtensions: yarnPkgExts } = require('@yarnpkg/extensions')
const browserslist = require('browserslist')
const { createNewSortInstance } = require('fast-sort')
const fs = require('fs-extra')
const picomatch = require('picomatch')
const semver = require('semver')
const which = require('which')
const whichFn = require('which')
const { sync: whichSyncFn } = whichFn

const EMPTY_FILE = '/* empty */\n'
const LICENSE = 'LICENSE'
Expand Down Expand Up @@ -67,49 +69,117 @@ const relTestNpmNodeModulesPath = path.relative(

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

const { compare: localCompare } = new Intl.Collator()
const ignores = Object.freeze([
...new Set([
// Most of these ignored files can be included specifically if included in the
// files globs. Exceptions to this are:
// https://docs.npmjs.com/cli/v10/configuring-npm/package-json#files
// These can not be included.
'.git',
'.npmrc',
'**/node_modules',
'**/package-lock.json',
'**/pnpm-lock.ya?ml',
'**/yarn.lock',
...includeIgnoreFile(gitignorePath).ignores
])
])

const innerReadDirNames = (dirents, options) => {
const opts = { sort: true, ...options }
const names = dirents.filter(d => d.isDirectory()).map(d => d.name)
return opts.sort ? names.sort(localCompare) : names
}
const { compare: localCompare } = new Intl.Collator()

const naturalSort = createNewSortInstance({
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' })
.compare
})

const readDirNamesSync = (dirname, options) =>
innerReadDirNames(fs.readdirSync(dirname, { withFileTypes: true }), options)
const innerReadDirNames = function innerReadDirNames(dirents, options) {
const { sort, includeEmpty } = { sort: true, includeEmpty: false, ...options }
const names = dirents
.filter(
d =>
d.isDirectory() &&
(includeEmpty || !isDirEmptySync(path.join(d.parentPath, d.name)))
)
.map(d => d.name)
return sort ? names.sort(localCompare) : names
}

const matcherCache = new Map()

const getGlobMatcher = function getGlobMatcher(glob, options) {
const patterns = Array.isArray(glob) ? glob : [glob]
const key = JSON.stringify({ patterns, options })
let matcher = matcherCache.get(key)
if (matcher) {
return matcher
}
matcher = picomatch(patterns, {
dot: true,
nocase: true,
...options
})
matcherCache.set(key, matcher)
return matcher
}

const whichSyncOptions = {
const isDirEmptySync = function isDirEmptySync(dirname) {
try {
const files = fs.readdirSync(dirname)
const { length } = files
if (length === 0) {
return true
}
const matcher = getGlobMatcher(ignores, { cwd: dirname })
let ignoredCount = 0
for (let i = 0; i < length; i += 1) {
if (matcher(files[i])) {
ignoredCount += 1
}
}
return ignoredCount === length
} catch (e) {
return e?.code === 'ENOENT'
}
}

const readDirNamesSync = function readDirNamesSync(dirname, options) {
return innerReadDirNames(
fs.readdirSync(dirname, { withFileTypes: true }),
options
)
}

const defaultWhichOptions = {
path: `${rootNodeModulesBinPath}${path.delimiter}${process.env.PATH}`
}
const whichSync = cmd => which.sync(cmd, whichSyncOptions)

const which = function which(cmd, options) {
return whichFn(cmd, { ...defaultWhichOptions, ...options })
}

const whichSync = function whichSync(cmd, options) {
return whichSyncFn(cmd, { ...defaultWhichOptions, ...options })
}

const kInternalsSymbol = Symbol('@socketregistry.constants.internals')

const internals = {
getGlobMatcher,
innerReadDirNames,
isDirEmptySync,
localCompare,
naturalSort,
readDirNamesSync,
which,
whichSync
}

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

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

const ignores = Object.freeze([
...new Set([
// Most of these ignored files can be included specifically if included in the
// files globs. Exceptions to this are:
// https://docs.npmjs.com/cli/v10/configuring-npm/package-json#files
// These can not be included.
'.git',
'.npmrc',
'**/node_modules',
'**/package-lock.json',
'**/pnpm-lock.ya?ml',
'**/yarn.lock',
...includeIgnoreFile(gitignorePath).ignores
])
])

const lifecycleScriptNames = new Set(
[
'dependencies',
Expand Down Expand Up @@ -261,6 +331,7 @@ const tsLibs = new Set([
])

module.exports = {
[kInternalsSymbol]: internals,
EMPTY_FILE,
LICENSE,
LICENSE_CONTENT,
Expand All @@ -280,14 +351,12 @@ module.exports = {
VERSION,
ecosystems,
execPath,
ignores,
innerReadDirNames,
gitignorePath,
ignores,
kInternalsSymbol,
lifecycleScriptNames,
localCompare,
lowerToCamelCase,
maintainedNodeVersions,
naturalSort,
npmExecPath,
npmPackageNames,
npmPackagesPath,
Expand Down
2 changes: 0 additions & 2 deletions scripts/make-npm-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ function modifyContent(content, data = {}) {
execPath,
[
runScriptParallelExecPath,
'--continue-on-error',
'--aggregate-output',
'update:package-json',
`update:npm:test-package-json -- --add ${pkgName}`
],
Expand Down
17 changes: 14 additions & 3 deletions scripts/utils/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ const util = require('node:util')

const fs = require('fs-extra')

const constants = require('@socketregistry/scripts/constants')
const {
innerReadDirNames,
readDirNamesSync
} = require('@socketregistry/scripts/constants')
kInternalsSymbol,
[kInternalsSymbol]: { innerReadDirNames, readDirNamesSync, isDirEmptySync }
} = constants
const {
normalizePackageJson,
toEditablePackageJson,
Expand All @@ -32,6 +33,14 @@ async function cp(srcPath, destPath, options) {
})
}

async function isDirEmpty(dirname) {
try {
return (await fs.readdir(dirname)).length === 0
} catch (e) {
return e?.code === 'ENOENT'
}
}

function isSymbolicLinkSync(filepath) {
try {
return fs.lstatSync(filepath).isSymbolicLink()
Expand Down Expand Up @@ -80,6 +89,8 @@ function uniqueSync(filepath) {

module.exports = {
cp,
isDirEmpty,
isDirEmptySync,
isSymbolicLinkSync,
move,
readDirNames,
Expand Down
12 changes: 10 additions & 2 deletions scripts/utils/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

const path = require('node:path')

const { PACKAGE_JSON } = require('@socketregistry/scripts/constants')
const constants = require('@socketregistry/scripts/constants')
const {
PACKAGE_JSON,
kInternalsSymbol,
[kInternalsSymbol]: { getGlobMatcher, which, whichSync }
} = constants

const slashRegExp = /[/\\]/
const leadingDotSlashRegExp = /^\.\.?[/\\]/
Expand Down Expand Up @@ -40,11 +45,14 @@ function trimTrailingSlash(filepath) {
}

module.exports = {
getGlobMatcher,
isNodeModules,
isRelative,
resolvePackageJsonDirname,
resolvePackageJsonPath,
splitPath,
trimLeadingDotSlash,
trimTrailingSlash
trimTrailingSlash,
which,
whichSync
}
7 changes: 4 additions & 3 deletions scripts/utils/sorts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const constants = require('@socketregistry/scripts/constants')
const {
localCompare,
naturalSort
} = require('@socketregistry/scripts/constants')
kInternalsSymbol,
[kInternalsSymbol]: { localCompare, naturalSort }
} = constants

module.exports = {
localCompare,
Expand Down

0 comments on commit e3235c3

Please sign in to comment.