-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (153 loc) · 4.32 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use strict";
const fs = require("fs");
const inquirer = require("inquirer");
const fse = require("fs-extra");
const semver = require("semver");
const Command = require("@fun5-cli-dev/command");
const log = require("@fun5-cli-dev/log");
const TYPE_PROJECT = "project";
const TYPE_COMPONENT = "component";
class InitCommand extends Command {
init() {
this.projectName = this._argv[0] || "";
this.force = this._cmd._optionValues.force;
log.verbose("projectName", this.projectName);
log.verbose("force", this.force);
}
async exec() {
try {
// 1.准备阶段
await this.prepare();
// 2.下载模版
// 3.安装模版
} catch (e) {
log.error(e.message);
}
}
async prepare() {
const localPath = process.cwd();
// 1.当前目录是否为空
if (!this.isDirEmpty(localPath)) {
let ifContinue = false;
if (!this.force) {
// 1.1询问是否继续创建 - inquirer
ifContinue = (
await inquirer.prompt({
type: "confirm",
name: "ifContinue",
default: false,
message:
"当前文件夹不为空,如需继续创建项目,将会清空当前文件夹,是否继续创建项目?",
})
).ifContinue;
if (!ifContinue) return;
}
// 2.是否为强制更新
if (ifContinue || this.force) {
// 二次确认
const { confirmDelete } = await inquirer.prompt({
type: "confirm",
name: "confirmDelete",
default: false,
message: "是否确认清空当前目录下的文件?",
});
// 清空当前目录(不删除当前目录)
if (confirmDelete) fse.emptyDirSync(localPath);
}
}
return this.getProjectInfo();
}
isDirEmpty(localPath) {
let fileList = fs.readdirSync(localPath);
fileList = fileList.filter(
(file) => !file.startsWith(".") && ["node_modules"].indexOf(file) < 0
);
return !fileList || fileList.length <= 0;
}
async getProjectInfo() {
const projectInfo = {};
// 1.选择创建项目获组件
const { type } = await inquirer.prompt({
type: "list",
name: "type",
default: "project",
message: "请选择初始化类型",
choices: [
{ name: "项目", value: TYPE_PROJECT },
{ name: "组件", value: TYPE_COMPONENT },
],
});
log.verbose("type", type);
if (type === TYPE_PROJECT) {
// 2.获取项目的基本信息
const o = await inquirer.prompt([
{
type: "input",
name: "projectName",
default: "",
message: "请输入项目名称",
validate: function (v) {
// 1.首字符必须为英文
// 2.尾字符必须为英文或数字
// 3.字符仅允许 -_
const done = this.async();
setTimeout(function () {
if (
!/^[a-zA-Z]+([-][a-zA-Z][a-zA-Z0-9]*|[_][a-zA-Z][a-zA-Z0-9]*|[a-zA-Z0-9])*$/.test(
v
)
) {
done("请输入合法的项目名称!");
return;
}
done(null, true);
}, 0);
},
filter: function (v) {
return v;
},
},
{
type: "input",
name: "projectVersion",
default: "1.0.0",
message: "请输入项目版本号",
validate: function (v) {
const done = this.async();
setTimeout(function () {
if (!!!semver.valid(v)) {
done("请输入合法的项目版本号!");
return;
}
done(null, true);
}, 0);
},
filter: function (v) {
return !!semver.valid(v) ? semver.valid(v) : v;
},
},
]);
console.log(o);
} else if (type === TYPE_COMPONENT) {
}
return projectInfo;
}
}
//projectName, cmdObj, command
function init(argv) {
return new InitCommand(argv);
/**
* 如果没有 apply
* arguments[0] = projectName
* cmdObj = undefined
console.log(
"inittt",
projectName,
cmdObj,
command.parent.opts(),
process.env.CLI_TARGET_PATH
);
*/
}
module.exports.InitCommand = InitCommand;
module.exports = init;