Skip to content

Commit 2b0c92f

Browse files
committed
fix(usage): clean up usage declarations
Small refactor of commands to allow usage to be more programmatically generated, leading us in the direction of more tighly coupling each command to the params it accepts.
1 parent 8806015 commit 2b0c92f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1023
-759
lines changed

lib/access.js

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const readPackageJson = require('read-package-json-fast')
55

66
const output = require('./utils/output.js')
77
const otplease = require('./utils/otplease.js')
8-
const usageUtil = require('./utils/usage.js')
98
const getIdentity = require('./utils/get-identity.js')
9+
const BaseCommand = require('./base-command.js')
1010

1111
const subcommands = [
1212
'public',
@@ -20,24 +20,23 @@ const subcommands = [
2020
'2fa-not-required',
2121
]
2222

23-
class Access {
24-
constructor (npm) {
25-
this.npm = npm
23+
class Access extends BaseCommand {
24+
static get name () {
25+
return 'access'
2626
}
2727

28-
get usage () {
29-
return usageUtil(
30-
'access',
31-
'npm access public [<package>]\n' +
32-
'npm access restricted [<package>]\n' +
33-
'npm access grant <read-only|read-write> <scope:team> [<package>]\n' +
34-
'npm access revoke <scope:team> [<package>]\n' +
35-
'npm access 2fa-required [<package>]\n' +
36-
'npm access 2fa-not-required [<package>]\n' +
37-
'npm access ls-packages [<user>|<scope>|<scope:team>]\n' +
38-
'npm access ls-collaborators [<package> [<user>]]\n' +
39-
'npm access edit [<package>]'
40-
)
28+
static get usage () {
29+
return [
30+
'public [<package>]',
31+
'restricted [<package>]',
32+
'grant <read-only|read-write> <scope:team> [<package>]',
33+
'revoke <scope:team> [<package>]',
34+
'2fa-required [<package>]',
35+
'2fa-not-required [<package>]',
36+
'ls-packages [<user>|<scope>|<scope:team>]',
37+
'ls-collaborators [<package> [<user>]]',
38+
'edit [<package>]',
39+
]
4140
}
4241

4342
async completion (opts) {
@@ -67,12 +66,7 @@ class Access {
6766
}
6867

6968
exec (args, cb) {
70-
this.access(args)
71-
.then(x => cb(null, x))
72-
.catch(err => err.code === 'EUSAGE'
73-
? cb(err.message)
74-
: cb(err)
75-
)
69+
this.access(args).then(() => cb()).catch(cb)
7670
}
7771

7872
async access ([cmd, ...args]) {
@@ -203,12 +197,6 @@ class Access {
203197
return name
204198
}
205199
}
206-
207-
usageError (msg) {
208-
return Object.assign(new Error(`\nUsage: ${msg}\n\n` + this.usage), {
209-
code: 'EUSAGE',
210-
})
211-
}
212200
}
213201

214202
module.exports = Access

lib/adduser.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
const log = require('npmlog')
22
const output = require('./utils/output.js')
3-
const usageUtil = require('./utils/usage.js')
43
const replaceInfo = require('./utils/replace-info.js')
4+
const BaseCommand = require('./base-command.js')
55
const authTypes = {
66
legacy: require('./auth/legacy.js'),
77
oauth: require('./auth/oauth.js'),
88
saml: require('./auth/saml.js'),
99
sso: require('./auth/sso.js'),
1010
}
1111

12-
class AddUser {
13-
constructor (npm) {
14-
this.npm = npm
12+
class AddUser extends BaseCommand {
13+
static get name () {
14+
return 'adduser'
1515
}
1616

17-
/* istanbul ignore next - see test/lib/load-all-commands.js */
18-
get usage () {
19-
return usageUtil(
20-
'adduser',
21-
'npm adduser [--registry=url] [--scope=@orgname] [--always-auth]'
22-
)
17+
static get usage () {
18+
return ['[--registry=url] [--scope=@orgname] [--always-auth]']
2319
}
2420

2521
exec (args, cb) {

lib/audit.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ const auditReport = require('npm-audit-report')
33
const output = require('./utils/output.js')
44
const reifyFinish = require('./utils/reify-finish.js')
55
const auditError = require('./utils/audit-error.js')
6-
const usageUtil = require('./utils/usage.js')
6+
const BaseCommand = require('./base-command.js')
77

8-
class Audit {
9-
constructor (npm) {
10-
this.npm = npm
8+
class Audit extends BaseCommand {
9+
/* istanbul ignore next - see test/lib/load-all-commands.js */
10+
static get name () {
11+
return 'audit'
1112
}
1213

1314
/* istanbul ignore next - see test/lib/load-all-commands.js */
14-
get usage () {
15-
return usageUtil(
16-
'audit',
17-
'npm audit [--json] [--production]' +
18-
'\nnpm audit fix ' +
19-
'[--force|--package-lock-only|--dry-run|--production|--only=(dev|prod)]'
20-
)
15+
static get usage () {
16+
return [
17+
'[--json] [--production]',
18+
'fix [--force|--package-lock-only|--dry-run|--production|--only=(dev|prod)]',
19+
]
2120
}
2221

2322
async completion (opts) {

lib/base-command.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Base class for npm.commands[cmd]
2+
const usageUtil = require('./utils/usage.js')
3+
4+
class BaseCommand {
5+
constructor (npm) {
6+
this.npm = npm
7+
}
8+
9+
get usage () {
10+
let usage = `npm ${this.constructor.name}\n\n`
11+
if (this.constructor.description)
12+
usage = `${usage}${this.constructor.description}\n\n`
13+
14+
usage = `${usage}Usage:\n`
15+
if (!this.constructor.usage)
16+
usage = `${usage}npm ${this.constructor.name}`
17+
else
18+
usage = `${usage}${this.constructor.usage.map(u => `npm ${this.constructor.name} ${u}`).join('\n')}`
19+
20+
// Mostly this just appends aliases, this could be more clear
21+
usage = usageUtil(this.constructor.name, usage)
22+
usage = `${usage}\n\nRun "npm ${this.constructor.name} help" for more info`
23+
return usage
24+
}
25+
26+
usageError (msg) {
27+
if (!msg) {
28+
return Object.assign(new Error(`\nUsage: ${this.usage}`), {
29+
code: 'EUSAGE',
30+
})
31+
}
32+
33+
return Object.assign(new Error(`\nUsage: ${msg}\n\n${this.usage}`), {
34+
code: 'EUSAGE',
35+
})
36+
}
37+
}
38+
module.exports = BaseCommand

lib/bin.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
const output = require('./utils/output.js')
22
const envPath = require('./utils/path.js')
3-
const usageUtil = require('./utils/usage.js')
3+
const BaseCommand = require('./base-command.js')
44

5-
class Bin {
6-
constructor (npm) {
7-
this.npm = npm
5+
class Bin extends BaseCommand {
6+
static get name () {
7+
return 'bin'
88
}
99

10-
/* istanbul ignore next - see test/lib/load-all-commands.js */
11-
get usage () {
12-
return usageUtil('bin', 'npm bin [-g]')
10+
static get usage () {
11+
return ['[-g]']
1312
}
1413

1514
exec (args, cb) {

lib/bugs.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
const log = require('npmlog')
22
const pacote = require('pacote')
33
const openUrl = require('./utils/open-url.js')
4-
const usageUtil = require('./utils/usage.js')
54
const hostedFromMani = require('./utils/hosted-git-info-from-manifest.js')
5+
const BaseCommand = require('./base-command.js')
66

7-
class Bugs {
8-
constructor (npm) {
9-
this.npm = npm
7+
class Bugs extends BaseCommand {
8+
static get name () {
9+
return 'bugs'
1010
}
1111

12-
/* istanbul ignore next - see test/lib/load-all-commands.js */
13-
get usage () {
14-
return usageUtil('bugs', 'npm bugs [<pkgname>]')
12+
static get usage () {
13+
return ['[<pkgname>]']
1514
}
1615

1716
exec (args, cb) {

lib/cache.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@ const output = require('./utils/output.js')
55
const pacote = require('pacote')
66
const path = require('path')
77
const rimraf = promisify(require('rimraf'))
8+
const BaseCommand = require('./base-command.js')
89

9-
const usageUtil = require('./utils/usage.js')
10-
class Cache {
11-
constructor (npm) {
12-
this.npm = npm
10+
class Cache extends BaseCommand {
11+
/* istanbul ignore next - see test/lib/load-all-commands.js */
12+
static get name () {
13+
return 'cache'
1314
}
1415

15-
get usage () {
16-
return usageUtil('cache',
17-
'npm cache add <tarball file>' +
18-
'\nnpm cache add <folder>' +
19-
'\nnpm cache add <tarball url>' +
20-
'\nnpm cache add <git url>' +
21-
'\nnpm cache add <name>@<version>' +
22-
'\nnpm cache clean' +
23-
'\nnpm cache verify'
24-
)
16+
/* istanbul ignore next - see test/lib/load-all-commands.js */
17+
static get usage () {
18+
return [
19+
'add <tarball file>',
20+
'add <folder>',
21+
'add <tarball url>',
22+
'add <git url>',
23+
'add <name>@<version>',
24+
'clean',
25+
'verify',
26+
]
2527
}
2628

2729
async completion (opts) {

lib/ci.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const fs = require('fs')
77
const readdir = util.promisify(fs.readdir)
88

99
const log = require('npmlog')
10-
const usageUtil = require('./utils/usage.js')
1110

1211
const removeNodeModules = async where => {
1312
const rimrafOpts = { glob: false }
@@ -18,15 +17,12 @@ const removeNodeModules = async where => {
1817
await Promise.all(entries.map(f => rimraf(`${path}/${f}`, rimrafOpts)))
1918
process.emit('timeEnd', 'npm-ci:rm')
2019
}
20+
const BaseCommand = require('./base-command.js')
2121

22-
class CI {
23-
constructor (npm) {
24-
this.npm = npm
25-
}
26-
22+
class CI extends BaseCommand {
2723
/* istanbul ignore next - see test/lib/load-all-commands.js */
28-
get usage () {
29-
return usageUtil('ci', 'npm ci')
24+
static get name () {
25+
return 'ci'
3026
}
3127

3228
exec (args, cb) {

lib/completion.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,18 @@ const isWindowsShell = require('./utils/is-windows-shell.js')
4242
const output = require('./utils/output.js')
4343
const fileExists = require('./utils/file-exists.js')
4444

45-
const usageUtil = require('./utils/usage.js')
4645
const { promisify } = require('util')
46+
const BaseCommand = require('./base-command.js')
4747

48-
class Completion {
49-
constructor (npm) {
50-
this.npm = npm
48+
class Completion extends BaseCommand {
49+
/* istanbul ignore next - see test/lib/load-all-commands.js */
50+
static get name () {
51+
return 'completion'
5152
}
5253

5354
/* istanbul ignore next - see test/lib/load-all-commands.js */
54-
get usage () {
55-
return usageUtil('completion', 'source <(npm completion)')
55+
static get description () {
56+
return 'npm command completion script. save to ~/.bashrc or ~/.zshrc'
5657
}
5758

5859
// completion for the completion command

lib/config.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { defaults, types } = require('./utils/config.js')
2-
const usageUtil = require('./utils/usage.js')
32
const output = require('./utils/output.js')
43

54
const mkdirp = require('mkdirp-infer-owner')
@@ -29,22 +28,22 @@ const keyValues = args => {
2928

3029
const publicVar = k => !/^(\/\/[^:]+:)?_/.test(k)
3130

32-
class Config {
33-
constructor (npm) {
34-
this.npm = npm
31+
const BaseCommand = require('./base-command.js')
32+
class Config extends BaseCommand {
33+
/* istanbul ignore next - see test/lib/load-all-commands.js */
34+
static get name () {
35+
return 'config'
3536
}
3637

37-
get usage () {
38-
return usageUtil(
39-
'config',
40-
'npm config set <key>=<value> [<key>=<value> ...]' +
41-
'\nnpm config get [<key> [<key> ...]]' +
42-
'\nnpm config delete <key> [<key> ...]' +
43-
'\nnpm config list [--json]' +
44-
'\nnpm config edit' +
45-
'\nnpm set <key>=<value> [<key>=<value> ...]' +
46-
'\nnpm get [<key> [<key> ...]]'
47-
)
38+
/* istanbul ignore next - see test/lib/load-all-commands.js */
39+
static get usage () {
40+
return [
41+
'set <key>=<value> [<key>=<value> ...]',
42+
'get [<key> [<key> ...]]',
43+
'delete <key> [<key> ...]',
44+
'list [--json]',
45+
'edit',
46+
]
4847
}
4948

5049
async completion (opts) {
@@ -254,10 +253,6 @@ ${defData}
254253
}
255254
output(JSON.stringify(publicConf, null, 2))
256255
}
257-
258-
usageError () {
259-
return Object.assign(new Error(this.usage), { code: 'EUSAGE' })
260-
}
261256
}
262257

263258
module.exports = Config

0 commit comments

Comments
 (0)