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/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' } ) })