@controlla/cli
Version:
Command line interface for rapid Controlla projects development
127 lines (108 loc) • 4 kB
JavaScript
const chalk = require('chalk')
const semver = require('semver')
const requiredVersion = require('../package.json').engines.node
function checkNodeVersion (wanted, id) {
if (!semver.satisfies(process.version, wanted)) {
console.log(chalk.red(
'You are using Node ' + process.version + ', but this version of ' + id +
' requires Node ' + wanted + '.\nPlease upgrade your Node version.'
))
process.exit(1)
}
}
checkNodeVersion(requiredVersion, 'controlla-cli')
if (semver.satisfies(process.version, '9.x')) {
console.log(chalk.red(
`You are using Node ${process.version}.\n` +
`Node.js 9.x has already reached end-of-life and will not be supported in future major releases.\n` +
`It's strongly recommended to use an active LTS version instead.`
))
}
const minimist = require('minimist')
const program = require('commander')
program
.version(require('../package').version)
.usage('<command> [options]')
program
.command('new <app-name>')
.description('create a new project powered by controlla-service')
.option('-p, --preset <presetName>', 'Used specified controlla project')
.option('-m, --packageManager <command>', 'Use specified npm client when installing dependencies')
.option('-g, --git [message]', 'Force git initialization with initial commit message')
.option('-n, --no-git', 'Skip git initialization')
.option('-f, --force', 'Overwrite target directory if it exists')
.action((name, cmd) => {
const options = cleanArgs(cmd)
if (minimist(process.argv.slice(3))._.length > 1) {
console.log(chalk.yellow('\n Info: You provided more than one argument. The first one will be used as the app\'s name, the rest are ignored.'))
}
// --git makes commander to default git to true
if (process.argv.includes('-g') || process.argv.includes('--git')) {
options.forceGit = true
}
require('../lib/create')(name, options)
})
program
.command('start')
.description('install all dependencies for exist project')
.option('-m, --packageManager <command>', 'Use specified npm client when installing dependencies')
.action((cmd) => {
require('../lib/start')(cleanArgs(cmd))
})
program
.command('info')
.description('print debugging information about your environment')
.action((cmd) => {
console.log(chalk.bold('\nEnvironment Info:'))
require('envinfo').run(
{
System: ['OS', 'CPU', 'Memory', 'Shell'],
Binaries: ['Node', 'Yarn', 'npm'],
Browsers: ['Chrome', 'Edge', 'Firefox', 'Safari', 'Epiphany'],
npmPackages: '/**/{typescript,*vue*,@vue/*/,@controlla/*/}',
npmGlobalPackages: ['@vue/cli', '@controlla/cli']
},
{
showNotFound: true,
duplicates: true,
fullTree: true
}
).then(console.log)
})
// output help information on unknown commands
program
.arguments('<command>')
.action((cmd) => {
program.outputHelp()
console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`))
console.log()
})
// add some useful info on help
program.on('--help', () => {
console.log()
console.log(` Run ${chalk.cyan(`controlla <command> --help`)} for detailed usage of given command.`)
console.log()
})
program.commands.forEach(c => c.on('--help', () => console.log()))
program.parse(process.argv)
if (!process.argv.slice(2).length) {
program.outputHelp()
}
function camelize (str) {
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '')
}
// commander passes the Command object itself as options,
// extract only actual options into a fresh object.
function cleanArgs (cmd) {
const args = {}
cmd.options.forEach(o => {
const key = camelize(o.long.replace(/^--/, ''))
// if an option is not present and Command has a method with the same name
// it should not be copied
if (typeof cmd[key] !== 'function' && typeof cmd[key] !== 'undefined') {
args[key] = cmd[key]
}
})
return args
}