-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (88 loc) · 3.65 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
84
85
86
87
88
#!/usr/bin/env node
const fs = require('fs');
const program = require('commander');
const download = require('download-git-repo');
const handlebars = require('handlebars');
const inquirer = require('inquirer');
const ora = require('ora');
const chalk = require('chalk');
const symbols = require('log-symbols');
program.version('1.1.0', '-v, --version')
.command('init [name]')
.action((name) => {
name = name || "quant-project"
if (!fs.existsSync(name)) {
inquirer.prompt([
{
name: 'title',
message: '请输入项目标题',
default: "web管理后台",
}, {
name: 'description',
message: '请输入项目描述',
default: "description"
},
{
name: 'author',
message: '请输入作者名称',
default: "author"
}, {
type: "confirm",
name: 'isTop',
message: '导航栏是在顶部Y|| 侧边N',
default: true,
}, {
type: "confirm",
name: 'haveDemo',
message: '生成的项目是否涵盖管理工程Demo用例 Y || N',
default: false,
}, {
name: 'version',
message: '请输入项目版本',
default: "1.0.0",
}
]).then((answers) => {
const spinner = ora('正在生成模板...');
spinner.start();
let url = "direct:https://github.com/LiLixikun/tuitui-ui.git";
if (!!answers.haveDemo) {
url = "direct:https://github.com/LiLixikun/tuitui-ui";
}
download(url, name, { clone: true }, (err) => {
if (err) {
spinner.fail();
console.log(symbols.error, chalk.red(err), err);
} else {
spinner.succeed();
const fileName = `${name}/package.json`;
const configName = `${name}/src/common/config.js`;
const meta = {
name,
description: answers.description,
author: answers.author,
version: answers.version
}
const config = {
isTop: answers.isTop,
title: answers.title,
}
if (fs.existsSync(fileName)) {
const content = fs.readFileSync(fileName).toString();
const result = handlebars.compile(content)(meta);
fs.writeFileSync(fileName, result);
}
if (fs.existsSync(configName)) {
const content = fs.readFileSync(configName).toString();
const result = handlebars.compile(content)(config);
fs.writeFileSync(configName, result);
}
console.log(symbols.success, chalk.green('项目初始化完成'));
}
})
})
} else {
// 错误提示项目已存在,避免覆盖原有项目
console.log(symbols.error, chalk.red('项目已存在'));
}
})
program.parse(process.argv);