-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·59 lines (56 loc) · 1.86 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
#!/usr/bin/env node
const fs = require('fs');
const program = require('commander');
const download = require('download-git-repo');
const inquirer = require('inquirer');
const Handlebars = require('Handlebars');
const ora = require('ora');
const templates = {
'react-template': {
gitUrl: 'https://github.com/sunyonghua/react-template',
downloadUrl: 'https://github.com:sunyonghua/react-template#master',
description: 'react版的webpack脚手架'
},
'vue-template': {
gitUrl: 'https://github.com:sunyonghua/vue-template#master',
downloadUrl: '',
description: 'vue版的webpack脚手架'
}
};
// 查看版本
program.version('1.0.0');
// 初始化项目模版
program
.command('init <templateName> <projectName>')
.description('init project template')
.action((templateName, projectName) => {
const spinner = ora('downloading...').start();
download(`${templates[templateName].downloadUrl}`, projectName, { clone: true }, function(err) {
if (err) {
return spinner.fail("download fail !!");
}
spinner.succeed('download success !!');
inquirer
.prompt([
{ type: 'input', name: 'projectName', message: 'projectName' },
{ type: 'input', name: 'description', message: 'description' },
{ type: 'input', name: 'author', message: 'author' }
])
.then(value => {
const package = fs.readFileSync(`${projectName}/package.json`, 'utf-8');
const result = Handlebars.compile(package)(value);
fs.writeFileSync(`${projectName}/package.json`, result);
console.log('项目创建成功')
});
});
});
// 查看所有模版
program
.command('list')
.description('see all project')
.action(() => {
for (let key in templates) {
console.log(`${key} --${templates[key].description}`);
}
});
program.parse(process.argv);