Skip to content

Commit c76f04a

Browse files
Yash-Singh1wraithgar
authored andcommitted
fix(set-script): add completion
PR-URL: #2925 Credit: @Yash-Singh1 Close: #2925 Reviewed-by: @ruyadorno
1 parent 877b4ed commit c76f04a

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/set-script.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const log = require('npmlog')
22
const fs = require('fs')
33
const parseJSON = require('json-parse-even-better-errors')
44
const rpj = require('read-package-json-fast')
5+
const { resolve } = require('path')
56

67
const BaseCommand = require('./base-command.js')
78
class SetScript extends BaseCommand {
@@ -20,6 +21,16 @@ class SetScript extends BaseCommand {
2021
return ['[<script>] [<command>]']
2122
}
2223

24+
async completion (opts) {
25+
const argv = opts.conf.argv.remain
26+
if (argv.length === 2) {
27+
// find the script name
28+
const json = resolve(this.npm.localPrefix, 'package.json')
29+
const { scripts = {} } = await rpj(json).catch(er => ({}))
30+
return Object.keys(scripts)
31+
}
32+
}
33+
2334
exec (args, cb) {
2435
this.set(args).then(() => cb()).catch(cb)
2536
}

test/lib/set-script.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,43 @@ const test = require('tap')
22
const requireInject = require('require-inject')
33
const parseJSON = require('json-parse-even-better-errors')
44

5+
test.test('completion', t => {
6+
const SetScript = requireInject('../../lib/set-script.js')
7+
const emptyDir = t.testdir()
8+
t.test('already have a script name', async t => {
9+
const setScript = new SetScript({localPrefix: emptyDir})
10+
const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run', 'x']}}})
11+
t.equal(res, undefined)
12+
t.end()
13+
})
14+
t.test('no package.json', async t => {
15+
const setScript = new SetScript({localPrefix: emptyDir})
16+
const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run']}}})
17+
t.strictSame(res, [])
18+
t.end()
19+
})
20+
t.test('has package.json, no scripts', async t => {
21+
const localPrefix = t.testdir({
22+
'package.json': JSON.stringify({}),
23+
})
24+
const setScript = new SetScript({localPrefix})
25+
const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run']}}})
26+
t.strictSame(res, [])
27+
t.end()
28+
})
29+
t.test('has package.json, with scripts', async t => {
30+
const localPrefix = t.testdir({
31+
'package.json': JSON.stringify({
32+
scripts: { hello: 'echo hello', world: 'echo world' },
33+
}),
34+
})
35+
const setScript = new SetScript({localPrefix})
36+
const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run']}}})
37+
t.strictSame(res, ['hello', 'world'])
38+
t.end()
39+
})
40+
t.end()
41+
})
542
test.test('fails on invalid arguments', (t) => {
643
const SetScript = requireInject('../../lib/set-script.js', {
744
npmlog: {},

0 commit comments

Comments
 (0)