Skip to content

Commit 2c3c747

Browse files
committed
fix(usage): tie usage to config
This starts us down the path of tying the params our commands accept to their config items. For now it is optional, and not every current config item would cleanly render if we added them today. The ones that are added here DO render nicely, and we can iterate from here. We can also at a later date do the same kind of appraoch with our positional args.
1 parent e94a458 commit 2c3c747

27 files changed

+297
-45
lines changed

docs/content/using-npm/config.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ The following shorthands are parsed on the command-line:
9696
* `-H`: `--usage`
9797
* `--help`: `--usage`
9898
* `-v`: `--version`
99+
* `-w`: `--workspace`
100+
* `--ws`: `--workspaces`
99101
* `-y`: `--yes`
100102

101103
<!-- AUTOGENERATED CONFIG SHORTHANDS END -->
@@ -1311,6 +1313,34 @@ The program to use to view help content.
13111313

13121314
Set to `"browser"` to view html help content in the default web browser.
13131315

1316+
#### `which`
1317+
1318+
* Default: null
1319+
* Type: null or Number
1320+
1321+
If there are multiple funding sources, which 1-indexed source URL to open.
1322+
1323+
#### `workspace`
1324+
1325+
* Default:
1326+
* Type: String (can be set multiple times)
1327+
1328+
Enable running a command in the context of the configured workspaces of the
1329+
current project while filtering by running only the workspaces defined by
1330+
this configuration option.
1331+
1332+
Valid values for the `workspace` config are either: - Workspace names - Path
1333+
to a workspace directory - Path to a parent workspace directory (will result
1334+
to selecting all of the nested workspaces)
1335+
1336+
#### `workspaces`
1337+
1338+
* Default: false
1339+
* Type: Boolean
1340+
1341+
Enable running a command in the context of **all** the configured
1342+
workspaces.
1343+
13141344
#### `yes`
13151345

13161346
* Default: false

lib/adduser.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ class AddUser extends BaseCommand {
1717
return 'adduser'
1818
}
1919

20-
static get usage () {
21-
return ['[--registry=url] [--scope=@orgname] [--always-auth]']
20+
static get params () {
21+
return [
22+
'registry',
23+
'scope',
24+
'always-auth',
25+
]
2226
}
2327

2428
exec (args, cb) {

lib/audit.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ class Audit extends BaseCommand {
1616
}
1717

1818
/* istanbul ignore next - see test/lib/load-all-commands.js */
19-
static get usage () {
19+
static get params () {
2020
return [
21-
'[--json] [--production]',
22-
'fix [--force|--package-lock-only|--dry-run|--production|--only=(dev|prod)]',
21+
'dry-run',
22+
'force',
23+
'json',
24+
'package-lock-only',
25+
'production',
2326
]
2427
}
2528

29+
/* istanbul ignore next - see test/lib/load-all-commands.js */
30+
static get usage () {
31+
return ['[fix]']
32+
}
33+
2634
async completion (opts) {
2735
const argv = opts.conf.argv.remain
2836

lib/base-command.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Base class for npm.commands[cmd]
22
const usageUtil = require('./utils/usage.js')
3+
const ConfigDefinitions = require('./utils/config/definitions.js')
34

45
class BaseCommand {
56
constructor (npm) {
@@ -25,6 +26,9 @@ class BaseCommand {
2526
else
2627
usage = `${usage}${this.constructor.usage.map(u => `npm ${this.constructor.name} ${u}`).join('\n')}`
2728

29+
if (this.constructor.params)
30+
usage = `${usage}\n\nOptions:\n[${this.constructor.params.map(p => ConfigDefinitions[p].usage).join('] [')}]`
31+
2832
// Mostly this just appends aliases, this could be more clear
2933
usage = usageUtil(this.constructor.name, usage)
3034
usage = `${usage}\n\nRun "npm help ${this.constructor.name}" for more info`

lib/bin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class Bin extends BaseCommand {
1010
return 'bin'
1111
}
1212

13-
static get usage () {
14-
return ['[-g]']
13+
static get params () {
14+
return ['global']
1515
}
1616

1717
exec (args, cb) {

lib/fund.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ class Fund extends BaseCommand {
3232
return 'fund'
3333
}
3434

35+
/* istanbul ignore next - see test/lib/load-all-commands.js */
36+
static get params () {
37+
return [
38+
'json',
39+
'browser',
40+
'unicode',
41+
'which',
42+
]
43+
}
44+
3545
/* istanbul ignore next - see test/lib/load-all-commands.js */
3646
static get usage () {
37-
return ['[--json] [--browser] [--unicode] [[<@scope>/]<pkg> [--which=<fundingSourceNumber>]']
47+
return ['[<@scope>/]<pkg>]']
3848
}
3949

4050
/* istanbul ignore next - see test/lib/load-all-commands.js */

lib/install.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ class Install extends BaseCommand {
2121
return 'install'
2222
}
2323

24+
/* istanbul ignore next - see test/lib/load-all-commands.js */
25+
static get params () {
26+
return [
27+
'save',
28+
'save-exact',
29+
]
30+
}
31+
2432
/* istanbul ignore next - see test/lib/load-all-commands.js */
2533
static get usage () {
2634
return [
@@ -33,7 +41,7 @@ class Install extends BaseCommand {
3341
'<tarball file>',
3442
'<tarball url>',
3543
'<git:// url>',
36-
'<github username>/<github project> [--save-prod|--save-dev|--save-optional|--save-peer] [--save-exact] [--no-save]',
44+
'<github username>/<github project>',
3745
]
3846
}
3947

lib/logout.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ class Logout extends BaseCommand {
1515
}
1616

1717
/* istanbul ignore next - see test/lib/load-all-commands.js */
18-
static get usage () {
19-
return ['[--registry=<url>] [--scope=<@scope>]']
18+
static get params () {
19+
return [
20+
'registry',
21+
'scope',
22+
]
2023
}
2124

2225
exec (args, cb) {

lib/pack.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ class Pack extends BaseCommand {
2121
return 'pack'
2222
}
2323

24+
/* istanbul ignore next - see test/lib/load-all-commands.js */
25+
static get params () {
26+
return ['dry-run']
27+
}
28+
2429
/* istanbul ignore next - see test/lib/load-all-commands.js */
2530
static get usage () {
26-
return ['[[<@scope>/]<pkg>...] [--dry-run]']
31+
return ['[<@scope>/]<pkg>...]']
2732
}
2833

2934
exec (args, cb) {

lib/ping.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ class Ping extends BaseCommand {
88
return 'Ping npm registry'
99
}
1010

11+
/* istanbul ignore next - see test/lib/load-all-commands.js */
12+
static get params () {
13+
return ['registry']
14+
}
15+
1116
/* istanbul ignore next - see test/lib/load-all-commands.js */
1217
static get name () {
1318
return 'ping'

0 commit comments

Comments
 (0)