Skip to content

Commit 87de0c7

Browse files
committed
fix: move explore command to @npmcli/package-json
1 parent 3238aa7 commit 87de0c7

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

lib/commands/explore.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// npm explore <pkg>[@<version>]
22
// open a subshell to the package folder.
33

4-
const rpj = require('read-package-json-fast')
4+
const pkgJson = require('@npmcli/package-json')
55
const runScript = require('@npmcli/run-script')
6-
const { join, resolve, relative } = require('path')
6+
const { join, relative } = require('path')
77
const log = require('../utils/log-shim.js')
88
const completion = require('../utils/completion/installed-shallow.js')
99
const BaseCommand = require('../base-command.js')
@@ -38,7 +38,7 @@ class Explore extends BaseCommand {
3838
// the set of arguments, or the shell config, and let @npmcli/run-script
3939
// handle all the escaping and PATH setup stuff.
4040

41-
const pkg = await rpj(resolve(path, 'package.json')).catch(er => {
41+
const { content: pkg } = await pkgJson.normalize(path).catch(er => {
4242
log.error('explore', `It doesn't look like ${pkgname} is installed.`)
4343
throw er
4444
})

test/lib/commands/explore.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ const mockNpm = require('../../fixtures/mock-npm')
33
const { cleanCwd } = require('../../fixtures/clean-snapshot')
44

55
const mockExplore = async (t, exec, {
6-
RPJ_ERROR = null,
6+
PJ_ERROR = null,
77
RUN_SCRIPT_ERROR = null,
88
RUN_SCRIPT_EXIT_CODE = 0,
99
RUN_SCRIPT_SIGNAL = null,
1010
} = {}) => {
11-
let RPJ_CALLED = ''
12-
const mockRPJ = async path => {
13-
if (RPJ_ERROR) {
14-
throw RPJ_ERROR
15-
}
16-
RPJ_CALLED = cleanCwd(path)
17-
return { some: 'package' }
11+
let PJ_CALLED = ''
12+
const mockPJ = {
13+
normalize: async path => {
14+
if (PJ_ERROR) {
15+
throw PJ_ERROR
16+
}
17+
PJ_CALLED = cleanCwd(path)
18+
return { content: { some: 'package' } }
19+
},
1820
}
1921

2022
let RUN_SCRIPT_EXEC = null
@@ -41,7 +43,7 @@ const mockExplore = async (t, exec, {
4143

4244
const mock = await mockNpm(t, {
4345
mocks: {
44-
'read-package-json-fast': mockRPJ,
46+
'@npmcli/package-json': mockPJ,
4547
'@npmcli/run-script': mockRunScript,
4648
},
4749
config: {
@@ -53,7 +55,7 @@ const mockExplore = async (t, exec, {
5355

5456
return {
5557
...mock,
56-
RPJ_CALLED,
58+
PJ_CALLED,
5759
RUN_SCRIPT_EXEC,
5860
output: cleanCwd(mock.joinedOutput()).trim(),
5961
}
@@ -62,11 +64,11 @@ const mockExplore = async (t, exec, {
6264
t.test('basic interactive', async t => {
6365
const {
6466
output,
65-
RPJ_CALLED,
67+
PJ_CALLED,
6668
RUN_SCRIPT_EXEC,
6769
} = await mockExplore(t, ['pkg'])
6870

69-
t.match(RPJ_CALLED, /\/pkg\/package.json$/)
71+
t.ok(PJ_CALLED.endsWith('/pkg'))
7072
t.strictSame(RUN_SCRIPT_EXEC, 'shell-command')
7173
t.match(output, /Exploring \{CWD\}\/[\w-_/]+\nType 'exit' or \^D when finished/)
7274
})
@@ -75,11 +77,11 @@ t.test('interactive tracks exit code', async t => {
7577
t.test('code', async t => {
7678
const {
7779
output,
78-
RPJ_CALLED,
80+
PJ_CALLED,
7981
RUN_SCRIPT_EXEC,
8082
} = await mockExplore(t, ['pkg'], { RUN_SCRIPT_EXIT_CODE: 99 })
8183

82-
t.match(RPJ_CALLED, /\/pkg\/package.json$/)
84+
t.ok(PJ_CALLED.endsWith('/pkg'))
8385
t.strictSame(RUN_SCRIPT_EXEC, 'shell-command')
8486
t.match(output, /Exploring \{CWD\}\/[\w-_/]+\nType 'exit' or \^D when finished/)
8587

@@ -123,11 +125,11 @@ t.test('interactive tracks exit code', async t => {
123125
t.test('basic non-interactive', async t => {
124126
const {
125127
output,
126-
RPJ_CALLED,
128+
PJ_CALLED,
127129
RUN_SCRIPT_EXEC,
128130
} = await mockExplore(t, ['pkg', 'ls'])
129131

130-
t.match(RPJ_CALLED, /\/pkg\/package.json$/)
132+
t.ok(PJ_CALLED.endsWith('/pkg'))
131133
t.strictSame(RUN_SCRIPT_EXEC, 'ls')
132134

133135
t.strictSame(output, '')
@@ -164,10 +166,10 @@ t.test('usage if no pkg provided', async t => {
164166
})
165167

166168
t.test('pkg not installed', async t => {
167-
const RPJ_ERROR = new Error('plurple')
169+
const PJ_ERROR = new Error('plurple')
168170

169171
await t.rejects(
170-
mockExplore(t, ['pkg', 'ls'], { RPJ_ERROR }),
172+
mockExplore(t, ['pkg', 'ls'], { PJ_ERROR }),
171173
{ message: 'plurple' }
172174
)
173175
})

0 commit comments

Comments
 (0)