前置知识点 ¶
commander ¶
commander
这是用来编写指令和处理命令行的,具体用法如下:
const program = require("commander"); // 定义指令 program .version('0.0.1') .command('init', 'Generate a new project from a template') .action(() => { // 回调函数 }) // 解析命令行参数 program.parse(process.argv);
复制成功
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
替代方案
yargs
inquirer ¶
inquirer
这是个强大的交互式命令行工具,具体用法如下:
const inquirer = require('inquirer'); inquirer .prompt([ // 一些交互式的问题 ]) .then(answers => { // 回调函数,answers 就是用户输入的内容,是个对象 });
复制成功
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
想象一下我们用 vue init webpack project-name 之后是不是会有几个交互问题,问你文件名啊、作者啊、描述啊、要不要用 eslint 啊等等之类的,就是用这个来写的。
chalk ¶
chalk
这是用来修改控制台输出内容样式的,比如颜色啊,具体用法如下:
const chalk = require('chalk'); console.log(chalk.green('success')); console.log(chalk.red('error'));
复制成功
1
2
3
2
3
ora ¶
ora
这是一个好看的加载,就是你下载的时候会有个转圈圈的那种效果,用法如下:
const ora = require('ora') let spinner = ora('downloading template ...') spinner.start()
复制成功
1
2
3
2
3
download-git-repo ¶
download-git-repo
看名字很明显了,这是用来下载远程模板的,支持 GitHub、 GitLab 和 Bitbucket 等,用法如下:
const download = require('download-git-repo') download(repository, destination, options, callback)
复制成功
1
2
3
2
3
其中 repository
是远程仓库地址;destination
是存放下载的文件路径,也可以直接写文件名,默认就是当前目录;options
是一些选项,比如 { clone:boolean }
表示用 http download 还是 git clone
的形式下载。
yeoman ¶
待續