Skip to content

Commit 44c0c5c

Browse files
committed
fix(help|edit): use npm.exec, use file:///
Windows no longer allows `file://`, but instead requires `file:///` for local file urls. Also after the command refactor things shifted and the tests didn't catch that the help pages didn't work at all anymore, and that the post-edit rebuild didn't work. This cleans up those two issues.
1 parent cad9bc7 commit 44c0c5c

File tree

7 files changed

+19
-37
lines changed

7 files changed

+19
-37
lines changed

lib/base-command.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Base class for npm.commands[cmd]
1+
// Base class for npm commands
22
const usageUtil = require('./utils/usage.js')
33
const ConfigDefinitions = require('./utils/config/definitions.js')
44
const getWorkspaces = require('./workspaces/get-workspaces.js')

lib/commands/edit.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ class Edit extends BaseCommand {
5050
editor.on('exit', (code) => {
5151
if (code)
5252
return reject(new Error(`editor process exited with code: ${code}`))
53-
this.npm.commands.rebuild([dir], (err) => {
54-
if (err)
55-
return reject(err)
56-
57-
resolve()
58-
})
53+
this.npm.exec('rebuild', [dir]).catch(reject).then(resolve)
5954
})
6055
})
6156
})

lib/commands/help-search.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class HelpSearch extends BaseCommand {
3030
if (!args.length)
3131
return this.npm.output(this.usage)
3232

33-
const docPath = path.resolve(__dirname, '..', 'docs/content')
33+
const docPath = path.resolve(__dirname, '..', '..', 'docs/content')
3434
const files = await glob(`${docPath}/*/*.md`)
3535
const data = await this.readFiles(files)
3636
const results = await this.searchFiles(args, data, files)

lib/commands/help.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Help extends BaseCommand {
3636
async completion (opts) {
3737
if (opts.conf.argv.remain.length > 2)
3838
return []
39-
const g = path.resolve(__dirname, '../man/man[0-9]/*.[0-9]')
39+
const g = path.resolve(__dirname, '../../man/man[0-9]/*.[0-9]')
4040
const files = await glob(g)
4141

4242
return Object.keys(files.reduce(function (acc, file) {
@@ -66,7 +66,7 @@ class Help extends BaseCommand {
6666
// support `npm help package.json`
6767
section = section.replace('.json', '-json')
6868

69-
const manroot = path.resolve(__dirname, '..', 'man')
69+
const manroot = path.resolve(__dirname, '..', '..', 'man')
7070
// find either section.n or npm-section.n
7171
const f = `${manroot}/${manSearch}/?(npm-)${section}.[0-9]*`
7272
let mans = await glob(f)
@@ -90,16 +90,7 @@ class Help extends BaseCommand {
9090
}
9191

9292
helpSearch (args) {
93-
return new Promise((resolve, reject) => {
94-
this.npm.commands['help-search'](args, (err) => {
95-
// This would only error if args was empty, which it never is
96-
/* istanbul ignore next */
97-
if (err)
98-
return reject(err)
99-
100-
resolve()
101-
})
102-
})
93+
return this.npm.exec('help-search', args)
10394
}
10495

10596
async viewMan (man) {
@@ -157,7 +148,7 @@ class Help extends BaseCommand {
157148
sect = 'using-npm'
158149
break
159150
}
160-
return 'file://' + path.resolve(__dirname, '..', 'docs', 'output', sect, f + '.html')
151+
return 'file:///' + path.resolve(__dirname, '..', '..', 'docs', 'output', sect, f + '.html')
161152
}
162153
}
163154
module.exports = Help

test/lib/commands/edit.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ const npm = {
2929
get: () => EDITOR,
3030
},
3131
dir: resolve(__dirname, '../../../node_modules'),
32-
commands: {
33-
rebuild: (args, cb) => {
34-
rebuildArgs = args
35-
return cb(rebuildFail)
36-
},
32+
exec: async (cmd, args) => {
33+
rebuildArgs = args
34+
if (rebuildFail)
35+
throw rebuildFail
3736
},
3837
}
3938

test/lib/commands/help-search.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ const npm = mockNpm({
1919
long: false,
2020
},
2121
usage: 'npm test usage',
22-
commands: {
23-
help: (args, cb) => {
24-
return cb(npmHelpErr)
25-
},
22+
exec: async () => {
23+
if (npmHelpErr)
24+
throw npmHelpErr
2625
},
2726
output,
2827
})

test/lib/commands/help.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ const npm = {
2020
cooked: [],
2121
},
2222
},
23-
commands: {
24-
'help-search': (args, cb) => {
23+
exec: async (cmd, args) => {
24+
if (cmd === 'help-search')
2525
helpSearchArgs = args
26-
return cb()
27-
},
28-
help: {
29-
usage: 'npm help <term>',
30-
},
26+
else if (cmd === 'help')
27+
return { usage: 'npm help <term>' }
3128
},
3229
deref: (cmd) => {},
3330
output: msg => {
@@ -162,6 +159,7 @@ t.test('npm help 1 install', async t => {
162159
await help.exec(['1', 'install'])
163160

164161
t.match(openUrlArg, /commands(\/|\\)npm-install.html$/, 'attempts to open the correct url')
162+
t.ok(openUrlArg.startsWith('file:///'), 'opens with the correct uri schema')
165163
})
166164

167165
t.test('npm help 5 install', async t => {

0 commit comments

Comments
 (0)