Skip to content

Commit

Permalink
deps: @npmcli/map-workspaces@3.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Apr 10, 2024
1 parent 49fb9b7 commit 699a1de
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 69 deletions.
143 changes: 83 additions & 60 deletions node_modules/@npmcli/map-workspaces/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,49 @@ const { minimatch } = require('minimatch')
const rpj = require('read-package-json-fast')
const { glob } = require('glob')

function appendNegatedPatterns (patterns) {
const results = []
for (let pattern of patterns) {
function appendNegatedPatterns (allPatterns) {
const patterns = []
const negatedPatterns = []
for (let pattern of allPatterns) {
const excl = pattern.match(/^!+/)
if (excl) {
pattern = pattern.slice(excl[0].length)
}

// strip off any / from the start of the pattern. /foo => foo
pattern = pattern.replace(/^\/+/, '')
// strip off any / or ./ from the start of the pattern. /foo => foo
pattern = pattern.replace(/^\.?\/+/, '')

// an odd number of ! means a negated pattern. !!foo ==> foo
const negate = excl && excl[0].length % 2 === 1
results.push({ pattern, negate })
if (negate) {
negatedPatterns.push(pattern)
} else {
// remove negated patterns that appeared before this pattern to avoid
// ignoring paths that were matched afterwards
// e.g: ['packages/**', '!packages/b/**', 'packages/b/a']
// in the above list, the last pattern overrides the negated pattern
// right before it. In effect, the above list would become:
// ['packages/**', 'packages/b/a']
// The order matters here which is why we must do it inside the loop
// as opposed to doing it all together at the end.
for (let i = 0; i < negatedPatterns.length; ++i) {
const negatedPattern = negatedPatterns[i]
if (minimatch(pattern, negatedPattern)) {
negatedPatterns.splice(i, 1)
}
}
patterns.push(pattern)
}
}

return results
// use the negated patterns to eagerly remove all the patterns that
// can be removed to avoid unnecessary crawling
for (const negated of negatedPatterns) {
for (const pattern of minimatch.match(patterns, negated)) {
patterns.splice(patterns.indexOf(pattern), 1)
}
}
return { patterns, negatedPatterns }
}

function getPatterns (workspaces) {
Expand Down Expand Up @@ -77,64 +103,66 @@ async function mapWorkspaces (opts = {}) {
}

const { workspaces = [] } = opts.pkg
const patterns = getPatterns(workspaces)
const { patterns, negatedPatterns } = getPatterns(workspaces)
const results = new Map()
const seen = new Map()

if (!patterns.length) {
if (!patterns.length && !negatedPatterns.length) {
return results
}

const getGlobOpts = () => ({
...opts,
ignore: [
...opts.ignore || [],
...['**/node_modules/**'],
'**/node_modules/**',
// just ignore the negated patterns to avoid unnecessary crawling
...negatedPatterns,
],
})

const getPackagePathname = pkgPathmame(opts)

for (const item of patterns) {
let matches = await glob(getGlobPattern(item.pattern), getGlobOpts())
// preserves glob@8 behavior
matches = matches.sort((a, b) => a.localeCompare(b, 'en'))

for (const match of matches) {
let pkg
const packageJsonPathname = getPackagePathname(match, 'package.json')
const packagePathname = path.dirname(packageJsonPathname)

try {
pkg = await rpj(packageJsonPathname)
} catch (err) {
if (err.code === 'ENOENT') {
continue
} else {
throw err
}
}
let matches = await glob(patterns.map((p) => getGlobPattern(p)), getGlobOpts())
// preserves glob@8 behavior
matches = matches.sort((a, b) => a.localeCompare(b, 'en'))

// we must preserve the order of results according to the given list of
// workspace patterns
const orderedMatches = []
for (const pattern of patterns) {
orderedMatches.push(...matches.filter((m) => {
return minimatch(m, pattern, { partial: true, windowsPathsNoEscape: true })
}))
}

const name = getPackageName(pkg, packagePathname)
for (const match of orderedMatches) {
let pkg
const packageJsonPathname = getPackagePathname(match, 'package.json')

let seenPackagePathnames = seen.get(name)
if (!seenPackagePathnames) {
seenPackagePathnames = new Set()
seen.set(name, seenPackagePathnames)
}
if (item.negate) {
seenPackagePathnames.delete(packagePathname)
try {
pkg = await rpj(packageJsonPathname)
} catch (err) {
if (err.code === 'ENOENT') {
continue
} else {
seenPackagePathnames.add(packagePathname)
throw err
}
}

const packagePathname = path.dirname(packageJsonPathname)
const name = getPackageName(pkg, packagePathname)

let seenPackagePathnames = seen.get(name)
if (!seenPackagePathnames) {
seenPackagePathnames = new Set()
seen.set(name, seenPackagePathnames)
}
seenPackagePathnames.add(packagePathname)
}

const errorMessageArray = ['must not have multiple workspaces with the same name']
for (const [packageName, seenPackagePathnames] of seen) {
if (seenPackagePathnames.size === 0) {
continue
}
if (seenPackagePathnames.size > 1) {
addDuplicateErrorMessages(errorMessageArray, packageName, seenPackagePathnames)
} else {
Expand Down Expand Up @@ -177,30 +205,25 @@ mapWorkspaces.virtual = function (opts = {}) {
const { workspaces = [] } = packages[''] || {}
// uses a pathname-keyed map in order to negate the exact items
const results = new Map()
const patterns = getPatterns(workspaces)
if (!patterns.length) {
const { patterns, negatedPatterns } = getPatterns(workspaces)
if (!patterns.length && !negatedPatterns.length) {
return results
}
patterns.push({ pattern: '**/node_modules/**', negate: true })

const getPackagePathname = pkgPathmame(opts)
negatedPatterns.push('**/node_modules/**')

for (const packageKey of Object.keys(packages)) {
if (packageKey === '') {
continue
const packageKeys = Object.keys(packages)
for (const pattern of negatedPatterns) {
for (const packageKey of minimatch.match(packageKeys, pattern)) {
packageKeys.splice(packageKeys.indexOf(packageKey), 1)
}
}

for (const item of patterns) {
if (minimatch(packageKey, item.pattern)) {
const packagePathname = getPackagePathname(packageKey)
const name = getPackageName(packages[packageKey], packagePathname)

if (item.negate) {
results.delete(packagePathname)
} else {
results.set(packagePathname, name)
}
}
const getPackagePathname = pkgPathmame(opts)
for (const pattern of patterns) {
for (const packageKey of minimatch.match(packageKeys, pattern)) {
const packagePathname = getPackagePathname(packageKey)
const name = getPackageName(packages[packageKey], packagePathname)
results.set(packagePathname, name)
}
}

Expand Down
8 changes: 4 additions & 4 deletions node_modules/@npmcli/map-workspaces/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@npmcli/map-workspaces",
"version": "3.0.4",
"version": "3.0.6",
"main": "lib/index.js",
"files": [
"bin/",
Expand All @@ -25,7 +25,7 @@
"author": "GitHub Inc.",
"license": "ISC",
"scripts": {
"lint": "eslint \"**/*.js\"",
"lint": "eslint \"**/*.{js,cjs,ts,mjs,jsx,tsx}\"",
"pretest": "npm run lint",
"test": "tap",
"snap": "tap",
Expand All @@ -43,7 +43,7 @@
},
"devDependencies": {
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/template-oss": "4.14.1",
"@npmcli/template-oss": "4.21.3",
"tap": "^16.0.1"
},
"dependencies": {
Expand All @@ -54,7 +54,7 @@
},
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
"version": "4.14.1",
"version": "4.21.3",
"publish": "true"
}
}
8 changes: 4 additions & 4 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"@npmcli/arborist": "^7.2.1",
"@npmcli/config": "^8.0.2",
"@npmcli/fs": "^3.1.0",
"@npmcli/map-workspaces": "^3.0.4",
"@npmcli/map-workspaces": "^3.0.6",
"@npmcli/package-json": "^5.0.2",
"@npmcli/promise-spawn": "^7.0.1",
"@npmcli/redact": "^1.1.0",
Expand Down Expand Up @@ -1736,9 +1736,9 @@
}
},
"node_modules/@npmcli/map-workspaces": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/@npmcli/map-workspaces/-/map-workspaces-3.0.4.tgz",
"integrity": "sha512-Z0TbvXkRbacjFFLpVpV0e2mheCh+WzQpcqL+4xp49uNJOxOnIAPZyXtUxZ5Qn3QBTGKA11Exjd9a5411rBrhDg==",
"version": "3.0.6",
"resolved": "https://registry.npmjs.org/@npmcli/map-workspaces/-/map-workspaces-3.0.6.tgz",
"integrity": "sha512-tkYs0OYnzQm6iIRdfy+LcLBjcKuQCeE5YLb8KnrIlutJfheNaPvPpgoFEyEFgbjzl5PLZ3IA/BWAwRU0eHuQDA==",
"inBundle": true,
"dependencies": {
"@npmcli/name-from-folder": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@npmcli/arborist": "^7.2.1",
"@npmcli/config": "^8.0.2",
"@npmcli/fs": "^3.1.0",
"@npmcli/map-workspaces": "^3.0.4",
"@npmcli/map-workspaces": "^3.0.6",
"@npmcli/package-json": "^5.0.2",
"@npmcli/promise-spawn": "^7.0.1",
"@npmcli/redact": "^1.1.0",
Expand Down

0 comments on commit 699a1de

Please sign in to comment.