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

Commit

Permalink
feat(cli): upgrade to CAC 6
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 25, 2018
1 parent dcdb7b3 commit 3ad34b0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 77 deletions.
115 changes: 48 additions & 67 deletions bin/cli.js
@@ -1,80 +1,61 @@
#!/usr/bin/env node
const cac = require('cac').default
const SAOError = require('../lib/SAOError')
const cac = require('cac')
const pkg = require('../package')

const cli = cac()

cli
.command(
'*',
{
desc: 'Run a generator',
alias: 'run'
},
(input, flags) => {
const options = Object.assign(
{
generator: input[0],
outDir: input[1] || '.',
updateCheck: true
},
flags
)

if (!options.generator) {
return cli.showHelp()
}

return require('../')(options).run()
.command('<generator> [outDir]', 'Run a generator')
.action((generator, outDir, flags) => {
const options = Object.assign(
{
generator,
outDir: outDir || '.',
updateCheck: true
},
flags
)

if (!options.generator) {
return cli.showHelp()
}
)
.option('npm-client', {
desc: `Use a specific npm client ('yarn' or 'npm')`,
type: 'string'
})
.option('update', {
desc: 'Update cached generator',
type: 'boolean',
alias: 'u'
})
.option('clone', {
desc: 'Clone repository instead of archive download',
type: 'boolean',
alias: 'c'
})
.option('yes', {
desc: 'Use the default options',
alias: 'y'
})
.option('registry', {
desc: 'Use a custom registry for npm and yarn',
type: 'string'
})

cli.command('set-alias', 'Set an alias for a generator path', input => {
const store = require('../lib/store')
const { escapeDots } = require('../lib/utils/common')
const logger = require('../lib/logger')

const name = input[0]
const value = input[1]
if (!name || !value) {
throw new SAOError(`Invalid arguments: sao set-alias <alias> <generator>`)
}
return require('../')(options)
.run()
.catch(err => {
require('..').handleError(err)
})
})
.option(
'--npm-client <client>',
`Use a specific npm client ('yarn' or 'npm')`
)
.option('-u, --update', 'Update cached generator')
.option('-c, --clone', 'Clone repository instead of archive download')
.option('-y, --yes', 'Use the default options')
.option('--registry <registry>', 'Use a custom registry for npm and yarn')

store.set(`alias.${escapeDots(name)}`, value)
logger.success(`Added alias '${name}'`)
})
cli
.command('set-alias <name> <value>', 'Set an alias for a generator path')
.action((name, value) => {
const store = require('../lib/store')
const { escapeDots } = require('../lib/utils/common')
const logger = require('../lib/logger')

store.set(`alias.${escapeDots(name)}`, value)
logger.success(`Added alias '${name}'`)
})

cli.command('get-alias', 'Get the generator for an alias', input => {
const store = require('../lib/store')
const { escapeDots } = require('../lib/utils/common')
cli
.command('get-alias <name>', 'Get the generator for an alias')
.action(name => {
const store = require('../lib/store')
const { escapeDots } = require('../lib/utils/common')

console.log(store.get(`alias.${escapeDots(input[0])}`))
})
console.log(store.get(`alias.${escapeDots(name)}`))
})

cli.on('error', error => {
return require('..').handleError(error)
})
cli.version(pkg.version)
cli.help()

cli.parse()
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -20,7 +20,7 @@
"author": "egoist <[email protected]>",
"license": "MIT",
"dependencies": {
"cac": "^5.0.15",
"cac": "^6.0.1",
"chalk": "^2.4.1",
"conf": "^2.0.0",
"cross-spawn": "^6.0.5",
Expand Down
13 changes: 4 additions & 9 deletions yarn.lock
Expand Up @@ -1112,17 +1112,12 @@ byte-size@^4.0.3:
resolved "https://registry.npmjs.org/byte-size/-/byte-size-4.0.4.tgz#29d381709f41aae0d89c631f1c81aec88cd40b23"
integrity sha512-82RPeneC6nqCdSwCX2hZUz3JPOvN5at/nTEw/CMf05Smu3Hrpo9Psb7LjN+k+XndNArG1EY8L4+BM3aTM4BCvw==

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

cacache@^10.0.4:
version "10.0.4"
Expand Down

0 comments on commit 3ad34b0

Please sign in to comment.