@tmsfe/tmskit 0.0.15 → 0.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.
- package/CHANGELOG.md +20 -2
- package/README.md +2 -3
- package/build/publish.sh +34 -0
- package/dist/index.cjs.js +964 -602
- package/package.json +13 -2
- package/src/compile/compile.js +56 -35
- package/src/compile/dev.js +40 -31
- package/src/compile/plugins/gulp-watch/index.js +172 -0
- package/src/compile/plugins/mpJsonDep.js +1 -1
- package/src/compile/watch.js +51 -15
- package/src/config/constant.js +1 -3
- package/src/config/defaultTmsConfig.js +1 -1
- package/src/core/buildAppJson.js +43 -44
- package/src/core/cloneModules.js +3 -3
- package/src/core/tmsMpconfig.js +19 -57
- package/src/entry.js +12 -1
- package/src/scripts/run/build/index.js +7 -1
- package/src/scripts/run/cloud/index.js +12 -0
- package/src/scripts/run/dev/index.js +18 -10
- package/src/scripts/run/index.js +37 -16
- package/src/scripts/run/init/index.js +27 -15
- package/src/scripts/run/install/index.js +19 -12
- package/src/utils/io.js +20 -0
- package/src/utils/log.js +4 -4
- package/src/utils/widgets.js +20 -3
package/src/core/buildAppJson.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 生成编译后的app.json
|
|
3
3
|
*/
|
|
4
|
+
/* eslint-disable no-param-reassign */
|
|
4
5
|
const fs = require('fs');
|
|
5
|
-
const { MODULE_CONFIG_FILENAME
|
|
6
|
-
const { getModuleConfig
|
|
6
|
+
const { MODULE_CONFIG_FILENAME } = require('../config/constant');
|
|
7
|
+
const { getModuleConfig } = require('./tmsMpconfig');
|
|
7
8
|
const { fail } = require('../utils/log');
|
|
8
|
-
const { resolve, isObject } = require('../utils/widgets');
|
|
9
|
+
const { resolve, isObject, filterField } = require('../utils/widgets');
|
|
9
10
|
const { handleError } = require('./handleError');
|
|
11
|
+
const { global } = require('../utils/global');
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
14
|
* 更新appJson里面的主包配置
|
|
@@ -36,7 +38,6 @@ function updateMainPackages(appJson, mainPackages = []) {
|
|
|
36
38
|
|
|
37
39
|
// 去掉 subpackages 中的主包配置
|
|
38
40
|
const foundMainPackageNames = foundMainPackages.map(item => item.name);
|
|
39
|
-
// eslint-disable-next-line
|
|
40
41
|
appJson.subpackages = appJson.subpackages.filter(subpackage => !foundMainPackageNames.includes(subpackage.name));
|
|
41
42
|
|
|
42
43
|
return appJson;
|
|
@@ -55,24 +56,20 @@ const getAppJsonContent = (sourceAppJsonPath) => {
|
|
|
55
56
|
// 加入默认值
|
|
56
57
|
appJson.subpackages = [];
|
|
57
58
|
appJson.pages = [];
|
|
58
|
-
// appJson.plugins = {};
|
|
59
|
-
delete appJson.entranceDeclare;
|
|
60
59
|
return appJson;
|
|
61
60
|
};
|
|
62
61
|
/**
|
|
63
62
|
* 更新app.json中的subpackages
|
|
64
63
|
* @param {Object} appJson
|
|
65
|
-
* @param {Object}
|
|
64
|
+
* @param {Object} modulesConfigs
|
|
66
65
|
*/
|
|
67
|
-
const updateSubpackages = (appJson,
|
|
66
|
+
const updateSubpackages = (appJson, modulesConfigs) => {
|
|
68
67
|
// eslint-disable-next-line
|
|
69
|
-
for (const
|
|
70
|
-
const moduleInfo = isObject(
|
|
71
|
-
|
|
72
|
-
const validModules = getValidModules(moduleInfo);
|
|
73
|
-
// eslint-disable-next-line
|
|
74
|
-
appJson.subpackages = appJson.subpackages.concat(validModules);
|
|
68
|
+
for (const modulePath in modulesConfigs) {
|
|
69
|
+
const moduleInfo = isObject(modulesConfigs[modulePath]) ? [modulesConfigs[modulePath]] : modulesConfigs[modulePath];
|
|
70
|
+
appJson.subpackages = appJson.subpackages.concat(moduleInfo);
|
|
75
71
|
}
|
|
72
|
+
appJson.subpackages.sort((item1, item2) => item1.name.localeCompare(item2.name));
|
|
76
73
|
};
|
|
77
74
|
|
|
78
75
|
/**
|
|
@@ -84,40 +81,35 @@ const fixAppJson = (appJson) => {
|
|
|
84
81
|
const pluginsMap = {};
|
|
85
82
|
Object.keys(appJson.plugins || {}).forEach(key => pluginsMap[key] = ['app.json']);
|
|
86
83
|
const subps = subpackages.map((subp) => {
|
|
87
|
-
const
|
|
84
|
+
const arrOfFileType = ['requiredBackgroundModes', 'embeddedAppIdList'];
|
|
85
|
+
const objOfFileType = ['preloadRule'];
|
|
88
86
|
Object.keys(subp).forEach((key) => {
|
|
89
|
-
if (key === 'dependencies') {
|
|
90
|
-
// eslint-disable-next-line
|
|
91
|
-
delete subp.dependencies;
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
87
|
if (key === 'plugins') {
|
|
95
88
|
Object.keys(subp.plugins).forEach((pk) => {
|
|
96
|
-
pluginsMap[pk]
|
|
89
|
+
pluginsMap[pk]
|
|
90
|
+
? pluginsMap[pk].push(`分包${subp.name}`)
|
|
91
|
+
: pluginsMap[pk] = [`分包${subp.name}`];
|
|
97
92
|
});
|
|
98
|
-
return;
|
|
99
93
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
invalidKeys.push(key);
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
if (['requiredBackgroundModes', 'embeddedAppIdList'].indexOf(key) > -1) {
|
|
106
|
-
// 提到appjson最上层处理
|
|
94
|
+
// 分包里数组类型字段,提到appjson最上层
|
|
95
|
+
if (arrOfFileType.indexOf(key) > -1) {
|
|
107
96
|
const preVal = appJson[key];
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
97
|
+
preVal
|
|
98
|
+
? appJson[key] = Array.from(new Set(preVal.slice(0).concat(subp[key])))
|
|
99
|
+
: appJson[key] = subp[key].slice(0);
|
|
100
|
+
}
|
|
101
|
+
// 分包里对象类型字段,提到appjson最上层
|
|
102
|
+
if (objOfFileType.indexOf(key) > -1) {
|
|
103
|
+
const preloadRuleMap = appJson[key] || {};
|
|
104
|
+
subp[key] && Object.keys(subp[key]).forEach((page) => {
|
|
105
|
+
if (!preloadRuleMap[page]) {
|
|
106
|
+
preloadRuleMap[page] = subp[key][page];
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
appJson[key] = preloadRuleMap;
|
|
113
110
|
}
|
|
114
111
|
});
|
|
115
|
-
|
|
116
|
-
fail(`不支持分包${subp?.name}配置${invalidKeys.join(',')}\n`);
|
|
117
|
-
}
|
|
118
|
-
// eslint-disable-next-line
|
|
119
|
-
invalidKeys.concat(['requiredBackgroundModes', 'embeddedAppIdList']).forEach(k => delete subp[k]);
|
|
120
|
-
return subp;
|
|
112
|
+
return filterField(subp, [...arrOfFileType, ...objOfFileType, 'dependencies']);
|
|
121
113
|
});
|
|
122
114
|
// 如果plugins重复,则错误提示
|
|
123
115
|
const pluginsErrMsg = Object.keys(pluginsMap).map((pk) => {
|
|
@@ -128,9 +120,8 @@ const fixAppJson = (appJson) => {
|
|
|
128
120
|
})
|
|
129
121
|
.reduce((pre, cur) => pre + cur, '');
|
|
130
122
|
if (pluginsErrMsg) {
|
|
131
|
-
|
|
123
|
+
throw new Error(`plugins配置出现错误:${pluginsErrMsg}`);
|
|
132
124
|
}
|
|
133
|
-
// eslint-disable-next-line
|
|
134
125
|
appJson.subpackages = subps;
|
|
135
126
|
};
|
|
136
127
|
|
|
@@ -143,11 +134,11 @@ const fixAppJson = (appJson) => {
|
|
|
143
134
|
function buildOutputAppJson(tmsConfig, modules) {
|
|
144
135
|
try {
|
|
145
136
|
// 获取当前 modules 下的所有子模块的配置内容
|
|
146
|
-
const
|
|
137
|
+
const modulesConfigs = getModuleConfig(modules, tmsConfig.appName, MODULE_CONFIG_FILENAME);
|
|
147
138
|
// 获取app.json的配置
|
|
148
139
|
const appJson = getAppJsonContent(resolve('./app.json'));
|
|
149
140
|
// 更新app.json中的subpackages
|
|
150
|
-
updateSubpackages(appJson,
|
|
141
|
+
updateSubpackages(appJson, modulesConfigs);
|
|
151
142
|
// 处理appJson中重复||冲突的地方
|
|
152
143
|
fixAppJson(appJson);
|
|
153
144
|
// 更新主包,需在subpackages处理完成后执行, pages/
|
|
@@ -155,6 +146,14 @@ function buildOutputAppJson(tmsConfig, modules) {
|
|
|
155
146
|
|
|
156
147
|
fs.writeFileSync(resolve(`${tmsConfig.outputDir}/app.json`), JSON.stringify(appJson, null, 2), 'utf8');
|
|
157
148
|
|
|
149
|
+
if (typeof tmsConfig?.hooks?.updateAppJson === 'function') {
|
|
150
|
+
tmsConfig?.hooks?.updateAppJson({
|
|
151
|
+
tmsConfig: filterField(tmsConfig, ['gitAccount']),
|
|
152
|
+
modules,
|
|
153
|
+
appJson,
|
|
154
|
+
isDev: global.getData('isDev'),
|
|
155
|
+
});
|
|
156
|
+
}
|
|
158
157
|
return appJson;
|
|
159
158
|
} catch (e) {
|
|
160
159
|
handleError(`生成app.json出现错误: ${e}`);
|
package/src/core/cloneModules.js
CHANGED
|
@@ -18,11 +18,11 @@ const { global } = require('../utils/global');
|
|
|
18
18
|
*/
|
|
19
19
|
function replaceGitUrlAccount(httpRepoUrl, moduleName) {
|
|
20
20
|
// 用户本地的私有项目配置(用来配置环境\模块信息\账号信息)
|
|
21
|
-
const
|
|
21
|
+
const tmsConfig = global.getData('tmsConfig');
|
|
22
22
|
|
|
23
23
|
let gitUrl = httpRepoUrl;
|
|
24
|
-
const { username = '', pass = '' } =
|
|
25
|
-
||
|
|
24
|
+
const { username = '', pass = '' } = tmsConfig?.gitAccout?.[moduleName]
|
|
25
|
+
|| tmsConfig?.gitAccount?.[moduleName]
|
|
26
26
|
|| {};
|
|
27
27
|
|
|
28
28
|
const urlPrefixReg = /http(s)?:\/\//;
|
package/src/core/tmsMpconfig.js
CHANGED
|
@@ -7,7 +7,6 @@ const { TMS_CONFIG_FILENAME, MODULE_CONFIG_FILENAME, TMS_PRIVATE_FILENAME } = re
|
|
|
7
7
|
const { resolve, isObject, isArray } = require('../utils/widgets');
|
|
8
8
|
const defaultTmsConfig = require('../config/defaultTmsConfig');
|
|
9
9
|
const { fail } = require('../utils/log');
|
|
10
|
-
const path = require('path');
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
12
|
* 读取tms.config.js
|
|
@@ -26,32 +25,9 @@ const readTmsConfig = function (env) {
|
|
|
26
25
|
// 合并默认值
|
|
27
26
|
loadash.mergeWith(tmsConfig, defaultTmsConfig);
|
|
28
27
|
|
|
29
|
-
// modules兼容处理
|
|
30
|
-
tmsConfig.modules = convertModules(tmsConfig.modules);
|
|
31
28
|
return 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.name === moduleName);
|
|
54
|
+
const module = tmsConfig.modules.all.find(module => module.name === 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字段是否一致`);
|
|
@@ -93,40 +63,33 @@ const checkModules = function (tmsConfig, modules, isQuit = false) {
|
|
|
93
63
|
return targetModules;
|
|
94
64
|
};
|
|
95
65
|
|
|
96
|
-
/**
|
|
97
|
-
* 过滤页面为空的分包
|
|
98
|
-
* @param {Array} moduleCfg 模块配置内容
|
|
99
|
-
* @returns pages不为空的分包
|
|
100
|
-
*/
|
|
101
|
-
const getValidModules = (moduleCfg) => {
|
|
102
|
-
// 过滤 pages 为空的情况
|
|
103
|
-
const validModules = moduleCfg.filter(item => item.pages.length > 0);
|
|
104
|
-
return validModules;
|
|
105
|
-
};
|
|
106
|
-
|
|
107
66
|
/**
|
|
108
67
|
* 适配处理module.config.json的字段
|
|
109
68
|
* @param { object } fileContent module.config.json的内容
|
|
110
69
|
* @param { string } appName 小程序的名称
|
|
111
70
|
*/
|
|
112
71
|
function adaptMpCgContent(fileContent, appName) {
|
|
113
|
-
const
|
|
72
|
+
const handleContent = function (appName, current) {
|
|
73
|
+
let res = current;
|
|
74
|
+
if (appName && current.mpConfig && current.mpConfig[appName]) {
|
|
75
|
+
res = { ...current, ...current.mpConfig[appName] };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
delete res.mpConfig;
|
|
79
|
+
delete res.isSubpackages;
|
|
80
|
+
return res;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
let content = JSON.parse(fileContent);
|
|
114
84
|
|
|
115
85
|
if (isArray(content)) {
|
|
116
86
|
let i = content.length - 1;
|
|
117
87
|
while (i >= 0) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (appName && current.mpConfig && current.mpConfig[appName]) {
|
|
121
|
-
current = { ...current, ...current.mpConfig[appName] };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
delete current.mpConfig;
|
|
125
|
-
delete current.isSubpackages;
|
|
126
|
-
|
|
127
|
-
content[i] = current;
|
|
88
|
+
content[i] = handleContent(appName, content[i]);
|
|
128
89
|
i--; // eslint-disable-line
|
|
129
90
|
}
|
|
91
|
+
} else {
|
|
92
|
+
content = handleContent(appName, content);
|
|
130
93
|
}
|
|
131
94
|
return content;
|
|
132
95
|
}
|
|
@@ -168,7 +131,7 @@ const tmsModulesMergeLocalModuleCfg = (modules, appName) => {
|
|
|
168
131
|
let moduleConfigContent = fs.readFileSync(moduleConfigPath, 'utf-8');
|
|
169
132
|
moduleConfigContent = adaptMpCgContent(moduleConfigContent, appName);
|
|
170
133
|
const moduleContentArr = isObject(moduleConfigContent) ? [moduleConfigContent] : moduleConfigContent;
|
|
171
|
-
|
|
134
|
+
moduleContentArr.forEach(({ name }, moduleContentArrIndex) => {
|
|
172
135
|
if (name === moduleName) {
|
|
173
136
|
findModule = true;
|
|
174
137
|
newModules.push({
|
|
@@ -178,7 +141,7 @@ const tmsModulesMergeLocalModuleCfg = (modules, appName) => {
|
|
|
178
141
|
}
|
|
179
142
|
});
|
|
180
143
|
if (!findModule) {
|
|
181
|
-
fail(`启动模块${moduleName}在${moduleConfigPath}
|
|
144
|
+
fail(`启动模块${moduleName}在${moduleConfigPath}没有找到,请检查配置`);
|
|
182
145
|
process.exit(1);
|
|
183
146
|
}
|
|
184
147
|
} catch (e) {
|
|
@@ -232,7 +195,6 @@ module.exports = {
|
|
|
232
195
|
readTmsConfig,
|
|
233
196
|
readTmsPrivateCf,
|
|
234
197
|
getModuleConfig,
|
|
235
|
-
getValidModules,
|
|
236
198
|
checkModules,
|
|
237
199
|
tmsModulesMergeLocalModuleCfg,
|
|
238
200
|
subModulesMergeDepModules,
|
package/src/entry.js
CHANGED
|
@@ -28,12 +28,23 @@ module.exports = [
|
|
|
28
28
|
options: [
|
|
29
29
|
['-m, --module [moduleName]', '模块名称'],
|
|
30
30
|
['-e, --env [env]', '环境变量'],
|
|
31
|
-
['-
|
|
31
|
+
['-noCache, --noCache', '不使用缓存功能'],
|
|
32
32
|
],
|
|
33
33
|
action: (cmd) => {
|
|
34
34
|
require('./scripts/run/index')('dev', cmd);
|
|
35
35
|
},
|
|
36
36
|
},
|
|
37
|
+
{
|
|
38
|
+
command: 'cloud',
|
|
39
|
+
description: '云函数开发',
|
|
40
|
+
options: [
|
|
41
|
+
['-m, --module [moduleName]', '模块名称'],
|
|
42
|
+
['-e, --env [env]', '环境变量'],
|
|
43
|
+
],
|
|
44
|
+
action: (cmd) => {
|
|
45
|
+
require('./scripts/run/index')('cloud', cmd);
|
|
46
|
+
},
|
|
47
|
+
},
|
|
37
48
|
{
|
|
38
49
|
command: 'build',
|
|
39
50
|
description: 'prod 打包编译',
|
|
@@ -1,5 +1,5 @@
|
|
|
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
|
|
|
@@ -9,6 +9,12 @@ async function build(tmsConfig, targetModules, env) {
|
|
|
9
9
|
|
|
10
10
|
const { targetModules: newModules } = await init(tmsConfig, targetModules);
|
|
11
11
|
|
|
12
|
+
if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
|
|
13
|
+
await tmsConfig?.hooks?.beforeCompile({
|
|
14
|
+
isDev: false,
|
|
15
|
+
tmsConfig: filterField(tmsConfig, ['gitAccount']),
|
|
16
|
+
modules: newModules });
|
|
17
|
+
};
|
|
12
18
|
compileBuild(tmsConfig, newModules, env);
|
|
13
19
|
}
|
|
14
20
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const { symLink } = require('../../../core/symbolicLink');
|
|
2
|
+
const { handleError } = require('../../../core/handleError');
|
|
3
|
+
const { succeed } = require('../../../utils/log');
|
|
4
|
+
|
|
5
|
+
module.exports = async (tmsConfig) => {
|
|
6
|
+
try {
|
|
7
|
+
await symLink(tmsConfig);
|
|
8
|
+
succeed('云函数创建软链成功');
|
|
9
|
+
} catch (e) {
|
|
10
|
+
handleError(`创建软链错误: ${e}`);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
@@ -1,16 +1,16 @@
|
|
|
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 { isInit } = require('../../../core/isInIt');
|
|
6
5
|
const { tmsModulesMergeLocalModuleCfg } = require('../../../core/tmsMpconfig');
|
|
7
6
|
const { info } = require('../../../utils/log');
|
|
8
7
|
const { global } = require('../../../utils/global');
|
|
8
|
+
const { CACHE_DIR } = require('../../../config/constant');
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
// 用户编译分包时,需要将dist中其他分包(主包不能删除)的内容删除,否则其他分包的内容混入到主包(导致主包的体积超2M)
|
|
12
12
|
function delOtherModule(tmsConfig, targetModules) {
|
|
13
|
-
const modules = tmsModulesMergeLocalModuleCfg(tmsConfig.modules, tmsConfig.appName);
|
|
13
|
+
const modules = tmsModulesMergeLocalModuleCfg(tmsConfig.modules.all, tmsConfig.appName);
|
|
14
14
|
const targetModulesName = targetModules.map(item => item.name);
|
|
15
15
|
modules.forEach((item) => {
|
|
16
16
|
if (item.root && targetModulesName.indexOf(item.name) === -1) {
|
|
@@ -24,16 +24,24 @@ function delOtherModule(tmsConfig, targetModules) {
|
|
|
24
24
|
|
|
25
25
|
async function dev(tmsConfig, targetModules, env) {
|
|
26
26
|
let newModules = targetModules;
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
// init函数 下载第三方代码后,会将最新的tms.config.js的modules 合并 module.config.json的配置项返回
|
|
32
|
-
const initData = await init(tmsConfig, newModules);
|
|
33
|
-
newModules = initData.targetModules;
|
|
27
|
+
const { noCache } = global.getData('cmd');
|
|
28
|
+
if (noCache) {
|
|
29
|
+
shelljs.rm('-rf', resolve(tmsConfig.outputDir));
|
|
30
|
+
shelljs.rm('-rf', CACHE_DIR);
|
|
34
31
|
}
|
|
35
32
|
|
|
33
|
+
// 初始化操作
|
|
34
|
+
const initData = await init(tmsConfig, newModules);
|
|
35
|
+
newModules = initData.targetModules;
|
|
36
|
+
|
|
36
37
|
info('当前dev启动的有效模块', newModules.map(item => item.name).sort());
|
|
38
|
+
if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
|
|
39
|
+
await tmsConfig?.hooks?.beforeCompile({
|
|
40
|
+
isDev: true,
|
|
41
|
+
tmsConfig: filterField(tmsConfig, ['gitAccount']),
|
|
42
|
+
modules: newModules,
|
|
43
|
+
});
|
|
44
|
+
};
|
|
37
45
|
delOtherModule(tmsConfig, newModules);
|
|
38
46
|
compileDev(tmsConfig, newModules, env);
|
|
39
47
|
}
|
package/src/scripts/run/index.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
const loadash = require('lodash');
|
|
1
3
|
const init = require('./init/index');
|
|
2
4
|
const dev = require('./dev/index');
|
|
3
5
|
const build = require('./build/index');
|
|
4
6
|
const install = require('./install/index');
|
|
7
|
+
const cloud = require('./cloud/index');
|
|
5
8
|
const { global } = require('../../utils/global');
|
|
6
9
|
const {
|
|
7
10
|
readTmsConfig,
|
|
@@ -24,34 +27,50 @@ const handleModuleArg = (cmd) => {
|
|
|
24
27
|
* @param {Object} modulePrivateCfg 私有配置里的模块
|
|
25
28
|
* @param {Array} moduleAll 当前小程序全部模块
|
|
26
29
|
*/
|
|
27
|
-
const getSpecificModules = (moduleArg,
|
|
30
|
+
const getSpecificModules = (moduleArg, modules) => {
|
|
28
31
|
if (moduleArg.length > 0) {
|
|
29
32
|
return moduleArg;
|
|
30
33
|
}
|
|
34
|
+
const { all, include, exclude, blockRemote } = modules;
|
|
31
35
|
// 单模块或多模块开发-用户在tms.private.js指定的模块
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return moduleAll.filter(module => !modulePrivateCfg.exclude.includes(module.name)).map(item => item.name);
|
|
38
|
-
}
|
|
39
|
-
if (modulePrivateCfg.blockRemote === true) {
|
|
40
|
-
return moduleAll.filter(module => module.repoInfo === undefined).map(item => item.name);
|
|
41
|
-
}
|
|
36
|
+
if (include?.length > 0) {
|
|
37
|
+
return include;
|
|
38
|
+
}
|
|
39
|
+
if (exclude?.length > 0) {
|
|
40
|
+
return all.filter(module => !exclude.includes(module.name)).map(item => item.name);
|
|
42
41
|
}
|
|
43
|
-
|
|
42
|
+
if (blockRemote === true) {
|
|
43
|
+
return all.filter(module => module.repoInfo === undefined).map(item => item.name);
|
|
44
|
+
}
|
|
45
|
+
return all.map(item => item.name);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const mergeConfig = (tmsConfig, tmsPrivateCf) => {
|
|
49
|
+
const modules = {};
|
|
50
|
+
if (Array.isArray(tmsConfig.modules)) {
|
|
51
|
+
modules.all = tmsConfig.modules;
|
|
52
|
+
tmsConfig.modules = modules;
|
|
53
|
+
}
|
|
54
|
+
// 合并默认值
|
|
55
|
+
const res = loadash.mergeWith(tmsConfig, tmsPrivateCf, (objValue, srcValue) => {
|
|
56
|
+
if (loadash.isArray(objValue) && objValue[0] && loadash.isObject(objValue[0])) {
|
|
57
|
+
return objValue.concat(srcValue);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return res;
|
|
44
62
|
};
|
|
45
63
|
|
|
46
64
|
async function run(commandName, cmd) {
|
|
47
|
-
//
|
|
65
|
+
// 用户本地的私有项目配置
|
|
48
66
|
const tmsPrivateCf = readTmsPrivateCf();
|
|
49
67
|
const { env = tmsPrivateCf?.env } = cmd;
|
|
50
|
-
|
|
68
|
+
let tmsConfig = readTmsConfig(env);
|
|
69
|
+
tmsConfig = mergeConfig(tmsConfig, tmsPrivateCf);
|
|
70
|
+
|
|
51
71
|
// 处理module参数
|
|
52
72
|
const specificModules = getSpecificModules(
|
|
53
73
|
handleModuleArg(cmd),
|
|
54
|
-
tmsPrivateCf.modules,
|
|
55
74
|
tmsConfig.modules,
|
|
56
75
|
);
|
|
57
76
|
|
|
@@ -75,7 +94,6 @@ async function run(commandName, cmd) {
|
|
|
75
94
|
global.setData({
|
|
76
95
|
env,
|
|
77
96
|
cmd,
|
|
78
|
-
tmsPrivateCf,
|
|
79
97
|
tmsConfig,
|
|
80
98
|
});
|
|
81
99
|
|
|
@@ -87,6 +105,9 @@ async function run(commandName, cmd) {
|
|
|
87
105
|
global.setData('isDev', true);
|
|
88
106
|
dev(tmsConfig, newModules, env);
|
|
89
107
|
return;
|
|
108
|
+
case 'cloud':
|
|
109
|
+
cloud(tmsConfig, env);
|
|
110
|
+
return;
|
|
90
111
|
case 'install':
|
|
91
112
|
install(tmsConfig, newModules, env);
|
|
92
113
|
return;
|
|
@@ -8,6 +8,7 @@ const { cloneModules } = require('../../../core/cloneModules');
|
|
|
8
8
|
const { tmsModulesMergeLocalModuleCfg, subModulesMergeDepModules } = require('../../../core/tmsMpconfig');
|
|
9
9
|
const { fail, info } = require('../../../utils/log');
|
|
10
10
|
const install = require('../install');
|
|
11
|
+
const { checkDependencies } = require('../../../core/checkDependencies');
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* 拷贝相关配置文件到编译输出目录
|
|
@@ -16,7 +17,7 @@ const install = require('../install');
|
|
|
16
17
|
* @param { array } defaultFiles 默认需要拷贝的配置项
|
|
17
18
|
* @returns
|
|
18
19
|
*/
|
|
19
|
-
const cpFilesToOutput = function (tmsConfig,
|
|
20
|
+
const cpFilesToOutput = function (tmsConfig, defaultFiles) {
|
|
20
21
|
const outputDir = resolve(tmsConfig.outputDir);
|
|
21
22
|
io.ensureDirExist(outputDir);
|
|
22
23
|
defaultFiles.forEach((item) => {
|
|
@@ -24,19 +25,26 @@ const cpFilesToOutput = function (tmsConfig, targetModules, defaultFiles) {
|
|
|
24
25
|
shelljs.cp('-rf', resolve(item), resolve(tmsConfig.outputDir, item));
|
|
25
26
|
}
|
|
26
27
|
});
|
|
28
|
+
};
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
/**
|
|
31
|
+
* 校验相关配置项
|
|
32
|
+
* @param {*} targetModules
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
function checkConfig(targetModules) {
|
|
36
|
+
for (const item of targetModules) {
|
|
37
|
+
if (!item.root) {
|
|
38
|
+
throw new Error(`检查${item.name} module.config.json的root字段`);
|
|
34
39
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (fs.existsSync(
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
+
|
|
41
|
+
// 判断源码目录是否有该模块
|
|
42
|
+
if (item.path && !fs.existsSync(resolve(item.path))) {
|
|
43
|
+
throw new Error(`${item.path}模块代码路径不存在, 请检查tms.config.js的${item.name}模块的path`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
40
48
|
|
|
41
49
|
async function task(tmsConfig, targetModules) {
|
|
42
50
|
// 下载和移动代码
|
|
@@ -51,15 +59,19 @@ async function task(tmsConfig, targetModules) {
|
|
|
51
59
|
// A分包依赖了B分包的代码, merge B分包进行编译;
|
|
52
60
|
newModules = subModulesMergeDepModules(tmsConfig, newModules);
|
|
53
61
|
|
|
62
|
+
checkConfig(newModules);
|
|
63
|
+
|
|
54
64
|
// 拷贝相关配置文件到输出目录
|
|
55
65
|
await createTask(
|
|
56
66
|
cpFilesToOutput,
|
|
57
67
|
'开始拷贝文件到编译输出目录',
|
|
58
68
|
'拷贝文件到编译输出目录完成',
|
|
59
|
-
)(tmsConfig,
|
|
69
|
+
)(tmsConfig, DEFAULT_COPY_CONFIG);
|
|
60
70
|
|
|
61
71
|
// install
|
|
62
|
-
|
|
72
|
+
if (checkDependencies(newModules, resolve('./'), tmsConfig.outputDir)) {
|
|
73
|
+
await install(tmsConfig, newModules);
|
|
74
|
+
}
|
|
63
75
|
|
|
64
76
|
// 动态生成编译后的app.json;
|
|
65
77
|
await createTask(
|
|
@@ -79,7 +91,7 @@ async function init(tmsConfig, targetModules) {
|
|
|
79
91
|
};
|
|
80
92
|
} catch (error) {
|
|
81
93
|
const errMsg = typeof error === 'object' ? error.message : error;
|
|
82
|
-
fail(
|
|
94
|
+
fail(`初始化流程出现错误${errMsg}`);
|
|
83
95
|
info('详细的错误信息', error);
|
|
84
96
|
process.exit(1);
|
|
85
97
|
}
|
|
@@ -1,10 +1,25 @@
|
|
|
1
|
+
const shelljs = require('shelljs');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const io = require('../../../utils/io');
|
|
1
4
|
const { createTask, resolve } = require('../../../utils/widgets');
|
|
2
5
|
const { buildMpNpm } = require('../../../core/mpCi');
|
|
3
6
|
const { CACHE_DIR } = require('../../../config/constant');
|
|
4
|
-
const {
|
|
5
|
-
const {
|
|
7
|
+
const { mpNpmInstallAll } = require('../../../core/npm');
|
|
8
|
+
const { fail } = require('../../../utils/log');
|
|
9
|
+
|
|
10
|
+
async function install(tmsConfig, modules) {
|
|
11
|
+
// 拷贝模块的package.json到编译输出目录
|
|
12
|
+
modules.forEach((item) => {
|
|
13
|
+
const outputModuleDir = resolve(`${tmsConfig.outputDir}/${item.root}`);
|
|
14
|
+
if (!fs.existsSync(resolve(item.path))) {
|
|
15
|
+
fail(`${item.path}模块代码路径不存在, 请检查tms.config.js的${item.name}模块的path`);
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
io.ensureDirExist(outputModuleDir);
|
|
19
|
+
const modulePackagePath = resolve(item.path, 'package.json');
|
|
20
|
+
if (fs.existsSync(modulePackagePath)) shelljs.cp('-Rf', modulePackagePath, outputModuleDir);
|
|
21
|
+
});
|
|
6
22
|
|
|
7
|
-
async function install(tmsConfig, modules, isCloud = false) {
|
|
8
23
|
// 小程序npm install
|
|
9
24
|
await createTask(
|
|
10
25
|
mpNpmInstallAll,
|
|
@@ -12,20 +27,12 @@ async function install(tmsConfig, modules, isCloud = false) {
|
|
|
12
27
|
'小程序npm install 完成',
|
|
13
28
|
)(modules, resolve(tmsConfig.outputDir), `${CACHE_DIR}/node_modules`);
|
|
14
29
|
|
|
15
|
-
const tmsPrivateCf = global.getData('tmsPrivateCf');
|
|
16
30
|
// 构建miniprogram_npm
|
|
17
31
|
await createTask(
|
|
18
32
|
buildMpNpm,
|
|
19
33
|
'开始构建miniprogram_npm',
|
|
20
34
|
'构建miniprogram_npm 完成',
|
|
21
|
-
)({ appId: tmsConfig.appId, projectPath: resolve('./'), privateKey:
|
|
22
|
-
|
|
23
|
-
// 安装云函数的
|
|
24
|
-
isCloud && createTask(
|
|
25
|
-
cloudNpmInstall,
|
|
26
|
-
'云函数npm install',
|
|
27
|
-
'云函数npm install安装完毕',
|
|
28
|
-
)(resolve(tmsConfig.cloudDir));
|
|
35
|
+
)({ appId: tmsConfig.appId, projectPath: resolve('./'), privateKey: tmsConfig.privateKey });
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
module.exports = install;
|