@tmsfe/tmskit 0.0.7 → 0.0.9-beta.1
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/README.md +27 -27
- package/dist/index.cjs.js +1284 -983
- package/main.js +3 -3
- package/package.json +73 -75
- package/src/{gulp → compile}/build.js +5 -5
- package/src/{gulp → compile}/compile.js +90 -81
- package/src/{gulp → compile}/dev.js +129 -102
- package/src/{gulp → compile}/plugins/less.js +116 -116
- package/src/{gulp → compile}/plugins/mpCommonDep.js +131 -131
- package/src/{gulp → compile}/plugins/mpJsonDep.js +112 -108
- package/src/{gulp → compile}/plugins/mpWxmlDep.js +194 -194
- package/src/{gulp → compile}/plugins/postcss-font-base64.js +72 -72
- package/src/{gulp → compile}/plugins/replaceEnv.js +29 -29
- package/src/{gulp → compile}/plugins/utils/pluginError.js +25 -25
- package/src/config/constant.js +69 -71
- package/src/config/defaultTmsConfig.js +16 -16
- package/src/{utils → core}/buildAppJson.js +166 -221
- package/src/{utils → core}/checkDependencies.js +77 -77
- package/src/core/cloneModules.js +203 -0
- package/src/{utils → core}/handleError.js +18 -16
- package/src/core/isInIt.js +69 -0
- package/src/{utils/mpCiUtils.js → core/mpCi.js} +73 -73
- package/src/core/npm.js +218 -0
- package/src/core/symbolicLink.js +24 -0
- package/src/{utils/tkitUtils.js → core/tmsMpconfig.js} +234 -158
- package/src/entry.js +62 -60
- package/src/index.js +63 -62
- package/src/init.js +33 -33
- package/src/scripts/create/ask.js +63 -63
- package/src/scripts/create/generator.js +25 -25
- package/src/scripts/create/ignoreFiles.js +7 -7
- package/src/scripts/create/index.js +72 -72
- package/src/scripts/create/render.js +19 -19
- package/src/scripts/run/build/index.js +16 -17
- package/src/scripts/run/dev/index.js +42 -84
- package/src/scripts/run/index.js +97 -68
- package/src/scripts/run/init/index.js +95 -87
- package/src/scripts/run/install/index.js +31 -29
- package/src/utils/findCssImport.js +30 -30
- package/src/utils/global.js +22 -36
- package/src/utils/io.js +107 -106
- package/src/utils/log.js +47 -44
- package/src/utils/widgets.js +178 -167
- package/src/utils/cliUtils.js +0 -35
- package/src/utils/cloneModules.js +0 -116
- package/src/utils/npmUtils.js +0 -166
|
@@ -1,158 +1,234 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const {
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 用来读取处理tms.config.js与module.config.json字段
|
|
3
|
+
*/
|
|
4
|
+
const loadash = require('lodash');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const { TMS_NAME, TMS_CONFIG_FILENAME, MODULE_CONFIG_FILENAME, TMS_PRIVATE_FILENAME } = require('../config/constant');
|
|
7
|
+
const { resolve, isObject, isArray } = require('../utils/widgets');
|
|
8
|
+
const defaultTmsConfig = require('../config/defaultTmsConfig');
|
|
9
|
+
const { fail } = require('../utils/log');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 读取tms.config.js
|
|
14
|
+
* @param env {string} 环境变量
|
|
15
|
+
*/
|
|
16
|
+
const readTmsConfig = function (env) {
|
|
17
|
+
const tmsConfigPath = resolve(TMS_CONFIG_FILENAME);
|
|
18
|
+
if (!fs.existsSync(tmsConfigPath)) {
|
|
19
|
+
fail('当前执行目录没有tms.config.js的配置项,请进行配置');
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
const tmsConfigFn = require(tmsConfigPath);
|
|
23
|
+
const tmsConfig = tmsConfigFn({
|
|
24
|
+
env,
|
|
25
|
+
});
|
|
26
|
+
// 合并默认值
|
|
27
|
+
loadash.mergeWith(tmsConfig, defaultTmsConfig);
|
|
28
|
+
|
|
29
|
+
// modules兼容处理
|
|
30
|
+
tmsConfig.modules = convertModules(tmsConfig.modules);
|
|
31
|
+
return tmsConfig;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// convertModules 处理默认值
|
|
35
|
+
const convertModules = (modules) => {
|
|
36
|
+
const newModules = [];
|
|
37
|
+
modules.forEach((module) => {
|
|
38
|
+
const newModule = {};
|
|
39
|
+
if (typeof module === 'string') {
|
|
40
|
+
// 路径字符串
|
|
41
|
+
Object.assign(newModule, {
|
|
42
|
+
name: path.basename(module),
|
|
43
|
+
path: module,
|
|
44
|
+
});
|
|
45
|
+
} else if (typeof module === 'object') {
|
|
46
|
+
Object.assign(newModule, module);
|
|
47
|
+
if (module.name === undefined) {
|
|
48
|
+
newModule.name = path.basename(module.path);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
newModules.push(newModule);
|
|
52
|
+
});
|
|
53
|
+
return newModules;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 读取tms.private.config.js
|
|
58
|
+
*/
|
|
59
|
+
const readTmsPrivateCf = function () {
|
|
60
|
+
let tmsPrivateCf = {};
|
|
61
|
+
const tmsPrivatePath = resolve(TMS_PRIVATE_FILENAME);
|
|
62
|
+
if (fs.existsSync(tmsPrivatePath)) {
|
|
63
|
+
tmsPrivateCf = require(tmsPrivatePath);
|
|
64
|
+
}
|
|
65
|
+
// 处理modules字段
|
|
66
|
+
if (tmsPrivateCf.modules instanceof Array) {
|
|
67
|
+
Object.assign(tmsPrivateCf.modules, {
|
|
68
|
+
include: tmsPrivateCf.modules,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return tmsPrivateCf;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 从tms.config.json中检索用户传入的有效modules
|
|
76
|
+
* @param { object } tmsConfig
|
|
77
|
+
* @param { array } modules
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
const checkModules = function (tmsConfig, modules) {
|
|
81
|
+
const targetModules = [];
|
|
82
|
+
modules.forEach((moduleName) => {
|
|
83
|
+
const module = tmsConfig.modules.find(module => module.name === moduleName);
|
|
84
|
+
module && targetModules.push(module);
|
|
85
|
+
});
|
|
86
|
+
if (targetModules.length === 0) {
|
|
87
|
+
fail(`你启动的模块无效,尝试 ${TMS_NAME} -m moduleName`);
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
return targetModules;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 过滤页面为空的分包
|
|
95
|
+
* @param {Array} moduleCfg 模块配置内容
|
|
96
|
+
* @returns pages不为空的分包
|
|
97
|
+
*/
|
|
98
|
+
const getValidModules = (moduleCfg) => {
|
|
99
|
+
// 过滤 pages 为空的情况
|
|
100
|
+
const validModules = moduleCfg.filter(item => item.pages.length > 0);
|
|
101
|
+
return validModules;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 适配处理module.config.json的字段
|
|
106
|
+
* @param { object } fileContent module.config.json的内容
|
|
107
|
+
* @param { string } appName 小程序的名称
|
|
108
|
+
*/
|
|
109
|
+
function adaptMpCgContent(fileContent, appName) {
|
|
110
|
+
const content = fileContent.contents ? JSON.parse(fileContent.contents.toString()) : JSON.parse(fileContent);
|
|
111
|
+
|
|
112
|
+
if (isArray(content)) {
|
|
113
|
+
let i = content.length - 1;
|
|
114
|
+
while (i >= 0) {
|
|
115
|
+
let current = content[i];
|
|
116
|
+
|
|
117
|
+
if (appName && current.mpConfig && current.mpConfig[appName]) {
|
|
118
|
+
current = { ...current, ...current.mpConfig[appName] };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
delete current.mpConfig;
|
|
122
|
+
delete current.isSubpackages;
|
|
123
|
+
|
|
124
|
+
content[i] = current;
|
|
125
|
+
i--; // eslint-disable-line
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return content;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 递归获取本地所有模块的配置信息
|
|
133
|
+
* @param {array} modules 用户要编译的模块列表
|
|
134
|
+
* @param { string } appName 小程序的名称
|
|
135
|
+
* @param { string } moduleConfigFilename moduleConfig的文件名
|
|
136
|
+
*/
|
|
137
|
+
function getModuleConfig(modules = [], appName, moduleConfigFilename) {
|
|
138
|
+
const modulesConfig = {};
|
|
139
|
+
|
|
140
|
+
modules.forEach(({ path }) => {
|
|
141
|
+
const moduleConfigPath = resolve(path, moduleConfigFilename);
|
|
142
|
+
if (fs.existsSync(moduleConfigPath)) {
|
|
143
|
+
const content = fs.readFileSync(moduleConfigPath, 'utf-8');
|
|
144
|
+
modulesConfig[moduleConfigPath] = adaptMpCgContent(content, appName);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
return modulesConfig;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* tms.config.js的modules 合并 module.config.json的配置项
|
|
153
|
+
* @param {array} modules
|
|
154
|
+
* @param {string} appName
|
|
155
|
+
* @param {string} moduleDir
|
|
156
|
+
* @returns
|
|
157
|
+
*/
|
|
158
|
+
const tmsModulesMergeLocalModuleCfg = (modules, appName) => {
|
|
159
|
+
const newModules = [];
|
|
160
|
+
modules.forEach(({ path: relativePath, name: moduleName }, moduleIndex) => {
|
|
161
|
+
const moduleConfigPath = resolve(relativePath, MODULE_CONFIG_FILENAME);
|
|
162
|
+
if (fs.existsSync(moduleConfigPath)) {
|
|
163
|
+
try {
|
|
164
|
+
let findModule = false;
|
|
165
|
+
let moduleConfigContent = fs.readFileSync(moduleConfigPath, 'utf-8');
|
|
166
|
+
moduleConfigContent = adaptMpCgContent(moduleConfigContent, appName);
|
|
167
|
+
const moduleContentArr = isObject(moduleConfigContent) ? [moduleConfigContent] : moduleConfigContent;
|
|
168
|
+
getValidModules(moduleContentArr).forEach(({ name }, moduleContentArrIndex) => {
|
|
169
|
+
if (name === moduleName) {
|
|
170
|
+
findModule = true;
|
|
171
|
+
newModules.push({
|
|
172
|
+
...modules[moduleIndex],
|
|
173
|
+
...moduleContentArr[moduleContentArrIndex],
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
if (!findModule) {
|
|
178
|
+
fail(`启动模块${moduleName}在${moduleConfigPath}没有找到,请检查配置`);
|
|
179
|
+
process.exit(1);
|
|
180
|
+
}
|
|
181
|
+
} catch (e) {
|
|
182
|
+
fail(`${moduleConfigPath}配置错误: ${e}`);
|
|
183
|
+
newModules.push({
|
|
184
|
+
...modules[moduleIndex],
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
} else {
|
|
188
|
+
newModules.push({
|
|
189
|
+
...modules[moduleIndex],
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
return newModules;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 分包依赖了分包的模块 合并所依赖的modules
|
|
198
|
+
* @param { object } tmsConfig
|
|
199
|
+
* @param {array} modules
|
|
200
|
+
* @param {string} moduleDir
|
|
201
|
+
* @returns
|
|
202
|
+
*/
|
|
203
|
+
const subModulesMergeDepModules = (tmsConfig, modules) => {
|
|
204
|
+
const moduleNames = [];
|
|
205
|
+
modules.forEach(({ name: moduleName }) => {
|
|
206
|
+
moduleNames.push(moduleName);
|
|
207
|
+
});
|
|
208
|
+
let mergeModules = modules;
|
|
209
|
+
let isOver = true;
|
|
210
|
+
|
|
211
|
+
modules.forEach(({ dependencies: dependencyModules }) => {
|
|
212
|
+
dependencyModules?.forEach((item) => {
|
|
213
|
+
// 如果所有模块的dep都在moduleNames内,则所有依赖都齐了
|
|
214
|
+
// 否则递归处理,根据name找到相关配置加到modules里
|
|
215
|
+
if (moduleNames.indexOf(item) === -1) {
|
|
216
|
+
isOver = false;
|
|
217
|
+
const tmpModules = checkModules(tmsConfig, [...new Set([item])]);
|
|
218
|
+
mergeModules = [...mergeModules, ...tmpModules];
|
|
219
|
+
mergeModules = tmsModulesMergeLocalModuleCfg(mergeModules, tmsConfig.appName);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
return isOver ? mergeModules : subModulesMergeDepModules(tmsConfig, mergeModules);
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
module.exports = {
|
|
227
|
+
readTmsConfig,
|
|
228
|
+
readTmsPrivateCf,
|
|
229
|
+
getModuleConfig,
|
|
230
|
+
getValidModules,
|
|
231
|
+
checkModules,
|
|
232
|
+
tmsModulesMergeLocalModuleCfg,
|
|
233
|
+
subModulesMergeDepModules,
|
|
234
|
+
};
|
package/src/entry.js
CHANGED
|
@@ -1,60 +1,62 @@
|
|
|
1
|
-
module.exports = [
|
|
2
|
-
{
|
|
3
|
-
command: 'create <app-name>',
|
|
4
|
-
description: '创建新的应用',
|
|
5
|
-
action: (appName, cmd) => {
|
|
6
|
-
require('./scripts/create')(appName, cmd);
|
|
7
|
-
},
|
|
8
|
-
},
|
|
9
|
-
{
|
|
10
|
-
name: 'run',
|
|
11
|
-
type: 'child',
|
|
12
|
-
description: '项目开发使用的命令',
|
|
13
|
-
commands: [
|
|
14
|
-
{
|
|
15
|
-
command: 'install',
|
|
16
|
-
description: '安装依赖',
|
|
17
|
-
options: [
|
|
18
|
-
['-m, --module [moduleName]', '模块名称'],
|
|
19
|
-
['-e, --env [env]', '环境变量'],
|
|
20
|
-
],
|
|
21
|
-
action: (cmd) => {
|
|
22
|
-
require('./scripts/run/index')('install', cmd);
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
command: 'dev',
|
|
27
|
-
description: 'dev 打包编译',
|
|
28
|
-
options: [
|
|
29
|
-
['-m, --module [moduleName]', '模块名称'],
|
|
30
|
-
['-e, --env [env]', '环境变量'],
|
|
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
|
-
]
|
|
1
|
+
module.exports = [
|
|
2
|
+
{
|
|
3
|
+
command: 'create <app-name>',
|
|
4
|
+
description: '创建新的应用',
|
|
5
|
+
action: (appName, cmd) => {
|
|
6
|
+
require('./scripts/create')(appName, cmd);
|
|
7
|
+
},
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
name: 'run',
|
|
11
|
+
type: 'child',
|
|
12
|
+
description: '项目开发使用的命令',
|
|
13
|
+
commands: [
|
|
14
|
+
{
|
|
15
|
+
command: 'install',
|
|
16
|
+
description: '安装依赖',
|
|
17
|
+
options: [
|
|
18
|
+
['-m, --module [moduleName]', '模块名称'],
|
|
19
|
+
['-e, --env [env]', '环境变量'],
|
|
20
|
+
],
|
|
21
|
+
action: (cmd) => {
|
|
22
|
+
require('./scripts/run/index')('install', cmd);
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
command: 'dev',
|
|
27
|
+
description: 'dev 打包编译',
|
|
28
|
+
options: [
|
|
29
|
+
['-m, --module [moduleName]', '模块名称'],
|
|
30
|
+
['-e, --env [env]', '环境变量'],
|
|
31
|
+
['-latest, --latest', '下载最新第三方模块代码、安装最新依赖'],
|
|
32
|
+
],
|
|
33
|
+
action: (cmd) => {
|
|
34
|
+
require('./scripts/run/index')('dev', cmd);
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
command: 'build',
|
|
39
|
+
description: 'prod 打包编译',
|
|
40
|
+
options: [
|
|
41
|
+
['-m, --module [moduleName]', '模块名称'],
|
|
42
|
+
['-e, --env [env]', '环境变量'],
|
|
43
|
+
],
|
|
44
|
+
action: (cmd) => {
|
|
45
|
+
require('./scripts/run/index')('build', cmd);
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
// 对外暂不暴露该命令
|
|
49
|
+
// {
|
|
50
|
+
// command: 'init',
|
|
51
|
+
// description: '模块配置初始化项目(eg: 下载第三方模块代码、安装依赖、生成app.json等)',
|
|
52
|
+
// options: [
|
|
53
|
+
// ['-m, --module [moduleName]', '模块名称'],
|
|
54
|
+
// ['-e, --env [env]', '环境变量'],
|
|
55
|
+
// ],
|
|
56
|
+
// action: (cmd) => {
|
|
57
|
+
// require('./scripts/run/index')('init', cmd);
|
|
58
|
+
// },
|
|
59
|
+
// },
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
];
|
package/src/index.js
CHANGED
|
@@ -1,62 +1,63 @@
|
|
|
1
|
-
const chalk = require('chalk');
|
|
2
|
-
const commander = require('commander');
|
|
3
|
-
const {
|
|
4
|
-
const {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
cmd.
|
|
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
|
-
|
|
1
|
+
const chalk = require('chalk');
|
|
2
|
+
const commander = require('commander');
|
|
3
|
+
const { suggestCommands } = require('./utils/widgets');
|
|
4
|
+
const { info } = require('./utils/log');
|
|
5
|
+
const { TMS_NAME } = require('./config/constant.js');
|
|
6
|
+
const commands = require('./entry');
|
|
7
|
+
const init = require('./init');
|
|
8
|
+
|
|
9
|
+
init();
|
|
10
|
+
|
|
11
|
+
const program = new commander.Command(TMS_NAME);
|
|
12
|
+
|
|
13
|
+
program
|
|
14
|
+
.version(`${TMS_NAME} ${require('../package.json').version}`, '-v, -V, --version');
|
|
15
|
+
|
|
16
|
+
function registerCommand(program, commands) {
|
|
17
|
+
commands.forEach((cmd) => {
|
|
18
|
+
if (cmd.type === 'child') {
|
|
19
|
+
const childProgram = new commander.Command(cmd.name);
|
|
20
|
+
cmd.usage && childProgram.usage(cmd.usage);
|
|
21
|
+
cmd.description && childProgram.description(cmd.description);
|
|
22
|
+
registerCommand(childProgram, cmd.commands);
|
|
23
|
+
|
|
24
|
+
program.addCommand(childProgram);
|
|
25
|
+
} else {
|
|
26
|
+
const command = program.command(cmd.command);
|
|
27
|
+
|
|
28
|
+
cmd.usage && command.usage(cmd.usage);
|
|
29
|
+
|
|
30
|
+
cmd.description && command.description(cmd.description);
|
|
31
|
+
|
|
32
|
+
cmd.options?.forEach(opt => command.option(...opt));
|
|
33
|
+
|
|
34
|
+
command.action(cmd.action);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
registerCommand(program, commands);
|
|
40
|
+
|
|
41
|
+
program.on('--help', () => {
|
|
42
|
+
info();
|
|
43
|
+
info(` Run ${chalk.cyan(`${TMS_NAME} <command> --help`)} for detailed usage of given command.`);
|
|
44
|
+
info();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// 捕获未注册的命令
|
|
48
|
+
program
|
|
49
|
+
.arguments('<command>')
|
|
50
|
+
.action((cmd) => {
|
|
51
|
+
program.outputHelp();
|
|
52
|
+
info(` ${chalk.red(`Unknown command ${chalk.yellow(cmd)}.`)}`);
|
|
53
|
+
info();
|
|
54
|
+
suggestCommands(cmd);
|
|
55
|
+
process.exitCode = 1;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (!process.argv.slice(2).length) {
|
|
59
|
+
program.outputHelp();
|
|
60
|
+
process.exit(-1);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
program.parse(process.argv);
|