@john-cli/init 1.0.16 → 1.0.21

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.
Files changed (2) hide show
  1. package/lib/index.js +98 -97
  2. package/package.json +5 -4
package/lib/index.js CHANGED
@@ -5,7 +5,8 @@ const inquirer = require('inquirer')
5
5
  const path = require('path')
6
6
  const fse = require('fs-extra')
7
7
  const semver = require('semver')
8
- const userHome = require('user-home')
8
+ const userHome = require('userhome')
9
+
9
10
  const ejs = require('ejs')
10
11
  const Command = require('@john-cli/command')
11
12
  const log = require('@john-cli/log')
@@ -47,10 +48,10 @@ class InitCommand extends Command {
47
48
  log.verbose('projectInfo', projectInfo)
48
49
  this.projectInfo = projectInfo
49
50
  // if (projectInfo.type === TYPE_PROJECT) {
50
- // 1.下载模板
51
- await this.downloadTemplate()
52
- // 2.安装模板
53
- await this.installTemplate()
51
+ // 1.下载模板
52
+ await this.downloadTemplate()
53
+ // 2.安装模板
54
+ await this.installTemplate()
54
55
  // }
55
56
  } catch (e) {
56
57
  log.error(e.message)
@@ -97,7 +98,7 @@ class InitCommand extends Command {
97
98
  throw new Error(errMsg)
98
99
  }
99
100
  const args = cmdArr.slice(1)
100
- ret = await execAsync(cmd,args, {
101
+ ret = await execAsync(cmd, args, {
101
102
  stdio: 'inherit',
102
103
  cwd: process.cwd()
103
104
  })
@@ -108,7 +109,7 @@ class InitCommand extends Command {
108
109
  return ret
109
110
  }
110
111
 
111
- async ejsRender(option = {}) {
112
+ async ejsRender (option = {}) {
112
113
  const dir = process.cwd()
113
114
  const projectInfo = this.projectInfo
114
115
  return new Promise((resolve, reject) => {
@@ -125,7 +126,7 @@ class InitCommand extends Command {
125
126
  const filePath = path.join(dir, file)
126
127
  // console.log(filePath)
127
128
  return new Promise((resolve1, reject1) => {
128
- ejs.renderFile(filePath, projectInfo, {}, (err,res) => {
129
+ ejs.renderFile(filePath, projectInfo, {}, (err, res) => {
129
130
  // console.log(err,res)
130
131
  if (err) {
131
132
  reject1(err)
@@ -144,7 +145,7 @@ class InitCommand extends Command {
144
145
  })
145
146
  }
146
147
 
147
- async installNormalTemplate() {
148
+ async installNormalTemplate () {
148
149
  console.log('templateNpm22:', this.templateNpm)
149
150
  // 拷贝模板代码至当前目录
150
151
  let spinner = spinnerStart('正在安装模板...')
@@ -164,7 +165,7 @@ class InitCommand extends Command {
164
165
  }
165
166
  const templateIgnore = this.templateInfo.ignore || []
166
167
  const ignore = ['**/node_modules/**', ...templateIgnore]
167
- await this.ejsRender({ignore})
168
+ await this.ejsRender({ ignore })
168
169
  const { installCommand, startCommand } = this.templateInfo
169
170
  // 依赖安装 npm install
170
171
  await this.execCommand(installCommand, '安装依赖过程失败!')
@@ -172,7 +173,7 @@ class InitCommand extends Command {
172
173
  await this.execCommand(startCommand, '启动过程失败!')
173
174
  }
174
175
 
175
- async installCustomTemplate() {
176
+ async installCustomTemplate () {
176
177
  console.log('安装自定义模板', this.templateNpm)
177
178
  }
178
179
 
@@ -185,7 +186,7 @@ class InitCommand extends Command {
185
186
  // console.log(this.projectInfo, this.template)
186
187
  const { projectTemplate } = this.projectInfo
187
188
  const templateInfo = this.template.find((item) => {
188
- return item.npmName === projectTemplate
189
+ return item.npmName === projectTemplate
189
190
  })
190
191
 
191
192
  const targetPath = path.resolve(userHome, '.john-cli', 'template') // 缓存项目模板
@@ -267,7 +268,7 @@ class InitCommand extends Command {
267
268
  // 3.是否强制更新
268
269
  if (ifContinue || this.force) {
269
270
  // 给用户做二次确认
270
- const { confirmDelete } = await inquirer.prompt({
271
+ const { confirmDelete } = await inquirer.prompt({
271
272
  type: 'confirm',
272
273
  name: 'confirmDelete',
273
274
  message: '是否确认清空当前目录下的文件?',
@@ -303,115 +304,115 @@ class InitCommand extends Command {
303
304
  choices: [{
304
305
  name: '项目',
305
306
  value: TYPE_PROJECT
306
- },{
307
+ }, {
307
308
  name: '组件',
308
309
  value: TYPE_COMPONENT
309
310
  }]
310
311
  })
311
312
  log.verbose('type', type)
312
313
  this.template = this.template.filter((template) => template.tag.includes(type))
313
- console.log('template:::',this.template)
314
+ console.log('template:::', this.template)
314
315
  const title = type === TYPE_PROJECT ? '项目' : '组件'
315
316
  // 2.获取项目基本信息(名称、版本号、描述)
316
- // 获取项目的基本信息
317
- const projectNamePrompt = {
317
+ // 获取项目的基本信息
318
+ const projectNamePrompt = {
319
+ type: 'input',
320
+ name: 'projectName',
321
+ message: `请输入${title}名称`,
322
+ default: '',
323
+ validate: function (v) {
324
+ // 限制输入规则
325
+ // 1.输入的首字符必须为英文字符
326
+ // 2.尾字符必须为英文或数字,不能为字符
327
+ // 3.字符仅允许"-_"
328
+ // a-b a_b aaabbb 不能出现 a_ a-
329
+ // 合法:a,a-b,a_b,a-b-c,a_b_c,a-b1-c1, a_b1_c1
330
+ // 不合法:1, a_, a-, a_1, a-1
331
+ // return /^[a-zA-Z]+([-][a-zA-Z][a-zA-Z0-9]*|[_][a-zA-Z][a-zA-Z0-9]*|[a-zA-Z0-9])*$/.test(v)
332
+ const done = this.async()
333
+ setTimeout(function () {
334
+ if (!isValidName(v)) {
335
+ done('请输入合法的项目名称(a-zA-Z0-9 or a_zA_Z0_9)')
336
+ return;
337
+ }
338
+ done(null, true)
339
+ }, 0);
340
+ },
341
+ filter: function (v) {
342
+ return v
343
+ }
344
+ }
345
+ const projectPrompt = []
346
+ if (!isProjectNameValid) {
347
+ projectPrompt.push(projectNamePrompt)
348
+ }
349
+ projectPrompt.push(
350
+ {
318
351
  type: 'input',
319
- name: 'projectName',
320
- message: `请输入${title}名称`,
321
- default: '',
352
+ name: 'projectVersion',
353
+ message: `请输入${title}版本号`,
354
+ default: '1.0.0',
322
355
  validate: function (v) {
323
- // 限制输入规则
324
- // 1.输入的首字符必须为英文字符
325
- // 2.尾字符必须为英文或数字,不能为字符
326
- // 3.字符仅允许"-_"
327
- // a-b a_b aaabbb 不能出现 a_ a-
328
- // 合法:a,a-b,a_b,a-b-c,a_b_c,a-b1-c1, a_b1_c1
329
- // 不合法:1, a_, a-, a_1, a-1
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)
331
356
  const done = this.async()
332
- setTimeout(function() {
333
- if (!isValidName(v)) {
334
- done('请输入合法的项目名称(a-zA-Z0-9 or a_zA_Z0_9)')
357
+ setTimeout(function () {
358
+ if (!(!!semver.valid(v))) {
359
+ done('请输入合法的版本号(如:v1.0.0 or 1.0.0)')
335
360
  return;
336
361
  }
337
362
  done(null, true)
338
363
  }, 0);
339
364
  },
340
365
  filter: function (v) {
341
- return v
366
+ if (!!semver.valid(v)) {
367
+ return semver.valid(v)
368
+ } else {
369
+ return v
370
+ }
342
371
  }
372
+ }, {
373
+ type: 'list',
374
+ name: 'projectTemplate',
375
+ message: `请选择${title}模板`,
376
+ choices: this.createTemplateChoice()
377
+ }
378
+ )
379
+ if (type === TYPE_PROJECT) {
380
+ const project = await inquirer.prompt(projectPrompt)
381
+ projectInfo = {
382
+ ...projectInfo,
383
+ type,
384
+ ...project
343
385
  }
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
386
+ } else if (type === TYPE_COMPONENT) {
387
+ const descriptionPrompt = {
388
+ type: 'input',
389
+ name: 'componentDescription',
390
+ message: '请输入组件描述信息',
391
+ default: '1.0.0',
392
+ validate: function (v) {
393
+ const done = this.async()
394
+ setTimeout(function () {
395
+ if (!(v)) {
396
+ done('请输入组件描述信息')
397
+ return;
369
398
  }
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
399
+ done(null, true)
400
+ }, 0);
408
401
  }
409
402
  }
403
+ projectPrompt.push(descriptionPrompt)
404
+ const component = await inquirer.prompt(projectPrompt)
405
+ projectInfo = {
406
+ ...projectInfo,
407
+ type,
408
+ ...component
409
+ }
410
+ }
410
411
 
411
412
  // 生成className
412
413
  if (projectInfo.projectName) {
413
414
  projectInfo.name = projectInfo.projectName
414
- projectInfo.className = require('kebab-case')(projectInfo.projectName).replace(/^-/,'')
415
+ projectInfo.className = require('kebab-case')(projectInfo.projectName).replace(/^-/, '')
415
416
  }
416
417
  if (projectInfo.projectVersion) {
417
418
  projectInfo.version = projectInfo.projectVersion
@@ -444,7 +445,7 @@ class InitCommand extends Command {
444
445
 
445
446
  }
446
447
 
447
- function init(argv) {
448
+ function init (argv) {
448
449
  // console.log('init', projectName, cmdObj.force, process.env.CLI_TARGET_PATH)
449
450
  return new InitCommand(argv);
450
451
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@john-cli/init",
3
- "version": "1.0.16",
3
+ "version": "1.0.21",
4
4
  "description": "john-cli init",
5
5
  "author": "tanhongjian <350089447@qq.com>",
6
6
  "homepage": "",
@@ -26,7 +26,7 @@
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.16",
29
+ "@john-cli/package": "^1.0.19",
30
30
  "@john-cli/request": "^1.0.7",
31
31
  "@john-cli/utils": "^1.0.7",
32
32
  "ejs": "^3.1.8",
@@ -34,7 +34,8 @@
34
34
  "glob": "^8.0.3",
35
35
  "inquirer": "^8.0.0",
36
36
  "kebab-case": "^1.0.1",
37
- "semver": "^7.3.7"
37
+ "semver": "^7.3.7",
38
+ "userhome": "^1.0.0"
38
39
  },
39
- "gitHead": "cc34ba2c8cc3f3374718208f1f163dafa24b661f"
40
+ "gitHead": "5f0545ffa2b525d8023cecf9007210c2ab6a4e85"
40
41
  }