@john-cli/init 1.0.16 → 1.0.18

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