命令行
- commander (v2.9.0)
const program = require('commander')
program
.usage('<template-name> [project-name]') //示例
.option('-c, --clone', 'use git clone') //选项
//更详细的选项
(function () {
program.parse(process.argv) //解析参数
if (program.args.length < 1) return program.help() //触发on --help事件
})()
program.on( '--help', () => {
console.log(' Examples:')
console.log()
console.log(chalk.gray(' # create a new project with an official template'))
console.log(' $ vue init webpack my-project')
console.log()
console.log(chalk.gray(' # create a new project straight from a github template'))
console.log(' $ vue init username/repo my-project')
console.log()
} )
命令行着色
- chalk (v2.1.0)
const chalk = require( 'chalk' )
//变成红色
console.log( chalk.red(' # hello world !') );
询问confirm
- inquirer (v6.0.0)
const inquirer = require('inquirer')
inquirer.prompt([{
type: 'confirm', //类型
message: '你确定要这么干下去🐎?',
name: 'answers' //返回的promise的 resolve值
}]).then(answers => {
console.log( answers ) // true | false
}).catch()
版本号
- semver (v5.1.0)
const semver = require('semver');
semver.satisfies( process.version, >=5.0 ) // true | false //当前运行版本是否满足 大于5.0
semver.clean(' =v1.2.3 ') // '1.2.3' 格式化
加载动画
- ora (v1.3.0)
⠴ downloading template 这样子的
const ora = require('ora')
let spinner = ora(' downloading template!! ')
spinner.start() //开始
spinner.stop() //结束
删除文件
- rimraf (v2.5.0)
const rm = require('rimraf')
rm.sync( '/User/hehe') //同步删除该目录 rm -rf
下载模板&&仓库
- download-git-repo (v1.0.1)
const download = require( 'download-git-repo' )
//clone true: git clone 下载 false: http down
download('git地址', '存放的目录', { clone: false }, err => {
console.log('成功')
})
命令行解析参数
- minimist (laster)
node index.js --beep=boop -t -z 12 -n5 foo bar
const argv = require('minimist')(process.argv.slice(2));
console.dir(argv);
// { _: [ 'foo', 'bar' ], beep: 'boop', t: true, z: 12, n: 5 }