@neosjs/cli
Version:
Neos(奈欧斯)是一个帮助开发者快速地创建 Vue3 应用并自动配置项目编译的脚手架
158 lines (137 loc) • 4.15 kB
JavaScript
const program = require('commander')
const minimist = require('minimist')
const chalk = require('chalk')
const semver = require('semver')
const requiredVersion = require('../package.json').engines.node
const { generateTitle } = require('../lib/utils/logger')
const didYouMean = require('didyoumean')
didYouMean.threshold = 0.6
const checkNodeVersion = (wanted, id) => {
if (!semver.satisfies(process.version, wanted)) {
console.log(chalk.red(`你使用的是 Node ${process.version}.\n请升级Node.'`))
process.exit(1)
}
}
checkNodeVersion(requiredVersion, '@neosjs/cli')
if (semver.satisfies(process.version, '9.x')) {
console.log(
chalk.red(
`你使用的是 Node ${process.version}.\n
请升级Node.`
)
)
}
program.version(
`@neosjs/cli ${require('../package.json').version}\n`,
'-v, --version',
'输出版本号'
)
const verifyArgs = name => {
if (minimist(process.argv.slice(3))._.length > 1) {
console.log(chalk.yellow(`\n 警告: 您提供了多个参数。第一个将被用作${name},其余的将被忽略.`))
}
}
// 创建文件
program
.command('create <projectName>')
.description('创建一个新项目')
.option('-f, --force', '如果目标目录存在,则覆盖它')
.option('-g, --git [message]', '使用初始提交消息强制git初始化')
.action((name, options) => {
verifyArgs('Project Name')
if (process.argv.includes('-g') || process.argv.includes('--git')) {
options.forceGit = true
}
require('../lib/create')(name, options)
})
// 打开url
program
.command('open <url>')
.description('打开 url')
.action((name, options) => {
verifyArgs('Url')
require('../lib/open')(name, options)
})
// 生成随机数
program
.command('random <length>')
.description('生成一个随机数')
.action(name => {
verifyArgs('Length')
require('../lib/random')(name)
})
program
.command('info')
.description('打印有关环境的调试信息')
.action(() => {
console.log(chalk.bold('\n环境信息:'))
require('envinfo')
.run(
{
System: ['OS', 'CPU','Shell'],
Binaries: ['Node', 'Yarn', 'npm', 'pnpm'],
Browsers: ['Chrome', 'Edge', 'Firefox', 'Safari'],
IDEs:['VSCode'],
Utilities:['Git'],
npmGlobalPackages: ['@neosjs/cli','pnpm']
},
{
showNotFound: true,
duplicates: true,
fullTree: true,
}
)
.then(env => console.log(env));
})
program.arguments('<command>').action(cmd => {
program.outputHelp()
console.log(' ' + chalk.red(`未知的命令 ${chalk.yellow(cmd)}.`))
console.log()
suggestCommands(cmd)
})
program.commands.forEach(c => c.on('--help', () => console.log()))
const enhanceErrorMessages = (methodName, log) => {
program.Command.prototype[methodName] = function (...args) {
if (methodName === 'unknownOption' && this._allowUnknownOption) {
return
}
this.outputHelp()
console.log(` ${chalk.red(log(...args))}`)
console.log()
process.exit(1)
}
}
enhanceErrorMessages(
'missingArgument',
argName => `缺少所需的参数 ${chalk.yellow(`<${argName}>`)}.`
)
enhanceErrorMessages('unknownOption', optionName => `未知选项 ${chalk.yellow(optionName)}.`)
enhanceErrorMessages(
'optionMissingArgument',
(option, flag) =>
`缺少选项所需的参数 ${chalk.yellow(option.flags)}${flag ? `, got ${chalk.yellow(flag)}` : ''}`
)
program
.configureHelp({
sortSubcommands: true,
subcommandTerm: cmd => cmd.name()
})
.helpOption('-h, --help', '查看帮助信息')
program.on('--help', () => {
console.log(`${generateTitle()}`)
console.log()
})
program.parse(process.argv)
if (!process.argv.slice(2).length) {
program.outputHelp()
}
function suggestCommands(unknownCommand) {
const availableCommands = program.commands.map(cmd => {
return cmd._name
})
const suggestion = didYouMean(unknownCommand, availableCommands)
if (suggestion) {
console.log(' ' + chalk.red(`你的意思是 ${chalk.yellow(suggestion)} ?\n`))
}
}