infra-cli
Version:
95 lines (86 loc) • 3.21 kB
JavaScript
const { Command } = require('commander')
const chalk = require('chalk')
const envInfo = require('envinfo')
const packageJson = require('../package.json')
const create = require('../lib/create')
const upgrade = require('../lib/upgrade')
// let projectName = ''
const spaces = ' '
const program = new Command(packageJson.name)
program
.version(packageJson.version, '-v, --version')
.command('create <name>')
.description('create a project through customization.')
.option('-f, --force', 'overwrite target directory if it exists.')
.option('-t, --template [templateName]', 'generate a project from a remote template.')
.option('-be, --template-be [templateName]', 'generate a backend project from a remote template.')
.option('--cache [cacheName]', 'generate a project from a local cache.')
.option('-c, --clone <git-url>', 'Use git clone when fetching remote template.')
.allowUnknownOption()
.action((name, options) => {
create(name, options)
})
.on('--help', () => {
console.log()
console.log(`Supplementary notes on the ${chalk.blue('create')} command:\n`)
console.log(`${spaces}${chalk.green('<name>')} is required.\n`)
console.log()
console.log(`${spaces}${chalk.green.cyan('--template')} can be one of:`)
console.log(
`${spaces}${spaces}- If you don’t enter the template name, all remote templates will be pulled for the user to choose.`
)
console.log(`${spaces}${spaces}- If you enter the template name, it will automatically match the remote template.`)
console.log()
console.log(`${spaces}${chalk.green.cyan('--cache')} can be one of:`)
console.log(
`${spaces}${spaces}- If you don’t enter the cache name, all local caches will be pulled for the user to choose.`
)
console.log(`${spaces}${spaces}- If you enter the cache name, it will automatically match the local cache.`)
console.log(`${spaces}${chalk.green.cyan('--clone')}:`)
console.log(`${spaces}${spaces}- Pull the template from git and generate the project.`)
})
program.on('command:*', ([cmd]) => {
program.outputHelp()
console.log()
console.log(spaces + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`))
console.log()
process.exitCode = 1
})
program
.command('info')
.description('print debugging information about your environment.')
.action((cmd) => {
console.log(chalk.blue.bold('\nEnvironment Info:'))
envInfo
.run(
{
System: ['OS', 'CPU'],
Binaries: ['Node', 'Yarn', 'npm'],
Browsers: ['Chrome', 'Edge', 'Firefox', 'Safari'],
npmPackages: '/**/{typescript,*react*}'
},
{
showNotFound: true,
duplicates: true,
fullTree: true
}
)
.then(console.log)
})
program.on('--help', () => {
console.log()
console.log(` Run ${chalk.cyan('infra <command> --help')} for detailed usage of given command.`)
console.log()
})
// TODO upgrade
program
.command('upgrade')
.description('upgrade infra cli.')
.action((cmd) => {
let { name, version } = packageJson
name = name.trim()
version = version.trim()
upgrade(name, version)
})
program.parse()