From f37fcdcdac0bf3b7f5a000632262411c2458d174 Mon Sep 17 00:00:00 2001 From: Gar Date: Thu, 30 May 2024 10:10:31 -0700 Subject: [PATCH 1/3] deps: remove read-package-json-fast --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 3afe2ae..9940f4e 100644 --- a/package.json +++ b/package.json @@ -50,8 +50,7 @@ "dependencies": { "@npmcli/name-from-folder": "^3.0.0", "glob": "^10.2.2", - "minimatch": "^9.0.0", - "read-package-json-fast": "^4.0.0" + "minimatch": "^9.0.0" }, "templateOSS": { "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.", From dde24f17dddc1b509bf970aadf32d8e48da3117e Mon Sep 17 00:00:00 2001 From: Gar Date: Mon, 3 Jun 2024 08:59:11 -0700 Subject: [PATCH 2/3] deps: @npmcli/package-json@5.2.0 --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 9940f4e..0bb40a5 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ }, "dependencies": { "@npmcli/name-from-folder": "^3.0.0", + "@npmcli/package-json": "^5.2.0", "glob": "^10.2.2", "minimatch": "^9.0.0" }, From 42a46958c453e898cf0589f4f20f56385cd65fa7 Mon Sep 17 00:00:00 2001 From: Gar Date: Thu, 30 May 2024 11:01:02 -0700 Subject: [PATCH 3/3] fix: use @npmcli/package-json to parse packages --- lib/index.js | 34 +++++++++++++--------------------- test/test.js | 2 +- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/lib/index.js b/lib/index.js index b20bf5d..1b39d2e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { @@ -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 @@ -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: [ @@ -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')) @@ -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 @@ -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'] @@ -200,6 +190,9 @@ mapWorkspaces.virtual = function (opts = {}) { code: 'EMAPWORKSPACESLOCKFILE', }) } + if (!opts.cwd) { + opts.cwd = process.cwd() + } const { packages = {} } = opts.lockfile const { workspaces = [] } = packages[''] || {} @@ -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) } diff --git a/test/test.js b/test/test.js index b8f98b4..3c9f98f 100644 --- a/test/test.js +++ b/test/test.js @@ -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(