Skip to content
This repository was archived by the owner on Feb 5, 2022. It is now read-only.

Commit 3ad34b0

Browse files
committed
feat(cli): upgrade to CAC 6
1 parent dcdb7b3 commit 3ad34b0

File tree

3 files changed

+53
-77
lines changed

3 files changed

+53
-77
lines changed

bin/cli.js

Lines changed: 48 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,61 @@
11
#!/usr/bin/env node
2-
const cac = require('cac').default
3-
const SAOError = require('../lib/SAOError')
2+
const cac = require('cac')
3+
const pkg = require('../package')
44

55
const cli = cac()
66

77
cli
8-
.command(
9-
'*',
10-
{
11-
desc: 'Run a generator',
12-
alias: 'run'
13-
},
14-
(input, flags) => {
15-
const options = Object.assign(
16-
{
17-
generator: input[0],
18-
outDir: input[1] || '.',
19-
updateCheck: true
20-
},
21-
flags
22-
)
23-
24-
if (!options.generator) {
25-
return cli.showHelp()
26-
}
27-
28-
return require('../')(options).run()
8+
.command('<generator> [outDir]', 'Run a generator')
9+
.action((generator, outDir, flags) => {
10+
const options = Object.assign(
11+
{
12+
generator,
13+
outDir: outDir || '.',
14+
updateCheck: true
15+
},
16+
flags
17+
)
18+
19+
if (!options.generator) {
20+
return cli.showHelp()
2921
}
30-
)
31-
.option('npm-client', {
32-
desc: `Use a specific npm client ('yarn' or 'npm')`,
33-
type: 'string'
34-
})
35-
.option('update', {
36-
desc: 'Update cached generator',
37-
type: 'boolean',
38-
alias: 'u'
39-
})
40-
.option('clone', {
41-
desc: 'Clone repository instead of archive download',
42-
type: 'boolean',
43-
alias: 'c'
44-
})
45-
.option('yes', {
46-
desc: 'Use the default options',
47-
alias: 'y'
48-
})
49-
.option('registry', {
50-
desc: 'Use a custom registry for npm and yarn',
51-
type: 'string'
52-
})
5322

54-
cli.command('set-alias', 'Set an alias for a generator path', input => {
55-
const store = require('../lib/store')
56-
const { escapeDots } = require('../lib/utils/common')
57-
const logger = require('../lib/logger')
58-
59-
const name = input[0]
60-
const value = input[1]
61-
if (!name || !value) {
62-
throw new SAOError(`Invalid arguments: sao set-alias <alias> <generator>`)
63-
}
23+
return require('../')(options)
24+
.run()
25+
.catch(err => {
26+
require('..').handleError(err)
27+
})
28+
})
29+
.option(
30+
'--npm-client <client>',
31+
`Use a specific npm client ('yarn' or 'npm')`
32+
)
33+
.option('-u, --update', 'Update cached generator')
34+
.option('-c, --clone', 'Clone repository instead of archive download')
35+
.option('-y, --yes', 'Use the default options')
36+
.option('--registry <registry>', 'Use a custom registry for npm and yarn')
6437

65-
store.set(`alias.${escapeDots(name)}`, value)
66-
logger.success(`Added alias '${name}'`)
67-
})
38+
cli
39+
.command('set-alias <name> <value>', 'Set an alias for a generator path')
40+
.action((name, value) => {
41+
const store = require('../lib/store')
42+
const { escapeDots } = require('../lib/utils/common')
43+
const logger = require('../lib/logger')
44+
45+
store.set(`alias.${escapeDots(name)}`, value)
46+
logger.success(`Added alias '${name}'`)
47+
})
6848

69-
cli.command('get-alias', 'Get the generator for an alias', input => {
70-
const store = require('../lib/store')
71-
const { escapeDots } = require('../lib/utils/common')
49+
cli
50+
.command('get-alias <name>', 'Get the generator for an alias')
51+
.action(name => {
52+
const store = require('../lib/store')
53+
const { escapeDots } = require('../lib/utils/common')
7254

73-
console.log(store.get(`alias.${escapeDots(input[0])}`))
74-
})
55+
console.log(store.get(`alias.${escapeDots(name)}`))
56+
})
7557

76-
cli.on('error', error => {
77-
return require('..').handleError(error)
78-
})
58+
cli.version(pkg.version)
59+
cli.help()
7960

8061
cli.parse()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"author": "egoist <[email protected]>",
2121
"license": "MIT",
2222
"dependencies": {
23-
"cac": "^5.0.15",
23+
"cac": "^6.0.1",
2424
"chalk": "^2.4.1",
2525
"conf": "^2.0.0",
2626
"cross-spawn": "^6.0.5",

yarn.lock

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,17 +1112,12 @@ byte-size@^4.0.3:
11121112
resolved "https://registry.npmjs.org/byte-size/-/byte-size-4.0.4.tgz#29d381709f41aae0d89c631f1c81aec88cd40b23"
11131113
integrity sha512-82RPeneC6nqCdSwCX2hZUz3JPOvN5at/nTEw/CMf05Smu3Hrpo9Psb7LjN+k+XndNArG1EY8L4+BM3aTM4BCvw==
11141114

1115-
cac@^5.0.15:
1116-
version "5.0.15"
1117-
resolved "https://registry.npmjs.org/cac/-/cac-5.0.15.tgz#01fb2de24d8fc81050e4ec6ee1fe2b81003431ad"
1118-
integrity sha512-8jTquyS9whajPHyT34giTxj3dUtim/+g01/3A7nJ2YPlat9O5SjsQ0hynF3lZp0+Pe7f9dh571jyDYBM9gVFdw==
1115+
cac@^6.0.1:
1116+
version "6.0.1"
1117+
resolved "https://registry.npmjs.org/cac/-/cac-6.0.1.tgz#a0de49a3d64190534fefd907d214622edff30249"
1118+
integrity sha512-9pYdRwP5buE/OHLYF/RUT6iLEbQsBstI36uiVPf9jfiOokH5LyT7UZxIJvzhMnhHdEe9/FlypZzh1cbQAXj20A==
11191119
dependencies:
1120-
chalk "^2.4.1"
1121-
joycon "^2.1.2"
11221120
minimost "^1.2.0"
1123-
redent "^2.0.0"
1124-
string-width "^2.1.1"
1125-
text-table "^0.2.0"
11261121

11271122
cacache@^10.0.4:
11281123
version "10.0.4"

0 commit comments

Comments
 (0)