init.js
2.04 KB
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
84
85
86
87
88
89
#!/usr/bin/env node
const inquirer = require('inquirer');
const chalk = require('chalk');
const exec = require('child_process').exec;
const templateMap = require(`${__dirname}/../template`);
const templateOptions = Object.keys(templateMap).reduce((list, key) => {
list.push({
name: `${templateMap[key].name} (${key})`,
value: key,
short: templateMap[key].name,
});
return list;
}, []);
const questions = [
{
name: 'template',
type: 'list',
message: '请选择使用的模板?',
choices: templateOptions,
},
{
name: 'projectName',
type: 'input',
message: '请输入系统名称',
validate(val) {
if (val === '') return '请输入系统名称';
return true;
},
},
];
function initAsk() {
inquirer.prompt(questions).then((answers) => {
const result = {
template: answers.template,
projectName: answers.projectName,
};
const template = templateMap[result.template];
initProject(result.projectName, template);
});
}
function initProject(projectName, template) {
console.log(chalk.white('\n 开始下载模板... \n'));
exec(`git clone -b ${template.branch} --depth=1 ${template.repo} ${projectName}`, (err) => {
if (err) {
console.log(chalk.red(`模板下载失败. ${err}`));
process.exit(1);
} else {
try {
commitWork(projectName, (err) => {
if (err) {
console.log(chalk.red(err));
} else {
console.log(chalk.green('\n ✅下载完成!'));
console.log('\n 快速开始');
console.log(`\n cd ${projectName}`);
console.log('\n npm install');
console.log('\n npm start\n');
process.exit(1);
}
});
} catch (err) {
console.log('项目配置生成失败', chalk.red(err));
}
}
});
}
function commitWork(projectName, callback) {
exec(
`
cd ${projectName}
rm -rf .git
git init
git add .
git commit -m 'init ${projectName}'
`,
callback,
);
}
initAsk();