-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (82 loc) · 4.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
const fs = require('fs'); // 文件模块
const program = require('commander'); // 可以自动的解析命令和参数,用于处理用户输入的命令
const download = require('download-git-repo'); // 下载并提取 git 仓库,用于下载项目模板
const handlebars = require('handlebars'); // 模板引擎,将用户提交的信息动态填充到文件中
const inquirer = require('inquirer'); // 通用的命令行用户界面集合,用于和用户进行交互
const ora = require('ora'); // 下载过程久的话,可以用于显示下载中的动画效果
const chalk = require('chalk'); // 可以给终端的字体加上颜色
const symbols = require('log-symbols'); // 可以在终端上显示出 √ 或 × 等的图标
program.version('2.2.0', '-v, --version')
.command('init <name>')
.action((name) => {
if (!fs.existsSync(name)) {
inquirer.prompt([
{
type: 'list',
message: '请选择需要的模板:',
name: 'template',
choices: ['react模板', 'typescript模板', 'node模板', 'egg模板', 'react组件发包npm模板', 'typescript组件发包npm模板']
}
]).then((answers) => {
let branch;
switch (answers.template) {
case 'react模板':
branch = "https://github.com:bruceliu68/react-template#master";
break;
case 'typescript模板':
branch = "https://github.com:bruceliu68/react-template-typescript#master";
break;
case 'node模板':
branch = "https://github.com:bruceliu68/node-template#master";
break;
case 'egg模板':
branch = "https://github.com:bruceliu68/egg-template#master";
break;
case 'react组件发包npm模板':
branch = "https://github.com:bruceliu68/react-component-template#master";
break;
case 'typescript组件发包npm模板':
branch = "https://github.com:bruceliu68/react-component-typescript-template#master";
break;
}
inquirer.prompt([
{
name: 'description',
message: '请输入项目描述'
},
{
name: 'author',
message: '请输入作者名称'
}
]).then((answers) => {
const spinner = ora('正在下载模板...');
spinner.start();
download(branch, name, { clone: true }, (err) => {
if (err) {
spinner.fail();
console.log(symbols.error, chalk.red(err));
} else {
spinner.succeed();
const fileName = `${name}/package.json`;
const meta = {
name,
description: answers.description,
author: answers.author
}
if (fs.existsSync(fileName)) {
const content = fs.readFileSync(fileName).toString();
const result = handlebars.compile(content)(meta);
fs.writeFileSync(fileName, result);
}
console.log(symbols.success, chalk.green('项目初始化完成'));
}
})
})
})
} else {
// 错误提示项目已存在,避免覆盖原有项目
console.log(symbols.error, chalk.red('项目已存在'));
}
})
program.parse(process.argv);