Skip to content

Commit

Permalink
fix: use @npmcli/package-json to parse packages
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar authored and reggi committed Sep 26, 2024
1 parent 044b3a8 commit 211aef3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
34 changes: 13 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path')

const getName = require('@npmcli/name-from-folder')
const { minimatch } = require('minimatch')
const rpj = require('read-package-json-fast')
const pkgJson = require('@npmcli/package-json')
const { glob } = require('glob')

function appendNegatedPatterns (allPatterns) {
Expand Down Expand Up @@ -67,15 +67,7 @@ function getPatterns (workspaces) {
}

function getPackageName (pkg, pathname) {
const { name } = pkg
return name || getName(pathname)
}

function pkgPathmame (opts) {
return (...args) => {
const cwd = opts.cwd ? opts.cwd : process.cwd()
return path.join.apply(null, [cwd, ...args])
}
return pkg.name || getName(pathname)
}

// make sure glob pattern only matches folders
Expand All @@ -101,16 +93,19 @@ async function mapWorkspaces (opts = {}) {
code: 'EMAPWORKSPACESPKG',
})
}
if (!opts.cwd) {
opts.cwd = process.cwd()
}

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

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

const seen = new Map()
const getGlobOpts = () => ({
...opts,
ignore: [
Expand All @@ -121,8 +116,6 @@ async function mapWorkspaces (opts = {}) {
],
})

const getPackagePathname = pkgPathmame(opts)

let matches = await glob(patterns.map((p) => getGlobPattern(p)), getGlobOpts())
// preserves glob@8 behavior
matches = matches.sort((a, b) => a.localeCompare(b, 'en'))
Expand All @@ -138,10 +131,8 @@ async function mapWorkspaces (opts = {}) {

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

try {
pkg = await rpj(packageJsonPathname)
pkg = await pkgJson.normalize(path.join(opts.cwd, match))
} catch (err) {
if (err.code === 'ENOENT') {
continue
Expand All @@ -150,15 +141,14 @@ async function mapWorkspaces (opts = {}) {
}
}

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

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

const errorMessageArray = ['must not have multiple workspaces with the same name']
Expand Down Expand Up @@ -200,6 +190,9 @@ mapWorkspaces.virtual = function (opts = {}) {
code: 'EMAPWORKSPACESLOCKFILE',
})
}
if (!opts.cwd) {
opts.cwd = process.cwd()
}

const { packages = {} } = opts.lockfile
const { workspaces = [] } = packages[''] || {}
Expand All @@ -218,10 +211,9 @@ mapWorkspaces.virtual = function (opts = {}) {
}
}

const getPackagePathname = pkgPathmame(opts)
for (const pattern of patterns) {
for (const packageKey of minimatch.match(packageKeys, pattern)) {
const packagePathname = getPackagePathname(packageKey)
const packagePathname = path.join(opts.cwd, packageKey)
const name = getPackageName(packages[packageKey], packagePathname)
results.set(packagePathname, name)
}
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ test('unexpected rpj errors', t => {
err.code = 'ERR'

const mapW = t.mock('../', {
'read-package-json-fast': () => Promise.reject(err),
'@npmcli/package-json': { normalize: () => Promise.reject(err) },
})

return t.rejects(
Expand Down

0 comments on commit 211aef3

Please sign in to comment.