@tmsfe/tmskit 0.0.17 → 0.0.20
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/CHANGELOG.md +12 -2
- package/dist/index.cjs.js +1421 -1083
- package/package.json +10 -8
- package/src/compile/build.js +2 -2
- package/src/compile/dev.js +35 -26
- package/src/compile/watch.js +31 -12
- package/src/config/constant.js +29 -5
- package/src/config/defaultTmsConfig.js +1 -1
- package/src/core/buildAppJson.js +40 -48
- package/src/core/cache.js +36 -0
- package/src/core/checkDependencies.js +35 -18
- package/src/core/cloneModules.js +6 -8
- package/src/core/handleError.js +7 -1
- package/src/core/npm.js +10 -4
- package/src/core/tmsMpconfig.js +90 -113
- package/src/entry.js +3 -3
- package/src/scripts/create/generator.js +1 -2
- package/src/scripts/create/index.js +73 -32
- package/src/scripts/run/build/index.js +9 -5
- package/src/scripts/run/dev/index.js +21 -16
- package/src/scripts/run/index.js +96 -65
- package/src/scripts/run/init/index.js +14 -15
- package/src/scripts/run/install/index.js +110 -18
- package/src/utils/md5.js +25 -0
- package/src/utils/widgets.js +31 -0
- package/src/core/isInIt.js +0 -65
package/src/core/npm.js
CHANGED
|
@@ -30,7 +30,12 @@ const collectNpmTasksMap = (packageJsonFiles, cacheDir) => {
|
|
|
30
30
|
const npmTasksMap = new Map();
|
|
31
31
|
for (const packageJsonPath of packageJsonFiles) {
|
|
32
32
|
const packageContent = fs.readFileSync(packageJsonPath);
|
|
33
|
-
|
|
33
|
+
let packageJson;
|
|
34
|
+
try {
|
|
35
|
+
packageJson = JSON.parse(packageContent);
|
|
36
|
+
} catch (e) {
|
|
37
|
+
throw new Error(`${packageJsonPath}json解析出现错误:${e}`);
|
|
38
|
+
}
|
|
34
39
|
const md5Obj = {
|
|
35
40
|
dependencies: packageJson.dependencies || {},
|
|
36
41
|
};
|
|
@@ -99,7 +104,7 @@ const collectNpmTasksMap = (packageJsonFiles, cacheDir) => {
|
|
|
99
104
|
|
|
100
105
|
|
|
101
106
|
// 遍历安装指定目录下所有项目的npm依赖
|
|
102
|
-
const
|
|
107
|
+
const npmInstallAll = async (modules, contextDir, cacheDir) => {
|
|
103
108
|
const cwd = process.cwd();
|
|
104
109
|
const packageJsonFiles = await findAllPackageJson(modules, contextDir);
|
|
105
110
|
|
|
@@ -118,12 +123,13 @@ const mpNpmInstallAll = async (modules, contextDir, cacheDir) => {
|
|
|
118
123
|
return Promise.all(callArr);
|
|
119
124
|
})
|
|
120
125
|
.catch((e) => {
|
|
121
|
-
handleError(`npm install ${params.packageJsonPath}出现错误:${e}
|
|
126
|
+
handleError(`npm install ${params.packageJsonPath}出现错误:${e}`, true);
|
|
122
127
|
}));
|
|
123
128
|
});
|
|
124
129
|
|
|
125
130
|
await Promise.all(arrPromises);
|
|
126
131
|
shell.cd(cwd);
|
|
132
|
+
return packageJsonFiles;
|
|
127
133
|
};
|
|
128
134
|
|
|
129
135
|
/**
|
|
@@ -215,7 +221,7 @@ function cloudNpmInstall(contextDir) {
|
|
|
215
221
|
|
|
216
222
|
module.exports = {
|
|
217
223
|
cloudNpmInstall,
|
|
218
|
-
|
|
224
|
+
npmInstallAll,
|
|
219
225
|
findAllPackageJson,
|
|
220
226
|
};
|
|
221
227
|
|
package/src/core/tmsMpconfig.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 用来读取处理tms.config.js与module.config.json字段
|
|
3
3
|
*/
|
|
4
|
+
/* eslint-disable no-param-reassign, no-nested-ternary */
|
|
4
5
|
const loadash = require('lodash');
|
|
5
6
|
const fs = require('fs');
|
|
6
7
|
const { TMS_CONFIG_FILENAME, MODULE_CONFIG_FILENAME, TMS_PRIVATE_FILENAME } = require('../config/constant');
|
|
7
8
|
const { resolve, isObject, isArray } = require('../utils/widgets');
|
|
8
9
|
const defaultTmsConfig = require('../config/defaultTmsConfig');
|
|
9
10
|
const { fail } = require('../utils/log');
|
|
10
|
-
const path = require('path');
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* 读取tms.config.js
|
|
@@ -23,35 +23,11 @@ const readTmsConfig = function (env) {
|
|
|
23
23
|
const tmsConfig = tmsConfigFn({
|
|
24
24
|
env,
|
|
25
25
|
});
|
|
26
|
-
// 合并默认值
|
|
27
|
-
loadash.mergeWith(tmsConfig, defaultTmsConfig);
|
|
28
26
|
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
return tmsConfig;
|
|
27
|
+
// 合并默认值
|
|
28
|
+
return loadash.mergeWith(defaultTmsConfig, tmsConfig);
|
|
32
29
|
};
|
|
33
30
|
|
|
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
31
|
|
|
56
32
|
/**
|
|
57
33
|
* 读取tms.private.config.js
|
|
@@ -62,12 +38,7 @@ const readTmsPrivateCf = function () {
|
|
|
62
38
|
if (fs.existsSync(tmsPrivatePath)) {
|
|
63
39
|
tmsPrivateCf = require(tmsPrivatePath);
|
|
64
40
|
}
|
|
65
|
-
|
|
66
|
-
if (tmsPrivateCf.modules instanceof Array) {
|
|
67
|
-
Object.assign(tmsPrivateCf.modules, {
|
|
68
|
-
include: tmsPrivateCf.modules,
|
|
69
|
-
});
|
|
70
|
-
}
|
|
41
|
+
|
|
71
42
|
return tmsPrivateCf;
|
|
72
43
|
};
|
|
73
44
|
|
|
@@ -80,11 +51,10 @@ const readTmsPrivateCf = function () {
|
|
|
80
51
|
const checkModules = function (tmsConfig, modules, isQuit = false) {
|
|
81
52
|
const targetModules = [];
|
|
82
53
|
modules.forEach((moduleName) => {
|
|
83
|
-
const module = tmsConfig.modules.find(module => module.
|
|
54
|
+
const module = tmsConfig.modules.all.find(module => module.moduleName === moduleName);
|
|
84
55
|
module && targetModules.push(module);
|
|
85
56
|
});
|
|
86
57
|
|
|
87
|
-
|
|
88
58
|
if (targetModules.length === 0) {
|
|
89
59
|
fail(`你启动的模块无效${modules.join(',')}无效,请检查tms.config.json>modules>${modules.join(',')}
|
|
90
60
|
>name字段与module.config.json的name字段是否一致`);
|
|
@@ -110,7 +80,7 @@ function adaptMpCgContent(fileContent, appName) {
|
|
|
110
80
|
return res;
|
|
111
81
|
};
|
|
112
82
|
|
|
113
|
-
let content =
|
|
83
|
+
let content = fileContent;
|
|
114
84
|
|
|
115
85
|
if (isArray(content)) {
|
|
116
86
|
let i = content.length - 1;
|
|
@@ -119,118 +89,125 @@ function adaptMpCgContent(fileContent, appName) {
|
|
|
119
89
|
i--; // eslint-disable-line
|
|
120
90
|
}
|
|
121
91
|
} else {
|
|
122
|
-
if (appName && content.mpConfig && content.mpConfig[appName]) {
|
|
123
|
-
content = { ...content, ...content.mpConfig[appName] };
|
|
124
|
-
delete content.mpConfig;
|
|
125
|
-
delete content.isSubpackages;
|
|
126
|
-
}
|
|
127
92
|
content = handleContent(appName, content);
|
|
128
93
|
}
|
|
129
94
|
return content;
|
|
130
95
|
}
|
|
131
96
|
|
|
132
97
|
/**
|
|
133
|
-
*
|
|
98
|
+
* 获取模块module.config.json中的配置信息
|
|
134
99
|
* @param {array} modules 用户要编译的模块列表
|
|
135
100
|
* @param { string } appName 小程序的名称
|
|
136
|
-
* @param {
|
|
101
|
+
* @param {boolean} notFindIsQuit 找不到配置文件是否退出
|
|
137
102
|
*/
|
|
138
|
-
function
|
|
139
|
-
const modulesConfig =
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if (fs.existsSync(moduleConfigPath)) {
|
|
144
|
-
const content = fs.readFileSync(moduleConfigPath, 'utf-8');
|
|
145
|
-
modulesConfig[moduleConfigPath] = adaptMpCgContent(content, appName);
|
|
103
|
+
function getModulesConfig(modules = [], appName, notFindIsQuit) {
|
|
104
|
+
const modulesConfig = [];
|
|
105
|
+
modules.forEach((moduleItem) => {
|
|
106
|
+
if (!moduleItem.path) {
|
|
107
|
+
throw new Error(`${moduleItem.moduleName}模块路径配置没有找到`);
|
|
146
108
|
}
|
|
109
|
+
const moduleConfigPath = resolve(moduleItem.path, MODULE_CONFIG_FILENAME);
|
|
110
|
+
if (!fs.existsSync(moduleConfigPath)) {
|
|
111
|
+
if (notFindIsQuit) {
|
|
112
|
+
throw new Error(`${moduleItem.moduleName}模块的配置文件module.config.json在${moduleItem.path}目录下没有找到`);
|
|
113
|
+
}
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
let moduleConfig;
|
|
117
|
+
try {
|
|
118
|
+
moduleConfig = JSON.parse(fs.readFileSync(moduleConfigPath, 'utf-8'));
|
|
119
|
+
} catch (e) {
|
|
120
|
+
throw new Error(`${moduleConfigPath}json解析报错: ${e}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 兼容历史逻辑,后续可删除--- start
|
|
124
|
+
if (!moduleItem.moduleName) moduleItem.moduleName = moduleItem.name;
|
|
125
|
+
delete moduleItem.name;
|
|
126
|
+
|
|
127
|
+
let subPackages = isObject(moduleConfig) && moduleConfig.subPackages
|
|
128
|
+
? moduleConfig.subPackages
|
|
129
|
+
: isObject(moduleConfig) ? [moduleConfig] : moduleConfig;
|
|
130
|
+
|
|
131
|
+
subPackages = adaptMpCgContent(subPackages, appName);
|
|
132
|
+
let dependencies = moduleConfig.dependencies || [];
|
|
133
|
+
subPackages.forEach((item) => {
|
|
134
|
+
item.path = moduleItem.path;
|
|
135
|
+
if (item.dependencies) {
|
|
136
|
+
dependencies = dependencies.concat(item.dependencies);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
moduleItem.subPackages = subPackages;
|
|
140
|
+
moduleItem.dependencies = dependencies;
|
|
141
|
+
// 兼容逻辑--- end
|
|
142
|
+
|
|
143
|
+
modulesConfig.push(moduleItem);
|
|
147
144
|
});
|
|
148
145
|
|
|
149
146
|
return modulesConfig;
|
|
150
147
|
}
|
|
151
148
|
|
|
152
149
|
/**
|
|
153
|
-
*
|
|
150
|
+
* 获取分包内容 (读取module.config.json的配置项)
|
|
154
151
|
* @param {array} modules
|
|
155
|
-
* @param {string} appName
|
|
156
|
-
* @param {string} moduleDir
|
|
157
152
|
* @returns
|
|
158
153
|
*/
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
modules.forEach((
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
let moduleConfigContent = fs.readFileSync(moduleConfigPath, 'utf-8');
|
|
167
|
-
moduleConfigContent = adaptMpCgContent(moduleConfigContent, appName);
|
|
168
|
-
const moduleContentArr = isObject(moduleConfigContent) ? [moduleConfigContent] : moduleConfigContent;
|
|
169
|
-
moduleContentArr.forEach(({ name }, moduleContentArrIndex) => {
|
|
170
|
-
if (name === moduleName) {
|
|
171
|
-
findModule = true;
|
|
172
|
-
newModules.push({
|
|
173
|
-
...modules[moduleIndex],
|
|
174
|
-
...moduleContentArr[moduleContentArrIndex],
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
if (!findModule) {
|
|
179
|
-
fail(`启动模块${moduleName}在${moduleConfigPath}没有找到,请检查配置`);
|
|
180
|
-
process.exit(1);
|
|
181
|
-
}
|
|
182
|
-
} catch (e) {
|
|
183
|
-
fail(`${moduleConfigPath}配置错误: ${e}`);
|
|
184
|
-
newModules.push({
|
|
185
|
-
...modules[moduleIndex],
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
} else {
|
|
189
|
-
newModules.push({
|
|
190
|
-
...modules[moduleIndex],
|
|
154
|
+
const getSubPackages = (modules) => {
|
|
155
|
+
const newSubPackages = [];
|
|
156
|
+
modules.forEach((module) => {
|
|
157
|
+
(module.subPackages || []).forEach((item) => {
|
|
158
|
+
newSubPackages.push({
|
|
159
|
+
path: module.path,
|
|
160
|
+
...item,
|
|
191
161
|
});
|
|
192
|
-
}
|
|
162
|
+
});
|
|
193
163
|
});
|
|
194
|
-
return
|
|
164
|
+
return newSubPackages;
|
|
195
165
|
};
|
|
196
166
|
|
|
197
167
|
/**
|
|
198
|
-
*
|
|
168
|
+
* 获取所有的模块,合并模块的依赖模块
|
|
199
169
|
* @param { object } tmsConfig
|
|
200
170
|
* @param {array} modules
|
|
201
|
-
* @param {
|
|
171
|
+
* @param {boolean} notFindIsQuit 找不到配置文件是否退出
|
|
202
172
|
* @returns
|
|
203
173
|
*/
|
|
204
|
-
const
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
174
|
+
const getModulesByMergeDepModules = (tmsConfig, modules, notFindIsQuit = false) => {
|
|
175
|
+
const allModules = new Map();
|
|
176
|
+
function dfs(tmsConfig, modules) {
|
|
177
|
+
modules.forEach((moduleItem) => {
|
|
178
|
+
const [moduleConfig = {}] = getModulesConfig([moduleItem], tmsConfig.appName, notFindIsQuit);
|
|
179
|
+
|
|
180
|
+
if (!allModules.has(moduleItem.moduleName)) {
|
|
181
|
+
allModules.set(moduleItem.moduleName, { ...moduleItem, ...moduleConfig });
|
|
182
|
+
|
|
183
|
+
const dependenciesModules = [];
|
|
184
|
+
(moduleConfig?.dependencies || []).forEach((dependenciesModule) => {
|
|
185
|
+
tmsConfig.modules.all.forEach((module) => {
|
|
186
|
+
if (dependenciesModule === module.moduleName) {
|
|
187
|
+
dependenciesModules.push({ ...module });
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
if (dependenciesModules.length) {
|
|
192
|
+
dfs(tmsConfig, dependenciesModules);
|
|
222
193
|
}
|
|
223
194
|
}
|
|
224
195
|
});
|
|
225
|
-
}
|
|
226
|
-
|
|
196
|
+
}
|
|
197
|
+
dfs(tmsConfig, modules);
|
|
198
|
+
|
|
199
|
+
const modulesArr = [];
|
|
200
|
+
for (const module of allModules.values()) {
|
|
201
|
+
modulesArr.push(module);
|
|
202
|
+
}
|
|
203
|
+
return modulesArr;
|
|
227
204
|
};
|
|
228
205
|
|
|
229
206
|
module.exports = {
|
|
230
207
|
readTmsConfig,
|
|
231
208
|
readTmsPrivateCf,
|
|
232
|
-
|
|
209
|
+
getModulesConfig,
|
|
233
210
|
checkModules,
|
|
234
|
-
|
|
235
|
-
|
|
211
|
+
getSubPackages,
|
|
212
|
+
getModulesByMergeDepModules,
|
|
236
213
|
};
|
package/src/entry.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
module.exports = [
|
|
2
2
|
{
|
|
3
|
-
command: 'create <
|
|
3
|
+
command: 'create <project-name>',
|
|
4
4
|
description: '创建新的应用',
|
|
5
|
-
action: (
|
|
6
|
-
require('./scripts/create')(
|
|
5
|
+
action: (projectName) => {
|
|
6
|
+
require('./scripts/create')(projectName);
|
|
7
7
|
},
|
|
8
8
|
},
|
|
9
9
|
{
|
|
@@ -3,8 +3,7 @@ const render = require('./render');
|
|
|
3
3
|
const ask = require('./ask');
|
|
4
4
|
const FILES_TO_IGNORE = require('./ignoreFiles');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
const generator = (buildDir, distDir, preMetadata) => new Promise((resolve, reject) => {
|
|
6
|
+
const generator = (buildDir, distDir, preMetadata = {}) => new Promise((resolve, reject) => {
|
|
8
7
|
Metalsmith(buildDir)
|
|
9
8
|
.metadata(preMetadata)
|
|
10
9
|
.ignore(FILES_TO_IGNORE)
|
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const fs = require('fs');
|
|
3
3
|
const shelljs = require('shelljs');
|
|
4
|
-
const
|
|
5
|
-
const {
|
|
4
|
+
const inquirer = require('inquirer');
|
|
5
|
+
const {
|
|
6
|
+
TEMPLATE_DIR,
|
|
7
|
+
TEMPLATE_URL,
|
|
8
|
+
TEMPLATE_NAME,
|
|
9
|
+
TEMPLATE_TKIT_DIR,
|
|
10
|
+
CREATE_TEMPLATE_QUESTION,
|
|
11
|
+
} = require('../../config/constant.js');
|
|
12
|
+
const { resolve } = require('../../utils/widgets');
|
|
6
13
|
const io = require('../../utils/io');
|
|
7
14
|
const { fail, succeed, info } = require('../../utils/log');
|
|
8
15
|
const generator = require('./generator');
|
|
16
|
+
const request = require('request');
|
|
17
|
+
const unzip = require('unzipper');
|
|
9
18
|
|
|
10
19
|
/**
|
|
11
20
|
* 如果该目录下面存在文件,换个名字
|
|
12
21
|
* @param { string } targetDir 当前文件夹
|
|
13
22
|
* @returns { undefined }
|
|
14
23
|
*/
|
|
15
|
-
async function
|
|
24
|
+
async function createProjectDir(targetDir) {
|
|
16
25
|
// 如果目录非空或者已经存在,提示用户,做选择
|
|
17
26
|
if (fs.existsSync(targetDir)) {
|
|
18
27
|
if (!await io.isDirEmpty(targetDir)) {
|
|
@@ -24,43 +33,75 @@ async function createAppDir(targetDir) {
|
|
|
24
33
|
}
|
|
25
34
|
}
|
|
26
35
|
|
|
36
|
+
/**
|
|
37
|
+
* 下载和解压远程的小程序模板
|
|
38
|
+
* @param {string} templateDir
|
|
39
|
+
* @param {string} templateUrl
|
|
40
|
+
* @param {string} templateName
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
function downloadAndUnZipTemplate(templateDir, templateUrl, templateName) {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const localZipPath = `${templateDir}/${templateName}.zip`;
|
|
46
|
+
const stream = fs.createWriteStream(localZipPath);
|
|
47
|
+
request(`${templateUrl}?v=${new Date().getTime()}`).pipe(stream)
|
|
48
|
+
.on('close', (err) => {
|
|
49
|
+
if (err) {
|
|
50
|
+
reject(err);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
fs.createReadStream(localZipPath).pipe(unzip.Extract({ path: templateDir }))
|
|
54
|
+
.on('close', (err) => {
|
|
55
|
+
if (err) {
|
|
56
|
+
reject(err);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
resolve();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
27
65
|
/**
|
|
28
66
|
* 创建本地小程序运行环境
|
|
29
|
-
* @param { string }
|
|
30
|
-
* @param { string }
|
|
67
|
+
* @param { string } projectType 项目类型
|
|
68
|
+
* @param { string } projectName 项目名称
|
|
31
69
|
*/
|
|
32
|
-
async function create(
|
|
70
|
+
async function create(projectName) {
|
|
33
71
|
const cwd = process.cwd();
|
|
34
|
-
const targetDir = path.resolve(cwd,
|
|
35
|
-
const
|
|
36
|
-
await createAppDir(targetDir);
|
|
72
|
+
const targetDir = path.resolve(cwd, projectName);
|
|
73
|
+
const { projectType } = await inquirer.prompt(CREATE_TEMPLATE_QUESTION);
|
|
37
74
|
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
TEMPLATE_DIR
|
|
44
|
-
|
|
45
|
-
);
|
|
75
|
+
// 创建项目目录
|
|
76
|
+
await createProjectDir(targetDir);
|
|
77
|
+
|
|
78
|
+
// 新创建缓存目录
|
|
79
|
+
if (fs.existsSync(TEMPLATE_DIR)) {
|
|
80
|
+
shelljs.rm('-rf', TEMPLATE_DIR);
|
|
81
|
+
}
|
|
82
|
+
fs.mkdirSync(TEMPLATE_DIR, { recursive: true });
|
|
83
|
+
|
|
84
|
+
// 下载和解压模板
|
|
85
|
+
await downloadAndUnZipTemplate(TEMPLATE_DIR, TEMPLATE_URL, TEMPLATE_NAME);
|
|
46
86
|
|
|
47
87
|
// 生成模板(1. 询问问题, 2. ejs生成模板 3.生成到目标目录)
|
|
48
|
-
generator(path.join(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
}
|
|
88
|
+
generator(path.join(TEMPLATE_DIR, TEMPLATE_NAME, projectType), targetDir).then(() => {
|
|
89
|
+
shelljs.cd(projectName);
|
|
90
|
+
const hookFilePath = resolve(projectName, TEMPLATE_TKIT_DIR, 'hooks.js');
|
|
91
|
+
if (fs.existsSync(hookFilePath)) {
|
|
92
|
+
const hooks = require(hookFilePath);
|
|
93
|
+
if (hooks.afterCreate) {
|
|
94
|
+
hooks.afterCreate.forEach((item) => {
|
|
95
|
+
if (typeof item === 'function') {
|
|
96
|
+
item.call(null, shelljs, { projectName });
|
|
97
|
+
} else {
|
|
98
|
+
shelljs.exec(item);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
shelljs.rm('-rf', resolve(projectName, TEMPLATE_TKIT_DIR));
|
|
62
103
|
}
|
|
63
|
-
|
|
104
|
+
|
|
64
105
|
succeed('项目创建完成.');
|
|
65
106
|
})
|
|
66
107
|
.catch((err) => {
|
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
const shelljs = require('shelljs');
|
|
2
|
-
const { resolve } = require('../../../utils/widgets');
|
|
2
|
+
const { resolve, filterField } = require('../../../utils/widgets');
|
|
3
3
|
const init = require('../init/index');
|
|
4
4
|
const compileBuild = require('../../../compile/build');
|
|
5
5
|
|
|
6
|
-
async function build(tmsConfig, targetModules
|
|
6
|
+
async function build(tmsConfig, targetModules) {
|
|
7
7
|
// 开始构建前,清理输出目录
|
|
8
8
|
await shelljs.rm('-rf', resolve(tmsConfig.outputDir));
|
|
9
9
|
|
|
10
|
-
const {
|
|
10
|
+
const { modules: newModules, subPackages } = await init(tmsConfig, targetModules);
|
|
11
11
|
|
|
12
|
+
const isDev = false;
|
|
12
13
|
if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
|
|
13
|
-
await tmsConfig?.hooks?.beforeCompile({
|
|
14
|
+
await tmsConfig?.hooks?.beforeCompile({
|
|
15
|
+
isDev,
|
|
16
|
+
tmsConfig: filterField(tmsConfig, ['gitAccount']),
|
|
17
|
+
modules: newModules });
|
|
14
18
|
};
|
|
15
|
-
compileBuild(tmsConfig, newModules,
|
|
19
|
+
compileBuild(tmsConfig, newModules, subPackages, isDev);
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
module.exports = build;
|
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
const shelljs = require('shelljs');
|
|
2
2
|
const compileDev = require('../../../compile/dev');
|
|
3
|
-
const { resolve } = require('../../../utils/widgets');
|
|
3
|
+
const { resolve, filterField } = require('../../../utils/widgets');
|
|
4
4
|
const init = require('../init/index');
|
|
5
|
-
const {
|
|
5
|
+
const { getModulesByMergeDepModules, getSubPackages } = require('../../../core/tmsMpconfig');
|
|
6
6
|
const { info } = require('../../../utils/log');
|
|
7
7
|
const { global } = require('../../../utils/global');
|
|
8
8
|
const { CACHE_DIR } = require('../../../config/constant');
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
// 用户编译分包时,需要将dist中其他分包(主包不能删除)的内容删除,否则其他分包的内容混入到主包(导致主包的体积超2M)
|
|
12
|
-
function
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
function delOtherPackages(tmsConfig, targetSubPackages) {
|
|
13
|
+
// 获取所有模块,合并模块依赖的模块
|
|
14
|
+
const allModules = getModulesByMergeDepModules(tmsConfig, tmsConfig.modules.all);
|
|
15
|
+
// 获取所有的分包
|
|
16
|
+
const allSubPackages = getSubPackages(allModules);
|
|
17
|
+
const targetSubPackagesName = targetSubPackages.map(item => item.name);
|
|
18
|
+
allSubPackages.forEach((item) => {
|
|
19
|
+
if (item.root && targetSubPackagesName.indexOf(item.name) === -1) {
|
|
20
|
+
const moduleRootDir = resolve(`${tmsConfig.outputDir}/${item.root}`);
|
|
18
21
|
shelljs.rm('-rf', `${moduleRootDir}/*`, { silent: true });
|
|
19
22
|
// 解决微信开发者工具(dist/app.json: ["subpackages"][0]["root"] 字段需为 目录)错误 - 提前创建该目录
|
|
20
23
|
// io.ensureDirExist(moduleRootDir);
|
|
@@ -22,8 +25,7 @@ function delOtherModule(tmsConfig, targetModules) {
|
|
|
22
25
|
});
|
|
23
26
|
}
|
|
24
27
|
|
|
25
|
-
async function dev(tmsConfig, targetModules
|
|
26
|
-
let newModules = targetModules;
|
|
28
|
+
async function dev(tmsConfig, targetModules) {
|
|
27
29
|
const { noCache } = global.getData('cmd');
|
|
28
30
|
if (noCache) {
|
|
29
31
|
shelljs.rm('-rf', resolve(tmsConfig.outputDir));
|
|
@@ -31,15 +33,18 @@ async function dev(tmsConfig, targetModules, env) {
|
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
// 初始化操作
|
|
34
|
-
const
|
|
35
|
-
newModules = initData.targetModules;
|
|
36
|
+
const { subPackages, modules: newModules } = await init(tmsConfig, targetModules);
|
|
36
37
|
|
|
37
|
-
info('当前dev启动的有效模块', newModules.map(item => item.
|
|
38
|
+
info('当前dev启动的有效模块', newModules.map(item => item.moduleName).sort());
|
|
38
39
|
if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
|
|
39
|
-
await tmsConfig?.hooks?.beforeCompile({
|
|
40
|
+
await tmsConfig?.hooks?.beforeCompile({
|
|
41
|
+
isDev: true,
|
|
42
|
+
tmsConfig: filterField(tmsConfig, ['gitAccount']),
|
|
43
|
+
modules: newModules,
|
|
44
|
+
});
|
|
40
45
|
};
|
|
41
|
-
|
|
42
|
-
compileDev(tmsConfig, newModules,
|
|
46
|
+
delOtherPackages(tmsConfig, subPackages);
|
|
47
|
+
compileDev(tmsConfig, newModules, subPackages, true);
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
module.exports = dev;
|