diff --git a/DEPENDENCIES.md b/DEPENDENCIES.md index 192c9cdf7903c..861e3a30a6157 100644 --- a/DEPENDENCIES.md +++ b/DEPENDENCIES.md @@ -132,7 +132,6 @@ graph LR; npm-->parse-conflict-json; npm-->proc-log; npm-->read-package-json-fast; - npm-->read-package-json; npm-->read; npm-->semver; npm-->ssri; @@ -563,7 +562,6 @@ graph LR; npm-->proc-log; npm-->qrcode-terminal; npm-->read-package-json-fast; - npm-->read-package-json; npm-->read; npm-->remark-gfm; npm-->remark-github; diff --git a/lib/commands/access.js b/lib/commands/access.js index 318151fc81e2c..2417f3a3f2c85 100644 --- a/lib/commands/access.js +++ b/lib/commands/access.js @@ -1,8 +1,6 @@ -const path = require('path') - const libnpmaccess = require('libnpmaccess') const npa = require('npm-package-arg') -const readPackageJson = require('read-package-json-fast') +const pkgJson = require('@npmcli/package-json') const localeCompare = require('@isaacs/string-locale-compare')('en') const otplease = require('../utils/otplease.js') @@ -178,8 +176,8 @@ class Access extends BaseCommand { async #getPackage (name, requireScope) { if (!name) { try { - const pkg = await readPackageJson(path.resolve(this.npm.prefix, 'package.json')) - name = pkg.name + const { content } = await pkgJson.normalize(this.npm.prefix) + name = content.name } catch (err) { if (err.code === 'ENOENT') { throw Object.assign(new Error('no package name given and no package.json found'), { diff --git a/lib/commands/config.js b/lib/commands/config.js index dfd44015cd043..576c8b56a6f9c 100644 --- a/lib/commands/config.js +++ b/lib/commands/config.js @@ -7,7 +7,7 @@ const { spawn } = require('child_process') const { EOL } = require('os') const ini = require('ini') const localeCompare = require('@isaacs/string-locale-compare')('en') -const rpj = require('read-package-json-fast') +const pkgJson = require('@npmcli/package-json') const log = require('../utils/log-shim.js') // These are the configs that we can nerf-dart. Not all of them currently even @@ -346,15 +346,15 @@ ${defData} } if (!this.npm.global) { - const pkgPath = resolve(this.npm.prefix, 'package.json') - const pkg = await rpj(pkgPath).catch(() => ({})) + const { content } = await pkgJson.normalize(this.npm.prefix).catch(() => ({ content: {} })) - if (pkg.publishConfig) { + if (content.publishConfig) { + const pkgPath = resolve(this.npm.prefix, 'package.json') msg.push(`; "publishConfig" from ${pkgPath}`) msg.push('; This set of config values will be used at publish-time.', '') - const pkgKeys = Object.keys(pkg.publishConfig).sort(localeCompare) + const pkgKeys = Object.keys(content.publishConfig).sort(localeCompare) for (const k of pkgKeys) { - const v = publicVar(k) ? JSON.stringify(pkg.publishConfig[k]) : '(protected)' + const v = publicVar(k) ? JSON.stringify(content.publishConfig[k]) : '(protected)' msg.push(`${k} = ${v}`) } msg.push('') diff --git a/lib/commands/diff.js b/lib/commands/diff.js index 3924166af0a88..64d81d525d79d 100644 --- a/lib/commands/diff.js +++ b/lib/commands/diff.js @@ -5,7 +5,7 @@ const npa = require('npm-package-arg') const pacote = require('pacote') const pickManifest = require('npm-pick-manifest') const log = require('../utils/log-shim') -const readPackage = require('read-package-json-fast') +const pkgJson = require('@npmcli/package-json') const BaseCommand = require('../base-command.js') class Diff extends BaseCommand { @@ -81,7 +81,7 @@ class Diff extends BaseCommand { async packageName (path) { let name try { - const pkg = await readPackage(resolve(this.prefix, 'package.json')) + const { content: pkg } = await pkgJson.normalize(this.prefix) name = pkg.name } catch (e) { log.verbose('diff', 'could not read project dir package.json') @@ -115,7 +115,7 @@ class Diff extends BaseCommand { let noPackageJson let pkgName try { - const pkg = await readPackage(resolve(this.prefix, 'package.json')) + const { content: pkg } = await pkgJson.normalize(this.prefix) pkgName = pkg.name } catch (e) { log.verbose('diff', 'could not read project dir package.json') @@ -228,7 +228,7 @@ class Diff extends BaseCommand { if (semverA && semverB) { let pkgName try { - const pkg = await readPackage(resolve(this.prefix, 'package.json')) + const { content: pkg } = await pkgJson.normalize(this.prefix) pkgName = pkg.name } catch (e) { log.verbose('diff', 'could not read project dir package.json') diff --git a/lib/commands/dist-tag.js b/lib/commands/dist-tag.js index bc61a4691e55a..584fbbc8ad6e0 100644 --- a/lib/commands/dist-tag.js +++ b/lib/commands/dist-tag.js @@ -1,10 +1,9 @@ const npa = require('npm-package-arg') -const path = require('path') const regFetch = require('npm-registry-fetch') const semver = require('semver') const log = require('../utils/log-shim') const otplease = require('../utils/otplease.js') -const readPackage = require('read-package-json-fast') +const pkgJson = require('@npmcli/package-json') const BaseCommand = require('../base-command.js') class DistTag extends BaseCommand { @@ -152,7 +151,7 @@ class DistTag extends BaseCommand { if (this.npm.global) { throw this.usageError() } - const { name } = await readPackage(path.resolve(this.npm.prefix, 'package.json')) + const { content: { name } } = await pkgJson.normalize(this.npm.prefix) if (!name) { throw this.usageError() } diff --git a/lib/commands/explore.js b/lib/commands/explore.js index 0d915cb4c6958..63667114d3f68 100644 --- a/lib/commands/explore.js +++ b/lib/commands/explore.js @@ -1,9 +1,9 @@ // npm explore [@] // open a subshell to the package folder. -const rpj = require('read-package-json-fast') +const pkgJson = require('@npmcli/package-json') const runScript = require('@npmcli/run-script') -const { join, resolve, relative } = require('path') +const { join, relative } = require('path') const log = require('../utils/log-shim.js') const completion = require('../utils/completion/installed-shallow.js') const BaseCommand = require('../base-command.js') @@ -38,7 +38,7 @@ class Explore extends BaseCommand { // the set of arguments, or the shell config, and let @npmcli/run-script // handle all the escaping and PATH setup stuff. - const pkg = await rpj(resolve(path, 'package.json')).catch(er => { + const { content: pkg } = await pkgJson.normalize(path).catch(er => { log.error('explore', `It doesn't look like ${pkgname} is installed.`) throw er }) diff --git a/lib/commands/init.js b/lib/commands/init.js index 1e5661a7840f2..4f77b2903c100 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -3,7 +3,6 @@ const { relative, resolve } = require('path') const { mkdir } = require('fs/promises') const initJson = require('init-package-json') const npa = require('npm-package-arg') -const rpj = require('read-package-json-fast') const libexec = require('libnpmexec') const mapWorkspaces = require('@npmcli/map-workspaces') const PackageJson = require('@npmcli/package-json') @@ -54,7 +53,7 @@ class Init extends BaseCommand { // reads package.json for the top-level folder first, by doing this we // ensure the command throw if no package.json is found before trying // to create a workspace package.json file or its folders - const pkg = await rpj(resolve(this.npm.localPrefix, 'package.json')).catch((err) => { + const { content: pkg } = await PackageJson.normalize(this.npm.localPrefix).catch(err => { if (err.code === 'ENOENT') { log.warn('Missing package.json. Try with `--include-workspace-root`.') } @@ -217,7 +216,7 @@ class Init extends BaseCommand { // translate workspaces paths into an array containing workspaces names const workspaces = [] for (const path of workspacesPaths) { - const { name } = await rpj(resolve(path, 'package.json')).catch(() => ({})) + const { content: { name } } = await PackageJson.normalize(path).catch(() => ({ content: {} })) if (name) { workspaces.push(name) diff --git a/lib/commands/link.js b/lib/commands/link.js index 61ae5a9808868..725912c2e7aa6 100644 --- a/lib/commands/link.js +++ b/lib/commands/link.js @@ -4,7 +4,7 @@ const readdir = util.promisify(fs.readdir) const { resolve } = require('path') const npa = require('npm-package-arg') -const rpj = require('read-package-json-fast') +const pkgJson = require('@npmcli/package-json') const semver = require('semver') const reifyFinish = require('../utils/reify-finish.js') @@ -96,11 +96,12 @@ class Link extends ArboristWorkspaceCmd { const names = [] for (const a of args) { const arg = npa(a) - names.push( - arg.type === 'directory' - ? (await rpj(resolve(arg.fetchSpec, 'package.json'))).name - : arg.name - ) + if (arg.type === 'directory') { + const { content } = await pkgJson.normalize(arg.fetchSpec) + names.push(content.name) + } else { + names.push(arg.name) + } } // npm link should not save=true by default unless you're diff --git a/lib/commands/owner.js b/lib/commands/owner.js index 3a997db800db7..201f348d689a3 100644 --- a/lib/commands/owner.js +++ b/lib/commands/owner.js @@ -3,14 +3,13 @@ const npmFetch = require('npm-registry-fetch') const pacote = require('pacote') const log = require('../utils/log-shim') const otplease = require('../utils/otplease.js') -const readPackageJsonFast = require('read-package-json-fast') +const pkgJson = require('@npmcli/package-json') const BaseCommand = require('../base-command.js') -const { resolve } = require('path') -const readJson = async (pkg) => { +const readJson = async (path) => { try { - const json = await readPackageJsonFast(pkg) - return json + const { content } = await pkgJson.normalize(path) + return content } catch { return {} } @@ -54,7 +53,7 @@ class Owner extends BaseCommand { if (this.npm.global) { return [] } - const { name } = await readJson(resolve(this.npm.prefix, 'package.json')) + const { name } = await readJson(this.npm.prefix) if (!name) { return [] } @@ -130,7 +129,7 @@ class Owner extends BaseCommand { if (this.npm.global) { throw this.usageError() } - const { name } = await readJson(resolve(prefix, 'package.json')) + const { name } = await readJson(prefix) if (!name) { throw this.usageError() } diff --git a/lib/commands/publish.js b/lib/commands/publish.js index 8befbc5ca34ce..54707278f9691 100644 --- a/lib/commands/publish.js +++ b/lib/commands/publish.js @@ -1,4 +1,3 @@ -const util = require('util') const log = require('../utils/log-shim.js') const semver = require('semver') const pack = require('libnpmpack') @@ -17,11 +16,7 @@ const { getContents, logTar } = require('../utils/tar.js') // revisit this at some point, and have a minimal set that's a SemVer-major // change that ought to get a RFC written on it. const { flatten } = require('../utils/config/index.js') - -// this is the only case in the CLI where we want to use the old full slow -// 'read-package-json' module, because we want to pull in all the defaults and -// metadata, like git sha's and default scripts and all that. -const readJson = util.promisify(require('read-package-json')) +const pkgJson = require('@npmcli/package-json') const BaseCommand = require('../base-command.js') class Publish extends BaseCommand { @@ -204,7 +199,9 @@ class Publish extends BaseCommand { async getManifest (spec, opts) { let manifest if (spec.type === 'directory') { - manifest = await readJson(`${spec.fetchSpec}/package.json`) + // Prepare is the special function for publishing, different than normalize + const { content } = await pkgJson.prepare(spec.fetchSpec) + manifest = content } else { manifest = await pacote.manifest(spec, { ...opts, diff --git a/lib/commands/run-script.js b/lib/commands/run-script.js index e1bce0e52a513..6b568dab65249 100644 --- a/lib/commands/run-script.js +++ b/lib/commands/run-script.js @@ -1,7 +1,6 @@ -const { resolve } = require('path') const runScript = require('@npmcli/run-script') const { isServerPackage } = runScript -const rpj = require('read-package-json-fast') +const pkgJson = require('@npmcli/package-json') const log = require('../utils/log-shim.js') const didYouMean = require('../utils/did-you-mean.js') const { isWindowsShell } = require('../utils/is-windows.js') @@ -39,9 +38,8 @@ class RunScript extends BaseCommand { async completion (opts) { const argv = opts.conf.argv.remain if (argv.length === 2) { - // find the script name - const json = resolve(this.npm.localPrefix, 'package.json') - const { scripts = {} } = await rpj(json).catch(er => ({})) + const { content: { scripts = {} } } = await pkgJson.normalize(this.npm.localPrefix) + .catch(er => ({ content: {} })) if (opts.isFish) { return Object.keys(scripts).map(s => `${s}\t${scripts[s].slice(0, 30)}`) } @@ -70,7 +68,10 @@ class RunScript extends BaseCommand { // null value const scriptShell = this.npm.config.get('script-shell') || undefined - pkg = pkg || (await rpj(`${path}/package.json`)) + if (!pkg) { + const { content } = await pkgJson.normalize(path) + pkg = content + } const { scripts = {} } = pkg if (event === 'restart' && !scripts.restart) { @@ -126,8 +127,8 @@ class RunScript extends BaseCommand { } async list (args, path) { - path = path || this.npm.localPrefix - const { scripts, name, _id } = await rpj(`${path}/package.json`) + /* eslint-disable-next-line max-len */ + const { content: { scripts, name, _id } } = await pkgJson.normalize(path || this.npm.localPrefix) const pkgid = _id || name if (!scripts) { @@ -197,7 +198,7 @@ class RunScript extends BaseCommand { await this.setWorkspaces() for (const workspacePath of this.workspacePaths) { - const pkg = await rpj(`${workspacePath}/package.json`) + const { content: pkg } = await pkgJson.normalize(workspacePath) const runResult = await this.run(args, { path: workspacePath, pkg, @@ -236,7 +237,7 @@ class RunScript extends BaseCommand { if (this.npm.config.get('json')) { const res = {} for (const workspacePath of this.workspacePaths) { - const { scripts, name } = await rpj(`${workspacePath}/package.json`) + const { content: { scripts, name } } = await pkgJson.normalize(workspacePath) res[name] = { ...scripts } } this.npm.output(JSON.stringify(res, null, 2)) @@ -245,7 +246,7 @@ class RunScript extends BaseCommand { if (this.npm.config.get('parseable')) { for (const workspacePath of this.workspacePaths) { - const { scripts, name } = await rpj(`${workspacePath}/package.json`) + const { content: { scripts, name } } = await pkgJson.normalize(workspacePath) for (const [script, cmd] of Object.entries(scripts || {})) { this.npm.output(`${name}:${script}:${cmd}`) } diff --git a/lib/commands/uninstall.js b/lib/commands/uninstall.js index e5373119ec757..c862c0c461054 100644 --- a/lib/commands/uninstall.js +++ b/lib/commands/uninstall.js @@ -1,5 +1,5 @@ const { resolve } = require('path') -const rpj = require('read-package-json-fast') +const pkgJson = require('@npmcli/package-json') const reifyFinish = require('../utils/reify-finish.js') const completion = require('../utils/completion/installed-shallow.js') @@ -24,7 +24,7 @@ class Uninstall extends ArboristWorkspaceCmd { throw new Error('Must provide a package name to remove') } else { try { - const pkg = await rpj(resolve(this.npm.localPrefix, 'package.json')) + const { content: pkg } = await pkgJson.normalize(this.npm.localPrefix) args.push(pkg.name) } catch (er) { if (er.code !== 'ENOENT' && er.code !== 'ENOTDIR') { diff --git a/lib/commands/unpublish.js b/lib/commands/unpublish.js index f1bcded192e5a..0df1ce0cc74f9 100644 --- a/lib/commands/unpublish.js +++ b/lib/commands/unpublish.js @@ -2,9 +2,7 @@ const libaccess = require('libnpmaccess') const libunpub = require('libnpmpublish').unpublish const npa = require('npm-package-arg') const npmFetch = require('npm-registry-fetch') -const path = require('path') -const util = require('util') -const readJson = util.promisify(require('read-package-json')) +const pkgJson = require('@npmcli/package-json') const { flatten } = require('../utils/config/index.js') const getIdentity = require('../utils/get-identity.js') @@ -96,8 +94,8 @@ class Unpublish extends BaseCommand { let manifest let manifestErr try { - const pkgJson = path.join(this.npm.localPrefix, 'package.json') - manifest = await readJson(pkgJson) + const { content } = await pkgJson.prepare(this.npm.localPrefix) + manifest = content } catch (err) { manifestErr = err } diff --git a/lib/utils/did-you-mean.js b/lib/utils/did-you-mean.js index 10b33d5f83a08..e6c1ba3f72e5b 100644 --- a/lib/utils/did-you-mean.js +++ b/lib/utils/did-you-mean.js @@ -1,5 +1,5 @@ const { distance } = require('fastest-levenshtein') -const readJson = require('read-package-json-fast') +const pkgJson = require('@npmcli/package-json') const { commands } = require('./cmd-list.js') const didYouMean = async (npm, path, scmd) => { @@ -13,7 +13,7 @@ const didYouMean = async (npm, path, scmd) => { // We would already be suggesting this in `npm x` so omit them here const runScripts = ['stop', 'start', 'test', 'restart'] try { - const { bin, scripts } = await readJson(`${path}/package.json`) + const { content: { scripts, bin } } = await pkgJson.normalize(path) best = best.concat( Object.keys(scripts || {}) .filter(cmd => distance(scmd, cmd) < scmd.length * 0.4 && !runScripts.includes(cmd)) diff --git a/lib/workspaces/get-workspaces.js b/lib/workspaces/get-workspaces.js index 7065533596000..59efb0e9f01be 100644 --- a/lib/workspaces/get-workspaces.js +++ b/lib/workspaces/get-workspaces.js @@ -1,7 +1,7 @@ const { resolve, relative } = require('path') const mapWorkspaces = require('@npmcli/map-workspaces') const { minimatch } = require('minimatch') -const rpj = require('read-package-json-fast') +const pkgJson = require('@npmcli/package-json') // minimatch wants forward slashes only for glob patterns const globify = pattern => pattern.split('\\').join('/') @@ -9,8 +9,8 @@ const globify = pattern => pattern.split('\\').join('/') // Returns an Map of paths to workspaces indexed by workspace name // { foo => '/path/to/foo' } const getWorkspaces = async (filters, { path, includeWorkspaceRoot, relativeFrom }) => { - // TODO we need a better error to be bubbled up here if this rpj call fails - const pkg = await rpj(resolve(path, 'package.json')) + // TODO we need a better error to be bubbled up here if this call fails + const { content: pkg } = await pkgJson.normalize(path) const workspaces = await mapWorkspaces({ cwd: path, pkg }) let res = new Map() if (includeWorkspaceRoot) { diff --git a/package-lock.json b/package-lock.json index 47354477e0381..b9382d1d6d1dc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -63,8 +63,6 @@ "proc-log", "qrcode-terminal", "read", - "read-package-json", - "read-package-json-fast", "semver", "ssri", "tar", @@ -138,8 +136,6 @@ "proc-log": "^3.0.0", "qrcode-terminal": "^0.12.0", "read": "^2.1.0", - "read-package-json": "^6.0.3", - "read-package-json-fast": "^3.0.2", "semver": "^7.5.1", "ssri": "^10.0.4", "tar": "^6.1.14", diff --git a/package.json b/package.json index 148a2a3b9e56a..768572a8d8b5c 100644 --- a/package.json +++ b/package.json @@ -107,8 +107,6 @@ "proc-log": "^3.0.0", "qrcode-terminal": "^0.12.0", "read": "^2.1.0", - "read-package-json": "^6.0.3", - "read-package-json-fast": "^3.0.2", "semver": "^7.5.1", "ssri": "^10.0.4", "tar": "^6.1.14", @@ -175,8 +173,6 @@ "proc-log", "qrcode-terminal", "read", - "read-package-json", - "read-package-json-fast", "semver", "ssri", "tar", diff --git a/test/lib/commands/explore.js b/test/lib/commands/explore.js index 786a34a8e2988..6988dca90fbfb 100644 --- a/test/lib/commands/explore.js +++ b/test/lib/commands/explore.js @@ -3,18 +3,20 @@ const mockNpm = require('../../fixtures/mock-npm') const { cleanCwd } = require('../../fixtures/clean-snapshot') const mockExplore = async (t, exec, { - RPJ_ERROR = null, + PJ_ERROR = null, RUN_SCRIPT_ERROR = null, RUN_SCRIPT_EXIT_CODE = 0, RUN_SCRIPT_SIGNAL = null, } = {}) => { - let RPJ_CALLED = '' - const mockRPJ = async path => { - if (RPJ_ERROR) { - throw RPJ_ERROR - } - RPJ_CALLED = cleanCwd(path) - return { some: 'package' } + let PJ_CALLED = '' + const mockPJ = { + normalize: async path => { + if (PJ_ERROR) { + throw PJ_ERROR + } + PJ_CALLED = cleanCwd(path) + return { content: { some: 'package' } } + }, } let RUN_SCRIPT_EXEC = null @@ -41,7 +43,7 @@ const mockExplore = async (t, exec, { const mock = await mockNpm(t, { mocks: { - 'read-package-json-fast': mockRPJ, + '@npmcli/package-json': mockPJ, '@npmcli/run-script': mockRunScript, }, config: { @@ -53,7 +55,7 @@ const mockExplore = async (t, exec, { return { ...mock, - RPJ_CALLED, + PJ_CALLED, RUN_SCRIPT_EXEC, output: cleanCwd(mock.joinedOutput()).trim(), } @@ -62,11 +64,11 @@ const mockExplore = async (t, exec, { t.test('basic interactive', async t => { const { output, - RPJ_CALLED, + PJ_CALLED, RUN_SCRIPT_EXEC, } = await mockExplore(t, ['pkg']) - t.match(RPJ_CALLED, /\/pkg\/package.json$/) + t.ok(PJ_CALLED.endsWith('/pkg')) t.strictSame(RUN_SCRIPT_EXEC, 'shell-command') t.match(output, /Exploring \{CWD\}\/[\w-_/]+\nType 'exit' or \^D when finished/) }) @@ -75,11 +77,11 @@ t.test('interactive tracks exit code', async t => { t.test('code', async t => { const { output, - RPJ_CALLED, + PJ_CALLED, RUN_SCRIPT_EXEC, } = await mockExplore(t, ['pkg'], { RUN_SCRIPT_EXIT_CODE: 99 }) - t.match(RPJ_CALLED, /\/pkg\/package.json$/) + t.ok(PJ_CALLED.endsWith('/pkg')) t.strictSame(RUN_SCRIPT_EXEC, 'shell-command') t.match(output, /Exploring \{CWD\}\/[\w-_/]+\nType 'exit' or \^D when finished/) @@ -123,11 +125,11 @@ t.test('interactive tracks exit code', async t => { t.test('basic non-interactive', async t => { const { output, - RPJ_CALLED, + PJ_CALLED, RUN_SCRIPT_EXEC, } = await mockExplore(t, ['pkg', 'ls']) - t.match(RPJ_CALLED, /\/pkg\/package.json$/) + t.ok(PJ_CALLED.endsWith('/pkg')) t.strictSame(RUN_SCRIPT_EXEC, 'ls') t.strictSame(output, '') @@ -164,10 +166,10 @@ t.test('usage if no pkg provided', async t => { }) t.test('pkg not installed', async t => { - const RPJ_ERROR = new Error('plurple') + const PJ_ERROR = new Error('plurple') await t.rejects( - mockExplore(t, ['pkg', 'ls'], { RPJ_ERROR }), + mockExplore(t, ['pkg', 'ls'], { PJ_ERROR }), { message: 'plurple' } ) }) diff --git a/test/lib/commands/init.js b/test/lib/commands/init.js index 00caf90d0ec9b..cb708303f405a 100644 --- a/test/lib/commands/init.js +++ b/test/lib/commands/init.js @@ -6,13 +6,12 @@ const { cleanTime } = require('../../fixtures/clean-snapshot') t.cleanSnapshot = cleanTime -const mockNpm = async (t, { noLog, libnpmexec, initPackageJson, packageJson, ...opts } = {}) => { +const mockNpm = async (t, { noLog, libnpmexec, initPackageJson, ...opts } = {}) => { const res = await _mockNpm(t, { ...opts, mocks: { ...(libnpmexec ? { libnpmexec } : {}), ...(initPackageJson ? { 'init-package-json': initPackageJson } : {}), - ...(packageJson ? { '@npmcli/package-json': packageJson } : {}), }, globals: { // init-package-json prints directly to console.log @@ -313,14 +312,7 @@ t.test('workspaces', async t => { await t.test('fail parsing top-level package.json to set workspace', async t => { const { npm } = await mockNpm(t, { prefixDir: { - 'package.json': JSON.stringify({ - name: 'top-level', - }), - }, - packageJson: { - async load () { - throw new Error('ERR') - }, + 'package.json': 'not json[', }, config: { workspace: 'a', yes: true }, noLog: true, @@ -328,8 +320,7 @@ t.test('workspaces', async t => { await t.rejects( npm.exec('init', []), - /ERR/, - 'should exit with error' + { code: 'EJSONPARSE' } ) }) diff --git a/test/lib/commands/uninstall.js b/test/lib/commands/uninstall.js index 59a517d144d38..ae116d44c208b 100644 --- a/test/lib/commands/uninstall.js +++ b/test/lib/commands/uninstall.js @@ -188,19 +188,15 @@ t.test('no args global but no package.json', async t => { ) }) -t.test('unknown error reading from localPrefix package.json', async t => { +t.test('non ENOENT error reading from localPrefix package.json', async t => { const { uninstall } = await mockNpm(t, { config: { global: true }, - mocks: { - 'read-package-json-fast': async () => { - throw new Error('ERR') - }, - }, + prefixDir: { 'package.json': 'not[json]' }, }) await t.rejects( uninstall([]), - /ERR/, - 'should throw unknown error' + { code: 'EJSONPARSE' }, + 'should throw non ENOENT error' ) }) diff --git a/test/lib/commands/unpublish.js b/test/lib/commands/unpublish.js index 96c06bf3ffee6..da317fbde8611 100644 --- a/test/lib/commands/unpublish.js +++ b/test/lib/commands/unpublish.js @@ -58,7 +58,7 @@ t.test('no args --force error reading package.json', async t => { await t.rejects( npm.exec('unpublish', []), - /Failed to parse json/, + /Invalid package.json/, 'should throw error from reading package.json' ) })