Skip to content

Commit

Permalink
fix: move explore command to @npmcli/package-json
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed May 22, 2023
1 parent 3238aa7 commit 87de0c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
6 changes: 3 additions & 3 deletions lib/commands/explore.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// npm explore <pkg>[@<version>]
// 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')
Expand Down Expand Up @@ -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
})
Expand Down
38 changes: 20 additions & 18 deletions test/lib/commands/explore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: {
Expand All @@ -53,7 +55,7 @@ const mockExplore = async (t, exec, {

return {
...mock,
RPJ_CALLED,
PJ_CALLED,
RUN_SCRIPT_EXEC,
output: cleanCwd(mock.joinedOutput()).trim(),
}
Expand All @@ -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/)
})
Expand All @@ -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/)

Expand Down Expand Up @@ -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, '')
Expand Down Expand Up @@ -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' }
)
})

0 comments on commit 87de0c7

Please sign in to comment.