@yuw-cli-dev/init 1.0.37 → 1.0.38

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.
@@ -0,0 +1,8 @@
1
+ const request = require('@yuw-cli-dev/request');
2
+
3
+ module.exports = async function () {
4
+ return request({
5
+ url: '/project/template',
6
+ method: 'GET',
7
+ });
8
+ };
package/lib/index.js CHANGED
@@ -1,8 +1,167 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- module.exports = init;
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const inquire = require('inquirer');
6
+ const fse = require('fs-extra');
7
+ const semver = require('semver');
8
+ const isValidFilename = require('valid-filename');
9
+ const Command = require('@yuw-cli-dev/command');
10
+ const log = require('@yuw-cli-dev/log');
11
+ const getProjectTemplate = require('./getProjectTemplate');
12
+
13
+ const TYPE_PROJECT = 'project';
14
+ const TYPE_COMPONENT = 'component';
15
+
16
+ class InitCommand extends Command {
17
+ init() {
18
+ this.projectName = this._argv[0] || '';
19
+ this.force = !!this._argv[1].force;
20
+ log.verbose('projectName', this.projectName);
21
+ log.verbose('force', this.force);
22
+ }
23
+ async exec() {
24
+ try {
25
+ const projectInfo = await this.prepare();
26
+ log.verbose('projectInfo', projectInfo);
27
+ if (projectInfo) {
28
+ this.projectInfo = projectInfo;
29
+ await this.downloadTemplate();
30
+ }
31
+ } catch (e) {
32
+ log.error(e.message);
33
+ }
34
+ }
35
+ downloadTemplate() {
36
+ return getProjectTemplate();
37
+ }
38
+ async prepare() {
39
+ const template = await getProjectTemplate();
40
+ if (!template || template.length === 0) {
41
+ throw new Error('项目模板不存在');
42
+ }
43
+ this.template = template;
44
+ log.verbose('template', template);
45
+
46
+ const localPath = process.cwd();
47
+ let isContinue = false;
48
+ if (!this.isDirEmpty(localPath)) {
49
+ if (!this.force) {
50
+ const rs = await inquire.prompt({
51
+ type: 'confirm',
52
+ name: 'isContinue',
53
+ message: '当前目录不为空,是否继续创建项目?(会清空当前目录)',
54
+ default: false,
55
+ });
56
+ isContinue = rs.isContinue;
57
+ if (!isContinue) {
58
+ return;
59
+ }
60
+ }
61
+ if (isContinue || this.force) {
62
+ const { ifContinue } = await inquire.prompt({
63
+ type: 'confirm',
64
+ name: 'ifContinue',
65
+ message: '是否确认清空当前目录下的文件?',
66
+ default: false,
67
+ });
68
+ if (ifContinue) {
69
+ fse.emptyDirSync(localPath);
70
+ }
71
+ }
72
+ }
73
+ return this.getProjectInfo();
74
+ }
75
+ async getProjectInfo() {
76
+ let projectInfo = {};
77
+ const { type } = await inquire.prompt({
78
+ type: 'list',
79
+ name: 'type',
80
+ message: '请选择初始化的类型',
81
+ default: TYPE_PROJECT,
82
+ choices: [
83
+ { name: '项目', value: TYPE_PROJECT },
84
+ { name: '组件', value: TYPE_COMPONENT },
85
+ ],
86
+ });
87
+ log.verbose('type', type);
88
+ if (type === TYPE_PROJECT) {
89
+ const project = await inquire.prompt([
90
+ {
91
+ type: 'input',
92
+ name: 'projectName',
93
+ message: '请输入项目名称',
94
+ default: '',
95
+ validate(v) {
96
+ const done = this.async();
97
+ setTimeout(() => {
98
+ if (!isValidFilename(v)) {
99
+ done('项目名称不合法');
100
+ return;
101
+ }
102
+ done(null, true);
103
+ }, 0);
104
+ },
105
+ filter(v) {
106
+ return v;
107
+ },
108
+ },
109
+ {
110
+ type: 'input',
111
+ name: 'projectVersion',
112
+ message: '请输入项目版本号',
113
+ default: '1.0.0',
114
+ validate(v) {
115
+ const done = this.async();
116
+ setTimeout(() => {
117
+ if (!!semver.valid(v) === false) {
118
+ done('项目版本号不合法');
119
+ return;
120
+ }
121
+ done(null, true);
122
+ }, 0);
123
+ },
124
+ filter(v) {
125
+ if (!!semver.valid(v)) {
126
+ return semver.valid(v);
127
+ }
128
+ return v;
129
+ },
130
+ },
131
+ {
132
+ type: 'list',
133
+ name: 'projectTemplate',
134
+ message: '请选择项目模板',
135
+ choices: this.createTemplateChoices(),
136
+ },
137
+ ]);
138
+ projectInfo = { ...project, type };
139
+ } else if (type === TYPE_COMPONENT) {
140
+ }
141
+ return projectInfo;
142
+ }
143
+
144
+ createTemplateChoices() {
145
+ return this.template.map(item => ({
146
+ name: item.name,
147
+ value: item.npmName,
148
+ }));
149
+ }
4
150
 
5
- function init(projectName, cmdObj) {
6
- // TODO
7
- console.log("init", projectName, cmdObj.force, process.env.CLI_TARGET_PATH);
151
+ isDirEmpty(localPath) {
152
+ let fileList = fs.readdirSync(localPath);
153
+ // 文件过滤逻辑
154
+ fileList = fileList.filter(
155
+ file => !file.startsWith('.') && !['node_modules'].includes(file),
156
+ );
157
+ return !fileList || fileList.length <= 0;
158
+ }
8
159
  }
160
+
161
+ function init(argv) {
162
+ log.verbose('argv', argv);
163
+ return new InitCommand(argv);
164
+ }
165
+
166
+ module.exports = init;
167
+ module.exports.InitCommand = InitCommand;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuw-cli-dev/init",
3
- "version": "1.0.37",
3
+ "version": "1.0.38",
4
4
  "description": "yuw-cli-dev init",
5
5
  "author": "yuwei <yuwei@huya.com>",
6
6
  "homepage": "https://github.com/yousanjin/yuw-cli#readme",
@@ -26,5 +26,14 @@
26
26
  "bugs": {
27
27
  "url": "https://github.com/yousanjin/yuw-cli/issues"
28
28
  },
29
- "gitHead": "ab03556d97ed9d4caf7af33d1720f4e45e34749a"
29
+ "dependencies": {
30
+ "@yuw-cli-dev/command": "file:../../models/command",
31
+ "@yuw-cli-dev/log": "^1.0.36",
32
+ "@yuw-cli-dev/request": "^1.0.38",
33
+ "fs-extra": "^9.0.1",
34
+ "inquirer": "^7.3.3",
35
+ "semver": "^7.7.3",
36
+ "valid-filename": "^3.1.0"
37
+ },
38
+ "gitHead": "f92c3015d6799312f5c9b446775cfb5ed577eeed"
30
39
  }