@john-cli/init 1.0.7 → 1.0.15
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/lib/index.js +129 -38
- package/package.json +5 -3
package/lib/index.js
CHANGED
|
@@ -6,6 +6,7 @@ const path = require('path')
|
|
|
6
6
|
const fse = require('fs-extra')
|
|
7
7
|
const semver = require('semver')
|
|
8
8
|
const userHome = require('user-home')
|
|
9
|
+
const ejs = require('ejs')
|
|
9
10
|
const Command = require('@john-cli/command')
|
|
10
11
|
const log = require('@john-cli/log')
|
|
11
12
|
const Package = require('@john-cli/package')
|
|
@@ -45,14 +46,17 @@ class InitCommand extends Command {
|
|
|
45
46
|
const projectInfo = await this.prepare()
|
|
46
47
|
log.verbose('projectInfo', projectInfo)
|
|
47
48
|
this.projectInfo = projectInfo
|
|
48
|
-
if (projectInfo.type === TYPE_PROJECT) {
|
|
49
|
+
// if (projectInfo.type === TYPE_PROJECT) {
|
|
49
50
|
// 1.下载模板
|
|
50
51
|
await this.downloadTemplate()
|
|
51
52
|
// 2.安装模板
|
|
52
53
|
await this.installTemplate()
|
|
53
|
-
}
|
|
54
|
+
// }
|
|
54
55
|
} catch (e) {
|
|
55
56
|
log.error(e.message)
|
|
57
|
+
if (process.env.LOG_LEVEL === 'verbose') {
|
|
58
|
+
console.log(e)
|
|
59
|
+
}
|
|
56
60
|
}
|
|
57
61
|
}
|
|
58
62
|
|
|
@@ -104,6 +108,42 @@ class InitCommand extends Command {
|
|
|
104
108
|
return ret
|
|
105
109
|
}
|
|
106
110
|
|
|
111
|
+
async ejsRender(option = {}) {
|
|
112
|
+
const dir = process.cwd()
|
|
113
|
+
const projectInfo = this.projectInfo
|
|
114
|
+
return new Promise((resolve, reject) => {
|
|
115
|
+
require('glob')('**', {
|
|
116
|
+
cwd: dir,
|
|
117
|
+
ignore: option.ignore || '',
|
|
118
|
+
nodir: true
|
|
119
|
+
}, (err, files) => {
|
|
120
|
+
if (err) {
|
|
121
|
+
reject(err)
|
|
122
|
+
}
|
|
123
|
+
// console.log(files)
|
|
124
|
+
Promise.all(files.map(file => {
|
|
125
|
+
const filePath = path.join(dir, file)
|
|
126
|
+
// console.log(filePath)
|
|
127
|
+
return new Promise((resolve1, reject1) => {
|
|
128
|
+
ejs.renderFile(filePath, projectInfo, {}, (err,res) => {
|
|
129
|
+
// console.log(err,res)
|
|
130
|
+
if (err) {
|
|
131
|
+
reject1(err)
|
|
132
|
+
} else {
|
|
133
|
+
fse.writeFileSync(filePath, res)
|
|
134
|
+
resolve1(res)
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
})).then(() => {
|
|
139
|
+
resolve()
|
|
140
|
+
}).catch(err => {
|
|
141
|
+
reject(err)
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
107
147
|
async installNormalTemplate() {
|
|
108
148
|
console.log('templateNpm22:', this.templateNpm)
|
|
109
149
|
// 拷贝模板代码至当前目录
|
|
@@ -122,6 +162,9 @@ class InitCommand extends Command {
|
|
|
122
162
|
spinner.stop(true)
|
|
123
163
|
log.success('模板安装成功')
|
|
124
164
|
}
|
|
165
|
+
const templateIgnore = this.templateInfo.ignore || []
|
|
166
|
+
const ignore = ['**/node_modules/**', ...templateIgnore]
|
|
167
|
+
await this.ejsRender({ignore})
|
|
125
168
|
const { installCommand, startCommand } = this.templateInfo
|
|
126
169
|
// 依赖安装 npm install
|
|
127
170
|
await this.execCommand(installCommand, '安装依赖过程失败!')
|
|
@@ -242,7 +285,15 @@ class InitCommand extends Command {
|
|
|
242
285
|
}
|
|
243
286
|
|
|
244
287
|
async getProjectInfo () {
|
|
288
|
+
function isValidName (projectName) {
|
|
289
|
+
return /^[a-zA-Z]+([-][a-zA-Z][a-zA-Z0-9]*|[_][a-zA-Z][a-zA-Z0-9]*|[a-zA-Z0-9])*$/.test(projectName)
|
|
290
|
+
}
|
|
245
291
|
let projectInfo = {}
|
|
292
|
+
let isProjectNameValid = false
|
|
293
|
+
if (isValidName(this.projectName)) {
|
|
294
|
+
isProjectNameValid = true
|
|
295
|
+
projectInfo.projectName = this.projectName
|
|
296
|
+
}
|
|
246
297
|
// 1.获取项目类型(项目/组件)
|
|
247
298
|
const { type } = await inquirer.prompt({
|
|
248
299
|
type: 'list',
|
|
@@ -258,13 +309,15 @@ class InitCommand extends Command {
|
|
|
258
309
|
}]
|
|
259
310
|
})
|
|
260
311
|
log.verbose('type', type)
|
|
312
|
+
this.template = this.template.filter((template) => template.tag.includes(type))
|
|
313
|
+
console.log('template:::',this.template)
|
|
314
|
+
const title = type === TYPE_PROJECT ? '项目' : '组件'
|
|
261
315
|
// 2.获取项目基本信息(名称、版本号、描述)
|
|
262
|
-
if (type === TYPE_PROJECT) {
|
|
263
316
|
// 获取项目的基本信息
|
|
264
|
-
const
|
|
317
|
+
const projectNamePrompt = {
|
|
265
318
|
type: 'input',
|
|
266
319
|
name: 'projectName',
|
|
267
|
-
message:
|
|
320
|
+
message: `请输入${title}名称`,
|
|
268
321
|
default: '',
|
|
269
322
|
validate: function (v) {
|
|
270
323
|
// 限制输入规则
|
|
@@ -277,7 +330,7 @@ class InitCommand extends Command {
|
|
|
277
330
|
// return /^[a-zA-Z]+([-][a-zA-Z][a-zA-Z0-9]*|[_][a-zA-Z][a-zA-Z0-9]*|[a-zA-Z0-9])*$/.test(v)
|
|
278
331
|
const done = this.async()
|
|
279
332
|
setTimeout(function() {
|
|
280
|
-
if (
|
|
333
|
+
if (!isValidName(v)) {
|
|
281
334
|
done('请输入合法的项目名称(a-zA-Z0-9 or a_zA_Z0_9)')
|
|
282
335
|
return;
|
|
283
336
|
}
|
|
@@ -287,47 +340,85 @@ class InitCommand extends Command {
|
|
|
287
340
|
filter: function (v) {
|
|
288
341
|
return v
|
|
289
342
|
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
343
|
+
}
|
|
344
|
+
const projectPrompt = []
|
|
345
|
+
if (!isProjectNameValid) {
|
|
346
|
+
projectPrompt.push(projectNamePrompt)
|
|
347
|
+
}
|
|
348
|
+
projectPrompt.push(
|
|
349
|
+
{
|
|
350
|
+
type: 'input',
|
|
351
|
+
name: 'projectVersion',
|
|
352
|
+
message: `请输入${title}版本号`,
|
|
353
|
+
default: '1.0.0',
|
|
354
|
+
validate: function (v) {
|
|
355
|
+
const done = this.async()
|
|
356
|
+
setTimeout(function() {
|
|
357
|
+
if (!(!!semver.valid(v))) {
|
|
358
|
+
done('请输入合法的版本号(如:v1.0.0 or 1.0.0)')
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
done(null, true)
|
|
362
|
+
}, 0);
|
|
363
|
+
},
|
|
364
|
+
filter: function (v) {
|
|
365
|
+
if (!!semver.valid(v)) {
|
|
366
|
+
return semver.valid(v)
|
|
367
|
+
} else {
|
|
368
|
+
return v
|
|
301
369
|
}
|
|
302
|
-
done(null, true)
|
|
303
|
-
}, 0);
|
|
304
|
-
},
|
|
305
|
-
filter: function (v) {
|
|
306
|
-
if (!!semver.valid(v)) {
|
|
307
|
-
return semver.valid(v)
|
|
308
|
-
} else {
|
|
309
|
-
return v
|
|
310
370
|
}
|
|
371
|
+
},{
|
|
372
|
+
type: 'list',
|
|
373
|
+
name: 'projectTemplate',
|
|
374
|
+
message: `请选择${title}模板`,
|
|
375
|
+
choices: this.createTemplateChoice()
|
|
376
|
+
}
|
|
377
|
+
)
|
|
378
|
+
if (type === TYPE_PROJECT) {
|
|
379
|
+
const project = await inquirer.prompt(projectPrompt)
|
|
380
|
+
projectInfo = {
|
|
381
|
+
...projectInfo,
|
|
382
|
+
type,
|
|
383
|
+
...project
|
|
384
|
+
}
|
|
385
|
+
} else if (type === TYPE_COMPONENT) {
|
|
386
|
+
const descriptionPrompt = {
|
|
387
|
+
type: 'input',
|
|
388
|
+
name: 'componentDescription',
|
|
389
|
+
message: '请输入组件描述信息',
|
|
390
|
+
default: '1.0.0',
|
|
391
|
+
validate: function (v) {
|
|
392
|
+
const done = this.async()
|
|
393
|
+
setTimeout(function() {
|
|
394
|
+
if (!(v)) {
|
|
395
|
+
done('请输入组件描述信息')
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
done(null, true)
|
|
399
|
+
}, 0);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
projectPrompt.push(descriptionPrompt)
|
|
403
|
+
const component = await inquirer.prompt(projectPrompt)
|
|
404
|
+
projectInfo = {
|
|
405
|
+
...projectInfo,
|
|
406
|
+
type,
|
|
407
|
+
...component
|
|
311
408
|
}
|
|
312
|
-
},{
|
|
313
|
-
type: 'list',
|
|
314
|
-
name: 'projectTemplate',
|
|
315
|
-
message: '请选择项目模板',
|
|
316
|
-
choices: this.createTemplateChoice()
|
|
317
|
-
}
|
|
318
|
-
])
|
|
319
|
-
projectInfo = {
|
|
320
|
-
type,
|
|
321
|
-
...project
|
|
322
409
|
}
|
|
323
|
-
} else if (type === TYPE_COMPONENT) {
|
|
324
|
-
|
|
325
|
-
}
|
|
326
410
|
|
|
327
411
|
// 生成className
|
|
328
412
|
if (projectInfo.projectName) {
|
|
413
|
+
projectInfo.name = projectInfo.projectName
|
|
329
414
|
projectInfo.className = require('kebab-case')(projectInfo.projectName).replace(/^-/,'')
|
|
330
415
|
}
|
|
416
|
+
if (projectInfo.projectVersion) {
|
|
417
|
+
projectInfo.version = projectInfo.projectVersion
|
|
418
|
+
}
|
|
419
|
+
if (projectInfo.componentDescription) {
|
|
420
|
+
projectInfo.description = projectInfo.componentDescription
|
|
421
|
+
}
|
|
331
422
|
|
|
332
423
|
// return 项目的基本信息(Object)
|
|
333
424
|
return projectInfo
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@john-cli/init",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"description": "john-cli init",
|
|
5
5
|
"author": "tanhongjian <350089447@qq.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -26,13 +26,15 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@john-cli/command": "file:../../mocels/command",
|
|
28
28
|
"@john-cli/log": "file:../../log/log",
|
|
29
|
-
"@john-cli/package": "^1.0.
|
|
29
|
+
"@john-cli/package": "^1.0.15",
|
|
30
30
|
"@john-cli/request": "^1.0.7",
|
|
31
31
|
"@john-cli/utils": "^1.0.7",
|
|
32
|
+
"ejs": "^3.1.8",
|
|
32
33
|
"fs-extra": "^10.1.0",
|
|
34
|
+
"glob": "^8.0.3",
|
|
33
35
|
"inquirer": "^8.0.0",
|
|
34
36
|
"kebab-case": "^1.0.1",
|
|
35
37
|
"semver": "^7.3.7"
|
|
36
38
|
},
|
|
37
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "ebb4b3f33c6730ea6084626582c34b7e43296d6d"
|
|
38
40
|
}
|