@fastcar/cli 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/init.js +235 -243
package/package.json
CHANGED
package/src/init.js
CHANGED
|
@@ -2,280 +2,272 @@ const process = require("process");
|
|
|
2
2
|
const fs = require("fs");
|
|
3
3
|
const path = require("path");
|
|
4
4
|
const exec = require("child_process").execSync;
|
|
5
|
-
const inquirer = require(
|
|
5
|
+
const inquirer = require("inquirer");
|
|
6
6
|
const utils = require("./utils");
|
|
7
7
|
|
|
8
|
-
const WEBTEMPLATEURL =
|
|
9
|
-
|
|
8
|
+
const WEBTEMPLATEURL =
|
|
9
|
+
"https://e.coding.net/william_zhong/fast-car/fastcar-boot-web.git"; //web模板
|
|
10
|
+
const RPCTEMPLATEURL =
|
|
11
|
+
"https://e.coding.net/william_zhong/fast-car/fastcar-boot-rpc.git"; //rpc模板
|
|
12
|
+
|
|
10
13
|
const optionComponent = ["mysql", "redis", "mongo"];
|
|
11
14
|
|
|
12
15
|
const Questions = async (defaultName) => {
|
|
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
|
-
}
|
|
16
|
+
return new Promise((resolve) => {
|
|
17
|
+
inquirer
|
|
18
|
+
.prompt([
|
|
19
|
+
{
|
|
20
|
+
type: "input",
|
|
21
|
+
name: "name",
|
|
22
|
+
default: defaultName,
|
|
23
|
+
message: `name (${defaultName}) :`,
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
type: "input",
|
|
27
|
+
name: "version",
|
|
28
|
+
default: "1.0.0",
|
|
29
|
+
message: "version (1.0.0) :",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
type: "input",
|
|
33
|
+
name: "description",
|
|
34
|
+
message: "description:",
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
type: "input",
|
|
38
|
+
name: "repositoryUrl",
|
|
39
|
+
message: "repository url:",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
type: "input",
|
|
43
|
+
name: "author",
|
|
44
|
+
message: "author:",
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
type: "input",
|
|
48
|
+
name: "license",
|
|
49
|
+
default: "MIT",
|
|
50
|
+
message: "license (MIT) :",
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
type: "confirm",
|
|
54
|
+
name: "private",
|
|
55
|
+
message: "private (true) :",
|
|
56
|
+
default: true,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
type: "confirm",
|
|
60
|
+
name: "mysql",
|
|
61
|
+
message: "mysql (true) :",
|
|
62
|
+
default: true,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
type: "confirm",
|
|
66
|
+
name: "redis",
|
|
67
|
+
message: "redis (true) :",
|
|
68
|
+
default: true,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
type: "confirm",
|
|
72
|
+
name: "mongo",
|
|
73
|
+
message: "mongo (false) :",
|
|
74
|
+
default: false,
|
|
75
|
+
},
|
|
76
|
+
])
|
|
77
|
+
.then((answers) => {
|
|
78
|
+
resolve(answers);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
};
|
|
77
82
|
|
|
78
83
|
async function init(args = ["web"]) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
84
|
+
let currDir = process.cwd();
|
|
85
|
+
let type = args[0];
|
|
86
|
+
|
|
87
|
+
let disList = currDir.split(path.sep);
|
|
88
|
+
let lastName = disList[disList.length - 1];
|
|
89
|
+
|
|
90
|
+
//判定是否有package.json文件 若存在则跳过这一步
|
|
91
|
+
let realPackagePath = path.join(currDir, "package.json");
|
|
92
|
+
let packageInfo = {};
|
|
93
|
+
let questionInfo = {
|
|
94
|
+
mysql: false,
|
|
95
|
+
redis: false,
|
|
96
|
+
mongo: false,
|
|
97
|
+
};
|
|
98
|
+
let componentList = [];
|
|
99
|
+
if (fs.existsSync(realPackagePath)) {
|
|
100
|
+
packageInfo = require(realPackagePath);
|
|
101
|
+
} else {
|
|
102
|
+
questionInfo = await Questions(lastName);
|
|
103
|
+
packageInfo = {
|
|
104
|
+
name: questionInfo.name,
|
|
105
|
+
version: questionInfo.version,
|
|
106
|
+
description: questionInfo.description,
|
|
107
|
+
author: questionInfo.author,
|
|
108
|
+
license: questionInfo.license,
|
|
109
|
+
private: questionInfo.private,
|
|
93
110
|
};
|
|
94
|
-
let componentList = [];
|
|
95
|
-
if (fs.existsSync(realPackagePath)) {
|
|
96
|
-
|
|
97
|
-
packageInfo = require(realPackagePath);
|
|
98
|
-
} else {
|
|
99
|
-
|
|
100
|
-
questionInfo = await Questions(lastName);
|
|
101
|
-
packageInfo = {
|
|
102
|
-
name: questionInfo.name,
|
|
103
|
-
version: questionInfo.version,
|
|
104
|
-
description: questionInfo.description,
|
|
105
|
-
author: questionInfo.author,
|
|
106
|
-
license: questionInfo.license,
|
|
107
|
-
private: questionInfo.private,
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
if (!!questionInfo.repositoryUrl) {
|
|
111
|
-
|
|
112
|
-
let repType = questionInfo.repositoryUrl.split(".");
|
|
113
|
-
Reflect.set(packageInfo, {
|
|
114
|
-
repository: {
|
|
115
|
-
type: repType,
|
|
116
|
-
url: questionInfo.repositoryUrl
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
Object.keys(packageInfo).forEach((key) => {
|
|
123
|
-
|
|
124
|
-
if (!packageInfo[key]) {
|
|
125
|
-
Reflect.deleteProperty(packageInfo, key);
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
Object.keys(questionInfo).forEach((key) => {
|
|
130
|
-
|
|
131
|
-
if (questionInfo[key]) {
|
|
132
|
-
if (optionComponent.includes(key)) {
|
|
133
111
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
let downloadUrl = "";
|
|
145
|
-
switch (type) {
|
|
146
|
-
|
|
147
|
-
case "web": {
|
|
148
|
-
|
|
149
|
-
downloadUrl = WEBTEMPLATEURL;
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
case "rpc": {
|
|
153
|
-
|
|
154
|
-
downloadUrl = RPCTEMPLATEURL;
|
|
155
|
-
break;
|
|
156
|
-
}
|
|
157
|
-
default: {
|
|
112
|
+
if (!!questionInfo.repositoryUrl) {
|
|
113
|
+
let repType = questionInfo.repositoryUrl.split(".");
|
|
114
|
+
Reflect.set(packageInfo, {
|
|
115
|
+
repository: {
|
|
116
|
+
type: repType,
|
|
117
|
+
url: questionInfo.repositoryUrl,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
158
122
|
|
|
159
|
-
|
|
160
|
-
|
|
123
|
+
Object.keys(packageInfo).forEach((key) => {
|
|
124
|
+
if (!packageInfo[key]) {
|
|
125
|
+
Reflect.deleteProperty(packageInfo, key);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
Object.keys(questionInfo).forEach((key) => {
|
|
130
|
+
if (questionInfo[key]) {
|
|
131
|
+
if (optionComponent.includes(key)) {
|
|
132
|
+
componentList.push(`@fastcar/${key}`);
|
|
133
|
+
if (key == "mysql") {
|
|
134
|
+
componentList.push(`@fastcar/${key}-tool`);
|
|
161
135
|
}
|
|
136
|
+
}
|
|
162
137
|
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
//先暂定为只有web组件
|
|
141
|
+
let downloadUrl = "";
|
|
142
|
+
switch (type) {
|
|
143
|
+
case "web": {
|
|
144
|
+
downloadUrl = WEBTEMPLATEURL;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case "rpc": {
|
|
148
|
+
downloadUrl = RPCTEMPLATEURL;
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
default: {
|
|
152
|
+
downloadUrl = WEBTEMPLATEURL;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
163
156
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
let templateName = urlList[urlList.length - 1].replace(/.git/, "");
|
|
157
|
+
if (downloadUrl) {
|
|
158
|
+
let urlList = downloadUrl.split("/");
|
|
159
|
+
let templateName = urlList[urlList.length - 1].replace(/.git/, "");
|
|
168
160
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
161
|
+
console.log(`Start downloading template ${downloadUrl}`);
|
|
162
|
+
exec(`git clone ${downloadUrl} --depth=1`);
|
|
163
|
+
console.log("Download complete");
|
|
172
164
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
165
|
+
//解压依赖
|
|
166
|
+
//删除原先路径下的包
|
|
167
|
+
let templateDir = path.join(currDir, templateName);
|
|
176
168
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
169
|
+
//删除git路径
|
|
170
|
+
let gitPath = path.join(templateDir, ".git");
|
|
171
|
+
if (fs.existsSync(gitPath)) {
|
|
172
|
+
utils.delDirEctory(gitPath);
|
|
173
|
+
}
|
|
180
174
|
|
|
181
|
-
|
|
175
|
+
console.log("copy template files");
|
|
176
|
+
//复制至项目文件下
|
|
177
|
+
utils.copyDirectory(templateDir, currDir);
|
|
178
|
+
|
|
179
|
+
//解压node_modules
|
|
180
|
+
// console.log("unzip node_modules");
|
|
181
|
+
// let nodeModulesPath = path.join(currDir, "node_modules.zip");
|
|
182
|
+
// await utils.unzipFile(nodeModulesPath, currDir);
|
|
183
|
+
|
|
184
|
+
//合并package.json文件
|
|
185
|
+
let templatePackagePath = path.join(templateDir, "package.json");
|
|
186
|
+
if (fs.existsSync(templatePackagePath)) {
|
|
187
|
+
let templatePackage = require(templatePackagePath);
|
|
188
|
+
//替换本地包名
|
|
189
|
+
if (templatePackage.scripts) {
|
|
190
|
+
templatePackage.scripts = JSON.stringify(
|
|
191
|
+
templatePackage.scripts
|
|
192
|
+
).replace(/\$npm_package_name/g, packageInfo.name);
|
|
193
|
+
templatePackage.scripts = JSON.parse(templatePackage.scripts);
|
|
194
|
+
}
|
|
195
|
+
if (templatePackage.dependencies) {
|
|
196
|
+
if (!packageInfo.dependencies) {
|
|
197
|
+
packageInfo.dependencies = {};
|
|
182
198
|
}
|
|
183
199
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
utils.copyDirectory(templateDir, currDir);
|
|
187
|
-
|
|
188
|
-
//解压node_modules
|
|
189
|
-
// console.log("unzip node_modules");
|
|
190
|
-
// let nodeModulesPath = path.join(currDir, "node_modules.zip");
|
|
191
|
-
// await utils.unzipFile(nodeModulesPath, currDir);
|
|
192
|
-
|
|
193
|
-
//合并package.json文件
|
|
194
|
-
let templatePackagePath = path.join(templateDir, "package.json");
|
|
195
|
-
if (fs.existsSync(templatePackagePath)) {
|
|
196
|
-
|
|
197
|
-
let templatePackage = require(templatePackagePath);
|
|
198
|
-
//替换本地包名
|
|
199
|
-
if (templatePackage.scripts) {
|
|
200
|
-
|
|
201
|
-
templatePackage.scripts = JSON.stringify(templatePackage.scripts).replace(/\$npm_package_name/g, packageInfo.name);
|
|
202
|
-
templatePackage.scripts = JSON.parse(templatePackage.scripts);
|
|
203
|
-
}
|
|
204
|
-
if (templatePackage.dependencies) {
|
|
205
|
-
|
|
206
|
-
if (!packageInfo.dependencies) {
|
|
207
|
-
|
|
208
|
-
packageInfo.dependencies = {};
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
let tmpDep = {};
|
|
212
|
-
// Object.keys(templatePackage.dependencies).forEach((tmpKey) => {
|
|
213
|
-
|
|
214
|
-
// let flag = optionComponent.some((o) => {
|
|
215
|
-
|
|
216
|
-
// return tmpKey.indexOf(o) != -1;
|
|
217
|
-
// });
|
|
200
|
+
let tmpDep = {};
|
|
201
|
+
// Object.keys(templatePackage.dependencies).forEach((tmpKey) => {
|
|
218
202
|
|
|
219
|
-
|
|
220
|
-
// if (flag) {
|
|
203
|
+
// let flag = optionComponent.some((o) => {
|
|
221
204
|
|
|
222
|
-
|
|
205
|
+
// return tmpKey.indexOf(o) != -1;
|
|
206
|
+
// });
|
|
223
207
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
// }
|
|
208
|
+
// //如果是可选组件则看有没有被包含进来
|
|
209
|
+
// if (flag) {
|
|
227
210
|
|
|
228
|
-
|
|
229
|
-
// });
|
|
230
|
-
componentList.forEach((item) => {
|
|
211
|
+
// if (!componentList.includes(tmpKey)) {
|
|
231
212
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
});
|
|
213
|
+
// return;
|
|
214
|
+
// }
|
|
215
|
+
// }
|
|
236
216
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
//覆盖其脚本
|
|
246
|
-
if (templatePackage.scripts) {
|
|
247
|
-
|
|
248
|
-
Object.assign(packageInfo.scripts, templatePackage.scripts);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
if (templatePackage.devDependencies) {
|
|
252
|
-
|
|
253
|
-
if (!packageInfo.devDependencies) {
|
|
254
|
-
|
|
255
|
-
packageInfo.devDependencies = {};
|
|
256
|
-
}
|
|
217
|
+
// Reflect.set(tmpDep, tmpKey, templatePackage.dependencies[tmpKey]);
|
|
218
|
+
// });
|
|
219
|
+
componentList.forEach((item) => {
|
|
220
|
+
if (!packageInfo.dependencies[item]) {
|
|
221
|
+
Reflect.set(tmpDep, item, `latest`);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
257
224
|
|
|
258
|
-
|
|
259
|
-
|
|
225
|
+
packageInfo.dependencies = Object.assign(
|
|
226
|
+
packageInfo.dependencies,
|
|
227
|
+
tmpDep,
|
|
228
|
+
templatePackage.dependencies
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (!packageInfo.scripts) {
|
|
233
|
+
packageInfo.scripts = {};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
//覆盖其脚本
|
|
237
|
+
if (templatePackage.scripts) {
|
|
238
|
+
Object.assign(packageInfo.scripts, templatePackage.scripts);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (templatePackage.devDependencies) {
|
|
242
|
+
if (!packageInfo.devDependencies) {
|
|
243
|
+
packageInfo.devDependencies = {};
|
|
260
244
|
}
|
|
261
245
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
246
|
+
Object.assign(
|
|
247
|
+
packageInfo.devDependencies,
|
|
248
|
+
templatePackage.devDependencies
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
267
252
|
|
|
268
|
-
|
|
269
|
-
|
|
253
|
+
console.log("wirte packageInfo");
|
|
254
|
+
fs.writeFileSync(realPackagePath, JSON.stringify(packageInfo, null, "\t"));
|
|
270
255
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
utils.writeYaml(pm2RunPath, pm2Config);
|
|
274
|
-
}
|
|
256
|
+
//更改配置的文件名
|
|
257
|
+
let projectName = packageInfo.name;
|
|
275
258
|
|
|
276
|
-
|
|
277
|
-
|
|
259
|
+
let pm2RunPath = path.join(currDir, "ecosystem.config.yml");
|
|
260
|
+
if (fs.existsSync(pm2RunPath)) {
|
|
261
|
+
let pm2Config = utils.readYaml(pm2RunPath);
|
|
262
|
+
pm2Config.apps.name = projectName;
|
|
263
|
+
utils.writeYaml(pm2RunPath, pm2Config);
|
|
278
264
|
}
|
|
265
|
+
|
|
266
|
+
console.log("clean files");
|
|
267
|
+
setTimeout(() => {
|
|
268
|
+
utils.delDirEctory(templateDir);
|
|
269
|
+
}, 1000);
|
|
270
|
+
}
|
|
279
271
|
}
|
|
280
272
|
|
|
281
|
-
module.exports = init;
|
|
273
|
+
module.exports = init;
|