Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add completion for set-script #2925

Merged
merged 6 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Audit extends BaseCommand {
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get params () {
return [
'audit-level',
'dry-run',
'force',
'json',
Expand Down
11 changes: 8 additions & 3 deletions lib/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ class CI extends BaseCommand {
}

const where = this.npm.prefix
const arb = new Arborist({ ...this.npm.flatOptions, path: where })
const opts = {
...this.npm.flatOptions,
path: where,
log: this.npm.log,
save: false, // npm ci should never modify the lockfile or package.json
}

const arb = new Arborist(opts)
await Promise.all([
arb.loadVirtual().catch(er => {
log.verbose('loadVirtual', er.stack)
Expand All @@ -55,8 +61,7 @@ class CI extends BaseCommand {
}),
removeNodeModules(where),
])
// npm ci should never modify the lockfile or package.json
await arb.reify({ ...this.npm.flatOptions, save: false })
await arb.reify(opts)

const ignoreScripts = this.npm.config.get('ignore-scripts')
// run the same set of scripts that `npm install` runs.
Expand Down
4 changes: 4 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class Config extends BaseCommand {
this.config(args).then(() => cb()).catch(cb)
}

execWorkspaces (args, filters, cb) {
this.exec(args, cb)
}

async config ([action, ...args]) {
this.npm.log.disableProgress()
try {
Expand Down
1 change: 1 addition & 0 deletions lib/dedupe.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Dedupe extends BaseCommand {
const where = this.npm.prefix
const opts = {
...this.npm.flatOptions,
log: this.npm.log,
path: where,
dryRun,
}
Expand Down
12 changes: 10 additions & 2 deletions lib/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ class Exec extends BaseCommand {
if (needInstall) {
const installDir = this.cacheInstallDir(packages)
await mkdirp(installDir)
const arb = new Arborist({ ...this.npm.flatOptions, path: installDir })
const arb = new Arborist({
...this.npm.flatOptions,
log: this.npm.log,
path: installDir,
})
const tree = await arb.loadActual()

// at this point, we have to ensure that we get the exact same
Expand Down Expand Up @@ -212,7 +216,11 @@ class Exec extends BaseCommand {
throw new Error('canceled')
}
}
await arb.reify({ ...this.npm.flatOptions, add })
await arb.reify({
...this.npm.flatOptions,
log: this.npm.log,
add,
})
}
pathArr.unshift(resolve(installDir, 'node_modules/.bin'))
}
Expand Down
13 changes: 7 additions & 6 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,16 @@ class Install extends BaseCommand {
if (this.npm.config.get('dev'))
log.warn('install', 'Usage of the `--dev` option is deprecated. Use `--include=dev` instead.')

const arb = new Arborist({
const opts = {
...this.npm.flatOptions,
log: this.npm.log,
auditLevel: null,
path: where,
})

await arb.reify({
...this.npm.flatOptions,
add: args,
})
}
const arb = new Arborist(opts)
await arb.reify(opts)

if (!args.length && !isGlobalInstall && !ignoreScripts) {
const scriptShell = this.npm.config.get('script-shell') || undefined
const scripts = [
Expand Down
9 changes: 8 additions & 1 deletion lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Link extends BaseCommand {
const globalOpts = {
...this.npm.flatOptions,
path: globalTop,
log: this.npm.log,
global: true,
prune: false,
}
Expand Down Expand Up @@ -113,12 +114,14 @@ class Link extends BaseCommand {
// reify all the pending names as symlinks there
const localArb = new Arborist({
...this.npm.flatOptions,
log: this.npm.log,
path: this.npm.prefix,
save,
})
await localArb.reify({
...this.npm.flatOptions,
path: this.npm.prefix,
log: this.npm.log,
add: names.map(l => `file:${resolve(globalTop, 'node_modules', l)}`),
save,
})
Expand All @@ -131,9 +134,13 @@ class Link extends BaseCommand {
const arb = new Arborist({
...this.npm.flatOptions,
path: globalTop,
log: this.npm.log,
global: true,
})
await arb.reify({ add: [`file:${this.npm.prefix}`] })
await arb.reify({
add: [`file:${this.npm.prefix}`],
log: this.npm.log,
})
await reifyFinish(this.npm, arb)
}

Expand Down
8 changes: 5 additions & 3 deletions lib/prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ class Prune extends BaseCommand {

async prune () {
const where = this.npm.prefix
const arb = new Arborist({
const opts = {
...this.npm.flatOptions,
path: where,
})
await arb.prune(this.npm.flatOptions)
log: this.npm.log,
}
const arb = new Arborist(opts)
await arb.prune(opts)
await reifyFinish(this.npm, arb)
}
}
Expand Down
11 changes: 11 additions & 0 deletions lib/set-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const log = require('npmlog')
const fs = require('fs')
const parseJSON = require('json-parse-even-better-errors')
const rpj = require('read-package-json-fast')
const { resolve } = require('path')

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

async completion (opts) {
const argv = opts.conf.argv.remain
if (argv.length === 2) {
// find the script name
const json = resolve(this.npm.localPrefix, 'package.json')
const { scripts = {} } = await rpj(json).catch(er => ({}))
return Object.keys(scripts)
}
}

exec (args, cb) {
this.set(args).then(() => cb()).catch(cb)
}
Expand Down
11 changes: 7 additions & 4 deletions lib/uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ class Uninstall extends BaseCommand {
}
}

const arb = new Arborist({ ...this.npm.flatOptions, path })

await arb.reify({
const opts = {
...this.npm.flatOptions,
path,
log: this.npm.log,
rm: args,
})

}
const arb = new Arborist(opts)
await arb.reify(opts)
await reifyFinish(this.npm, arb)
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Update extends BaseCommand {

const arb = new Arborist({
...this.npm.flatOptions,
log: this.npm.log,
path: where,
})

Expand Down
3 changes: 2 additions & 1 deletion lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ define('_auth', {
is safer to use a registry-provided authentication bearer token stored in
the ~/.npmrc file by running \`npm login\`.
`,
flatten,
})

define('access', {
Expand Down Expand Up @@ -220,7 +221,7 @@ define('audit', {

define('audit-level', {
default: null,
type: ['low', 'moderate', 'high', 'critical', 'none', null],
type: ['info', 'low', 'moderate', 'high', 'critical', 'none', null],
description: `
The minimum level of vulnerability for \`npm audit\` to exit with
a non-zero exit code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ registry and all registries configured for scopes. See the documentation for
#### \`audit-level\`

* Default: null
* Type: "low", "moderate", "high", "critical", "none", or null
* Type: "info", "low", "moderate", "high", "critical", "none", or null

The minimum level of vulnerability for \`npm audit\` to exit with a non-zero
exit code.
Expand Down
2 changes: 1 addition & 1 deletion tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ All commands:
npm audit [fix]

Options:
[--dry-run] [-f|--force] [--json] [--package-lock-only] [--production]
[--audit-level <info|low|moderate|high|critical|none>] [--dry-run] [-f|--force] [--json] [--package-lock-only] [--production]

Run "npm help audit" for more info

Expand Down
7 changes: 7 additions & 0 deletions test/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ t.test('config no args', t => {
})
})

t.test('config ignores workspaces', t => {
config.execWorkspaces([], [], (err) => {
t.match(err, /usage instructions/, 'should not error out when workspaces are defined')
t.end()
})
})

t.test('config list', t => {
t.plan(2)

Expand Down
6 changes: 4 additions & 2 deletions test/lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('should install using Arborist', (t) => {

const npm = mockNpm({
config: { dev: true },
flatOptions: { global: false },
flatOptions: { global: false, auditLevel: 'low' },
globalDir: 'path/to/node_modules/',
prefix: 'foo',
})
Expand All @@ -42,7 +42,9 @@ test('should install using Arborist', (t) => {
install.exec(['fizzbuzz'], er => {
if (er)
throw er
t.match(ARB_ARGS, { global: false, path: 'foo' })
t.match(ARB_ARGS,
{ global: false, path: 'foo', auditLevel: null },
'Arborist gets correct args and ignores auditLevel')
t.equal(REIFY_CALLED, true, 'called reify')
t.strictSame(SCRIPTS, [], 'no scripts when adding dep')
t.end()
Expand Down
37 changes: 37 additions & 0 deletions test/lib/set-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,43 @@ const test = require('tap')
const requireInject = require('require-inject')
const parseJSON = require('json-parse-even-better-errors')

test.test('completion', t => {
const SetScript = requireInject('../../lib/set-script.js')
const emptyDir = t.testdir()
t.test('already have a script name', async t => {
const setScript = new SetScript({localPrefix: emptyDir})
const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run', 'x']}}})
t.equal(res, undefined)
t.end()
})
t.test('no package.json', async t => {
const setScript = new SetScript({localPrefix: emptyDir})
const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run']}}})
t.strictSame(res, [])
t.end()
})
t.test('has package.json, no scripts', async t => {
const localPrefix = t.testdir({
'package.json': JSON.stringify({}),
})
const setScript = new SetScript({localPrefix})
const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run']}}})
t.strictSame(res, [])
t.end()
})
t.test('has package.json, with scripts', async t => {
const localPrefix = t.testdir({
'package.json': JSON.stringify({
scripts: { hello: 'echo hello', world: 'echo world' },
}),
})
const setScript = new SetScript({localPrefix})
const res = await setScript.completion({conf: {argv: {remain: ['npm', 'run']}}})
t.strictSame(res, ['hello', 'world'])
t.end()
})
t.end()
})
test.test('fails on invalid arguments', (t) => {
const SetScript = requireInject('../../lib/set-script.js', {
npmlog: {},
Expand Down
7 changes: 4 additions & 3 deletions test/lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const config = {
const noop = () => null
const npm = mockNpm({
globalDir: '',
log: noop,
config,
prefix: '',
})
Expand Down Expand Up @@ -38,7 +39,7 @@ t.test('no args', t => {
constructor (args) {
t.deepEqual(
args,
{ ...npm.flatOptions, path: npm.prefix },
{ ...npm.flatOptions, path: npm.prefix, log: noop },
'should call arborist contructor with expected args'
)
}
Expand Down Expand Up @@ -72,7 +73,7 @@ t.test('with args', t => {
constructor (args) {
t.deepEqual(
args,
{ ...npm.flatOptions, path: npm.prefix },
{ ...npm.flatOptions, path: npm.prefix, log: noop },
'should call arborist contructor with expected args'
)
}
Expand Down Expand Up @@ -140,7 +141,7 @@ t.test('update --global', t => {
const { path, ...opts } = args
t.deepEqual(
opts,
npm.flatOptions,
{ ...npm.flatOptions, log: noop },
'should call arborist contructor with expected options'
)

Expand Down