@controlla/cli
Version:
Command line interface for rapid Controlla projects development
204 lines (181 loc) • 6.76 kB
JavaScript
const chalk = require('chalk')
const semver = require('semver')
const gitconfig = require('gitconfig')
const krnos = require('@krnos/kronos')
const loadCommand = require('../lib/util/loadCommand')
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.`
))
}
async function checkGitCredentials () {
var email
await gitconfig.get({
location: 'global'
}).then((config) => {
email = config.user.email
}).catch(() => {
console.log(chalk.red(
`Controlla CLI\n` +
`You are not set your account's identity\n` +
` git config --global user.name "Mona Lisa"\n` +
` git config --global user.email "you@example.com"`
))
process.exit(1)
})
const hasPermission = await krnos.checkHasPermissions(email)
if (!hasPermission) {
console.log(chalk.red(
`You do not have permission to use Controlla CLI\n` +
`Are you logged in as the correct user?\n`
))
process.exit(1)
}
}
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')
.option('-t, --testing', 'Overwrite target directory if it exists')
.action((name, options) => {
if (!process.argv.includes('-t') && !process.argv.includes('--testing')) {
checkGitCredentials()
}
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('component <component-name>')
.description('create a new component powered by controlla-service')
.action((name, options) => {
checkGitCredentials()
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 component\'s name, the rest are ignored.'))
}
require('../lib/component')(name, options)
})
program
.command('view <view-name>')
.description('create a new view powered by controlla-service')
.action((name, options) => {
checkGitCredentials()
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 view\'s name, the rest are ignored.'))
}
require('../lib/view')(name, options)
})
program
.command('start')
.description('install all dependencies for exist project')
.option('-m, --packageManager <command>', 'Use specified npm client when installing dependencies')
.action((options) => {
checkGitCredentials()
require('../lib/start')(options)
})
program
.command('release')
.description('generate release for exist project')
.option('-f, --force', 'Generate release without update version')
.option('-m, --packageManager <command>', 'Use specified npm client when installing dependencies')
.action((options) => {
if (!process.argv.includes('-f') && !process.argv.includes('--force')) {
checkGitCredentials()
}
require('../lib/release')(options)
})
program
.command('deploy')
.description('deploying for exist project')
.option('-s, --service <command>', 'Use specified aws service to deploy project')
.action((options) => {
checkGitCredentials()
require('../lib/deploy')(options)
})
program
.command('aws <aws-enviroment>')
.description('manage aws platform')
.option('-ws --service <command>', 'Use specified aws service to manage')
.action((name, options) => {
checkGitCredentials()
require('../lib/aws')(name, options)
})
program
.command('serve [entry]')
.description('serve a .js or .vue file in development mode with zero config')
.option('-o, --open', 'Open browser')
.option('-c, --copy', 'Copy local url to clipboard')
.option('-p, --port <port>', 'Port used by the server (default: 8080 or next available port)')
.option('-h, --hot <command>', 'Use for hot reload')
.action((entry, options) => {
checkGitCredentials()
loadCommand('serve', '@vue/cli-service-global').serve(entry, options)
})
program
.command('info')
.description('print debugging information about your environment')
.action((options) => {
checkGitCredentials()
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()
}