@tmsfe/tmskit 0.0.20 → 0.0.23
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 +7 -1
- package/dist/index.cjs.js +1632 -1868
- package/package.json +4 -7
- package/src/{init.js → check.js} +2 -3
- package/src/compile/build.js +2 -2
- package/src/compile/compile.js +53 -155
- package/src/compile/dev.js +25 -69
- package/src/compile/plugins/mpProjectJson.js +13 -0
- package/src/compile/plugins/replaceEnv.js +7 -14
- package/src/compile/watch.js +49 -17
- package/src/config/constant.js +29 -8
- package/src/core/buildAppJson.js +3 -0
- package/src/core/checkDependencies.js +11 -8
- package/src/core/cloneModules.js +3 -0
- package/src/core/npm.js +6 -25
- package/src/core/recommendVersion.js +118 -0
- package/src/core/report.js +30 -0
- package/src/core/symbolicLink.js +17 -19
- package/src/core/tmsMpconfig.js +148 -69
- package/src/entry.js +13 -14
- package/src/index.js +61 -20
- package/src/scripts/create/index.js +3 -1
- package/src/scripts/extend-cmd/index.js +74 -0
- package/src/scripts/run/build/index.js +4 -2
- package/src/scripts/run/cloud/index.js +0 -2
- package/src/scripts/run/dev/index.js +9 -3
- package/src/scripts/run/index.js +52 -76
- package/src/scripts/run/init/index.js +2 -24
- package/src/{core → scripts/run/install}/cache.js +4 -4
- package/src/scripts/run/install/index.js +15 -19
- package/src/utils/log.js +3 -0
- package/src/utils/widgets.js +58 -34
package/src/core/buildAppJson.js
CHANGED
|
@@ -8,6 +8,8 @@ const { fail, info } = require('../utils/log');
|
|
|
8
8
|
const { resolve, filterField } = require('../utils/widgets');
|
|
9
9
|
const { handleError } = require('./handleError');
|
|
10
10
|
const { global } = require('../utils/global');
|
|
11
|
+
const report = require('../core/report');
|
|
12
|
+
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* 更新appJson里面的主包配置
|
|
@@ -145,6 +147,7 @@ function buildOutputAppJson(tmsConfig, modules) {
|
|
|
145
147
|
appJson,
|
|
146
148
|
isDev: global.getData('isDev'),
|
|
147
149
|
});
|
|
150
|
+
report('hooks:updateAppJson');
|
|
148
151
|
}
|
|
149
152
|
return appJson;
|
|
150
153
|
} catch (e) {
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const semver = require('semver');
|
|
3
|
-
const { resolve } = require('../utils/widgets');
|
|
3
|
+
const { resolve, getAbsolutePath } = require('../utils/widgets');
|
|
4
4
|
const path = require('path');
|
|
5
5
|
const shelljs = require('shelljs');
|
|
6
6
|
const { handleError } = require('./handleError');
|
|
7
7
|
|
|
8
8
|
const getLatestVersion = (npmName) => {
|
|
9
9
|
const data = shelljs.exec(`npm view ${npmName} version`);
|
|
10
|
-
|
|
10
|
+
if (data.code === 0) {
|
|
11
|
+
return data.stdout;
|
|
12
|
+
}
|
|
13
|
+
return '0.0.0';
|
|
11
14
|
};
|
|
12
15
|
|
|
13
16
|
// 收集package.json
|
|
14
|
-
function
|
|
17
|
+
function collectPackageJson(subPackages, cwd, outputDir) {
|
|
15
18
|
const packageJsonName = 'package.json'; // 查找文件名
|
|
16
19
|
// 1.1根目录的package.json
|
|
17
20
|
const packageArr = [
|
|
@@ -21,8 +24,8 @@ function collectHasPackageJson(modules, cwd, outputDir) {
|
|
|
21
24
|
},
|
|
22
25
|
];
|
|
23
26
|
// 1.2模块的package.json
|
|
24
|
-
|
|
25
|
-
const srcPackageDir =
|
|
27
|
+
subPackages.forEach((item) => {
|
|
28
|
+
const srcPackageDir = `${getAbsolutePath(item.path)}/package.json`;
|
|
26
29
|
if (fs.existsSync(srcPackageDir)) {
|
|
27
30
|
packageArr.push({
|
|
28
31
|
srcPackageDir,
|
|
@@ -48,14 +51,14 @@ function readPackageDependencies(srcPackageDir) {
|
|
|
48
51
|
|
|
49
52
|
/**
|
|
50
53
|
* 检查package.json的依赖大于node_module的版本,则返回true
|
|
51
|
-
* @param {*}
|
|
54
|
+
* @param {*} subPackages 模块
|
|
52
55
|
* @param {*} cwd 待检查package.json所在的目录 (eg: 当前执行脚本的目录)
|
|
53
56
|
* @param {*} outputDir 待检查node_modules存放的目录 (eg: dist/node_modules)
|
|
54
57
|
* @returns
|
|
55
58
|
*/
|
|
56
|
-
const isDependenciesUpdate = (
|
|
59
|
+
const isDependenciesUpdate = (subPackages, cwd, outputDir) => {
|
|
57
60
|
// 步骤1. 收集package.json
|
|
58
|
-
const packageArr =
|
|
61
|
+
const packageArr = collectPackageJson(subPackages, cwd, outputDir);
|
|
59
62
|
// 步骤2. 比较package.json的依赖与node_modules依赖的版本号
|
|
60
63
|
return checkPackageVersion(packageArr);
|
|
61
64
|
};
|
package/src/core/cloneModules.js
CHANGED
|
@@ -39,6 +39,9 @@ function replaceGitUrlAccount(httpRepoUrl, moduleName) {
|
|
|
39
39
|
* @returns { undefined } no return
|
|
40
40
|
*/
|
|
41
41
|
function moveFile(sourceDir, targetDir, ignore = []) {
|
|
42
|
+
if (fs.existsSync(targetDir)) {
|
|
43
|
+
shelljs.rm('-rf', targetDir);
|
|
44
|
+
}
|
|
42
45
|
// 删除不是文件夹的文件
|
|
43
46
|
return new Promise((resolve, reject) => {
|
|
44
47
|
MetalSmith(__dirname)
|
package/src/core/npm.js
CHANGED
|
@@ -6,10 +6,10 @@ const fsExtra = require('fs-extra');
|
|
|
6
6
|
const crypto = require('crypto');
|
|
7
7
|
const path = require('path');
|
|
8
8
|
const shell = require('shelljs');
|
|
9
|
-
const glob = require('glob-ignore');
|
|
10
9
|
const log = require('../utils/log');
|
|
11
10
|
const { npmInstall } = require('../utils/widgets');
|
|
12
11
|
const { handleError } = require('./handleError');
|
|
12
|
+
const { global } = require('../utils/global');
|
|
13
13
|
|
|
14
14
|
const shellJsOption = { async: false, silent: true };
|
|
15
15
|
const dirPath = process.cwd(); // 项目根目录
|
|
@@ -76,7 +76,8 @@ const collectNpmTasksMap = (packageJsonFiles, cacheDir) => {
|
|
|
76
76
|
fsExtra.emptydirSync(cacheNMPath);
|
|
77
77
|
shell.cp('-f', packageJsonPath, cacheNMPath);
|
|
78
78
|
log.info(`npm install: ${packageJsonPath}`);
|
|
79
|
-
|
|
79
|
+
const tmsConfig = global.getData('tmsConfig');
|
|
80
|
+
return npmInstall(cacheNMPath, tmsConfig.npm).then(() => {
|
|
80
81
|
const newShellJsOption = {
|
|
81
82
|
...shellJsOption,
|
|
82
83
|
cwd: cacheNMPath,
|
|
@@ -104,9 +105,9 @@ const collectNpmTasksMap = (packageJsonFiles, cacheDir) => {
|
|
|
104
105
|
|
|
105
106
|
|
|
106
107
|
// 遍历安装指定目录下所有项目的npm依赖
|
|
107
|
-
const npmInstallAll = async (
|
|
108
|
+
const npmInstallAll = async (subPackages, contextDir, cacheDir) => {
|
|
108
109
|
const cwd = process.cwd();
|
|
109
|
-
const packageJsonFiles = await findAllPackageJson(
|
|
110
|
+
const packageJsonFiles = await findAllPackageJson(subPackages, contextDir);
|
|
110
111
|
|
|
111
112
|
// 收集npm install的任务
|
|
112
113
|
const npmTasksMap = collectNpmTasksMap(packageJsonFiles, cacheDir);
|
|
@@ -189,10 +190,6 @@ const findAllPackageJson = (subRoots = [], contextDir) => {
|
|
|
189
190
|
const result = [path.join(cwd, packageJsonName)]; // 默认填充根目录下的package.json
|
|
190
191
|
|
|
191
192
|
subRoots.forEach((subRoot) => {
|
|
192
|
-
if (!subRoot.root) {
|
|
193
|
-
log.fail(`请检查${subRoot.name}的module.config.json是否有root字段`);
|
|
194
|
-
process.exit(1);
|
|
195
|
-
}
|
|
196
193
|
const toppath = path.join(cwd, subRoot.root); // 从该目录开始查找package.json文件
|
|
197
194
|
const list = findFilesByFilter(toppath, packageJsonName);
|
|
198
195
|
|
|
@@ -202,26 +199,10 @@ const findAllPackageJson = (subRoots = [], contextDir) => {
|
|
|
202
199
|
return result;
|
|
203
200
|
};
|
|
204
201
|
|
|
205
|
-
function cloudNpmInstall(contextDir) {
|
|
206
|
-
return new Promise((resolve, reject) => {
|
|
207
|
-
glob(`${contextDir}/**/package.json`, ['node_modules', 'miniprogram_npm'], (err, files) => {
|
|
208
|
-
if (err) {
|
|
209
|
-
reject(err);
|
|
210
|
-
}
|
|
211
|
-
files.forEach((file) => {
|
|
212
|
-
const dir = path.dirname(file);
|
|
213
|
-
shell.cd(dir);
|
|
214
|
-
shell.exec('npx npm install --production --registry http://mirrors.tencent.com/npm/', { silent: false });
|
|
215
|
-
});
|
|
216
|
-
resolve();
|
|
217
|
-
});
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
|
|
221
202
|
|
|
222
203
|
module.exports = {
|
|
223
|
-
cloudNpmInstall,
|
|
224
204
|
npmInstallAll,
|
|
225
205
|
findAllPackageJson,
|
|
206
|
+
findFilesByFilter,
|
|
226
207
|
};
|
|
227
208
|
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const shelljs = require('shelljs');
|
|
4
|
+
const chalk = require('chalk');
|
|
5
|
+
const request = require('request');
|
|
6
|
+
const inquirer = require('inquirer');
|
|
7
|
+
const { infoNoTime } = require('../utils/log');
|
|
8
|
+
const packageJson = require('../../package.json');
|
|
9
|
+
const { VERSION_CACHE_FILE, VERSION_URL } = require('../config/constant');
|
|
10
|
+
const { ensureDirExist } = require('../utils/io');
|
|
11
|
+
const { versionCompare } = require('../utils/widgets');
|
|
12
|
+
|
|
13
|
+
// 获取推荐的tmskit版本
|
|
14
|
+
function getRecommendVersion() {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
request(`${VERSION_URL}?v=${new Date().getTime()}`, (error, response, body) => {
|
|
17
|
+
if (!error && response && response.statusCode === 200) {
|
|
18
|
+
resolve(JSON.parse(body));
|
|
19
|
+
} else {
|
|
20
|
+
reject(response.statusCode);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 获取当前用户tmskit的版本
|
|
27
|
+
function getUserTmskitVersion() {
|
|
28
|
+
const data = shelljs.exec('tmskit -v', { async: false, silent: true });
|
|
29
|
+
if (data.code === 0) {
|
|
30
|
+
// tmskit 0.0.21 => 0.0.21
|
|
31
|
+
return data.slice(7);
|
|
32
|
+
}
|
|
33
|
+
return '0.0.0';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 询问用户是否安装最新版本
|
|
37
|
+
function isInstallLatestVersion() {
|
|
38
|
+
return inquirer.prompt([
|
|
39
|
+
{
|
|
40
|
+
type: 'confirm',
|
|
41
|
+
name: 'isInstall',
|
|
42
|
+
message: '是否安装最新版本',
|
|
43
|
+
choices: [{
|
|
44
|
+
name: '否',
|
|
45
|
+
value: false,
|
|
46
|
+
}, {
|
|
47
|
+
name: '是',
|
|
48
|
+
value: true,
|
|
49
|
+
}],
|
|
50
|
+
},
|
|
51
|
+
]);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 获取tmskit的版本是否推荐过
|
|
55
|
+
function getVersionIsRecommend(version) {
|
|
56
|
+
const filePath = VERSION_CACHE_FILE;
|
|
57
|
+
if (!fs.existsSync(filePath)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const content = require(filePath);
|
|
61
|
+
return content?.[version];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 设置tmskit版本推荐过
|
|
65
|
+
function setVersionRecommend(version, isRecommend) {
|
|
66
|
+
const filePath = VERSION_CACHE_FILE;
|
|
67
|
+
if (!fs.existsSync(filePath)) {
|
|
68
|
+
const dir = path.dirname(filePath);
|
|
69
|
+
ensureDirExist(dir);
|
|
70
|
+
fs.writeFileSync(filePath, '{}');
|
|
71
|
+
}
|
|
72
|
+
const content = require(filePath);
|
|
73
|
+
content[version] = isRecommend;
|
|
74
|
+
fs.writeFileSync(filePath, JSON.stringify(content, null, 2));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// 推荐理由
|
|
78
|
+
function displayRecommends(arr = []) {
|
|
79
|
+
infoNoTime(chalk.green('构建工具有新的版本~~:'));
|
|
80
|
+
arr.forEach((item) => {
|
|
81
|
+
infoNoTime(chalk.green(item));
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 推荐tmskit的安装版本
|
|
86
|
+
async function recommendVersion() {
|
|
87
|
+
try {
|
|
88
|
+
// 获取当前用户安装的版本
|
|
89
|
+
const tmskitVersion = getUserTmskitVersion();
|
|
90
|
+
// 获取官方推荐版本
|
|
91
|
+
const recommendRes = await getRecommendVersion();
|
|
92
|
+
const recommendVersion = recommendRes.version;
|
|
93
|
+
// 当前用户安装版本小于官方推荐版本 && 该版本没有推荐过
|
|
94
|
+
if (versionCompare(tmskitVersion, recommendVersion) === -1 && !getVersionIsRecommend(recommendVersion)) {
|
|
95
|
+
displayRecommends(recommendRes.version_detail);
|
|
96
|
+
// 设置该版本推荐过
|
|
97
|
+
setVersionRecommend(recommendVersion, true);
|
|
98
|
+
// 询问用户是否安装最新版本
|
|
99
|
+
const installRes = await isInstallLatestVersion(recommendRes);
|
|
100
|
+
if (installRes.isInstall) {
|
|
101
|
+
// mac
|
|
102
|
+
if (process.platform === 'darwin') {
|
|
103
|
+
infoNoTime(`将执行以下命令: ${chalk.green(`sudo npm install -g ${packageJson.name}@${recommendVersion}`)}`);
|
|
104
|
+
shelljs.exec(`sudo npm install -g ${packageJson.name}@${recommendVersion}`, { async: false, silent: false });
|
|
105
|
+
process.exit(-1);
|
|
106
|
+
} else {
|
|
107
|
+
// window
|
|
108
|
+
infoNoTime(`请使用超级管理员执行以下命令: ${chalk.green(`npm install -g ${packageJson.name}@${recommendVersion}`)}`);
|
|
109
|
+
process.exit(-1);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} catch {}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
module.exports = {
|
|
117
|
+
recommendVersion,
|
|
118
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const request = require('request');
|
|
2
|
+
const shelljs = require('shelljs');
|
|
3
|
+
const apiUrl = 'https://tim.map.qq.com/basic/tmskit/upload';
|
|
4
|
+
|
|
5
|
+
function getGitUser() {
|
|
6
|
+
const res = shelljs.exec('git config user.name', { async: false, silent: true });
|
|
7
|
+
return res.stdout;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const report = (name, attrs = {}) => {
|
|
11
|
+
try {
|
|
12
|
+
const param = [];
|
|
13
|
+
param[27] = name;
|
|
14
|
+
param[28] = 'tmskit';
|
|
15
|
+
param[29] = getGitUser();
|
|
16
|
+
param[30] = JSON.stringify(attrs);
|
|
17
|
+
|
|
18
|
+
for (let i = 0; i < 40; i++) {
|
|
19
|
+
if (!param[i]) param[i] = null;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
request.post({ url: apiUrl, json: { param } });
|
|
23
|
+
// (error, response, body) => {
|
|
24
|
+
// console.log('body:', body);
|
|
25
|
+
// }
|
|
26
|
+
} catch (e) {
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
module.exports = report;
|
package/src/core/symbolicLink.js
CHANGED
|
@@ -1,32 +1,30 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const { resolve } = require('../utils/widgets');
|
|
3
|
-
const { handleError } = require('./handleError');
|
|
4
3
|
const { ensureDirExist } = require('../utils/io');
|
|
5
|
-
const { warn } = require('../utils/log');
|
|
4
|
+
const { warn, succeed } = require('../utils/log');
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* 根据相关配置创建软链接
|
|
9
8
|
* @param { object } tmsConfig
|
|
10
9
|
*/
|
|
11
10
|
const symLink = (tmsConfig) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const targetPath = resolve(tmsConfig.cloudDir, item.name);
|
|
11
|
+
ensureDirExist(resolve(tmsConfig.cloudDir));
|
|
12
|
+
if (tmsConfig.cloudModules) {
|
|
13
|
+
tmsConfig.cloudModules.forEach((item) => {
|
|
14
|
+
const sourcePath = resolve(item.path);
|
|
15
|
+
const targetPath = resolve(tmsConfig.cloudDir, item.name);
|
|
18
16
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
17
|
+
if (!fs.existsSync(sourcePath)) {
|
|
18
|
+
warn(`云函数${sourcePath}不存在`);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (!fs.existsSync(targetPath)) {
|
|
22
|
+
fs.symlinkSync(sourcePath, targetPath);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
succeed('云函数创建软链成功');
|
|
26
|
+
} else {
|
|
27
|
+
warn('你没有在tms.config.js的cloudModules注册云函数');
|
|
30
28
|
}
|
|
31
29
|
};
|
|
32
30
|
|
package/src/core/tmsMpconfig.js
CHANGED
|
@@ -4,62 +4,86 @@
|
|
|
4
4
|
/* eslint-disable no-param-reassign, no-nested-ternary */
|
|
5
5
|
const loadash = require('lodash');
|
|
6
6
|
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
7
8
|
const { TMS_CONFIG_FILENAME, MODULE_CONFIG_FILENAME, TMS_PRIVATE_FILENAME } = require('../config/constant');
|
|
8
|
-
const { resolve, isObject, isArray } = require('../utils/widgets');
|
|
9
|
+
const { resolve, isObject, isArray, getAbsolutePath } = require('../utils/widgets');
|
|
9
10
|
const defaultTmsConfig = require('../config/defaultTmsConfig');
|
|
10
11
|
const { fail } = require('../utils/log');
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* 读取tms.config.js
|
|
14
|
-
*
|
|
15
|
+
* @param {string} configPath tms.config.js的路径
|
|
15
16
|
*/
|
|
16
|
-
const readTmsConfig = function (
|
|
17
|
-
const tmsConfigPath = resolve(TMS_CONFIG_FILENAME);
|
|
17
|
+
const readTmsConfig = function (configPath) {
|
|
18
|
+
const tmsConfigPath = configPath ? `${configPath}/${TMS_CONFIG_FILENAME}` : resolve(TMS_CONFIG_FILENAME);
|
|
18
19
|
if (!fs.existsSync(tmsConfigPath)) {
|
|
19
|
-
fail(
|
|
20
|
+
fail(`${path.dirname(tmsConfigPath)}没有找到tms.config.js,请进行配置`);
|
|
20
21
|
process.exit(1);
|
|
21
22
|
}
|
|
22
23
|
const tmsConfigFn = require(tmsConfigPath);
|
|
23
|
-
const tmsConfig = tmsConfigFn(
|
|
24
|
-
env,
|
|
25
|
-
});
|
|
24
|
+
const tmsConfig = typeof tmsConfigFn === 'function' ? tmsConfigFn() : tmsConfigFn;
|
|
26
25
|
|
|
27
26
|
// 合并默认值
|
|
28
|
-
return loadash.mergeWith(defaultTmsConfig, tmsConfig);
|
|
27
|
+
return loadash.mergeWith({}, defaultTmsConfig, tmsConfig);
|
|
29
28
|
};
|
|
30
29
|
|
|
31
30
|
|
|
32
31
|
/**
|
|
33
32
|
* 读取tms.private.config.js
|
|
33
|
+
* @param {string} configPath tms.private.config.js的路径
|
|
34
34
|
*/
|
|
35
|
-
const readTmsPrivateCf = function () {
|
|
35
|
+
const readTmsPrivateCf = function (configPath) {
|
|
36
36
|
let tmsPrivateCf = {};
|
|
37
|
-
const tmsPrivatePath = resolve(TMS_PRIVATE_FILENAME);
|
|
37
|
+
const tmsPrivatePath = configPath ? `${configPath}/${TMS_PRIVATE_FILENAME}` : resolve(TMS_PRIVATE_FILENAME);
|
|
38
38
|
if (fs.existsSync(tmsPrivatePath)) {
|
|
39
|
-
|
|
39
|
+
const tmsPrivateFn = require(tmsPrivatePath);
|
|
40
|
+
tmsPrivateCf = typeof tmsPrivateFn === 'function' ? tmsPrivateFn() : tmsPrivateFn;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
return tmsPrivateCf;
|
|
43
44
|
};
|
|
44
45
|
|
|
45
46
|
/**
|
|
46
|
-
*
|
|
47
|
+
* 获取tms.config.js, tms.private.config.js的配置项
|
|
48
|
+
* @param {string} configPath config.js的路径
|
|
49
|
+
* @returns
|
|
50
|
+
*/
|
|
51
|
+
const getTmsConfig = (configPath) => {
|
|
52
|
+
const tmsPrivateCf = readTmsPrivateCf(configPath);
|
|
53
|
+
const tmsConfig = readTmsConfig(configPath);
|
|
54
|
+
|
|
55
|
+
const modules = {};
|
|
56
|
+
if (Array.isArray(tmsConfig.modules)) {
|
|
57
|
+
modules.all = tmsConfig.modules;
|
|
58
|
+
tmsConfig.modules = modules;
|
|
59
|
+
}
|
|
60
|
+
// 合并默认值
|
|
61
|
+
const res = loadash.mergeWith(tmsConfig, tmsPrivateCf, (objValue, srcValue) => {
|
|
62
|
+
if (loadash.isArray(objValue) && objValue[0] && loadash.isObject(objValue[0])) {
|
|
63
|
+
return objValue.concat(srcValue);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return res;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 根据moduleNames获取modules
|
|
47
72
|
* @param { object } tmsConfig
|
|
48
|
-
* @param { array }
|
|
73
|
+
* @param { array } moduleNames
|
|
74
|
+
* @param {boolean} errorIsQuit 找不到配置文件是否退出
|
|
49
75
|
* @returns
|
|
50
76
|
*/
|
|
51
|
-
const
|
|
77
|
+
const getModulesByModuleNames = function (tmsConfig, moduleNames = []) {
|
|
52
78
|
const targetModules = [];
|
|
53
|
-
|
|
79
|
+
moduleNames.forEach((moduleName) => {
|
|
54
80
|
const module = tmsConfig.modules.all.find(module => module.moduleName === moduleName);
|
|
55
|
-
|
|
81
|
+
if (!module) {
|
|
82
|
+
throw new Error(`你启动的模块${moduleName}在tms.config.js的modules.all中没有注册`);
|
|
83
|
+
}
|
|
84
|
+
targetModules.push(module);
|
|
56
85
|
});
|
|
57
86
|
|
|
58
|
-
if (targetModules.length === 0) {
|
|
59
|
-
fail(`你启动的模块无效${modules.join(',')}无效,请检查tms.config.json>modules>${modules.join(',')}
|
|
60
|
-
>name字段与module.config.json的name字段是否一致`);
|
|
61
|
-
isQuit && process.exit(1);
|
|
62
|
-
}
|
|
63
87
|
return targetModules;
|
|
64
88
|
};
|
|
65
89
|
|
|
@@ -94,25 +118,32 @@ function adaptMpCgContent(fileContent, appName) {
|
|
|
94
118
|
return content;
|
|
95
119
|
}
|
|
96
120
|
|
|
121
|
+
const adaptDependencies = function (dependencies, subPackages) {
|
|
122
|
+
const newDependencies = dependencies || [];
|
|
123
|
+
subPackages.forEach((item) => {
|
|
124
|
+
if (item.dependencies) {
|
|
125
|
+
dependencies = newDependencies.concat(item.dependencies);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return newDependencies;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const adaptSubPackages = function (moduleConfig, appName) {
|
|
132
|
+
const subPackages = isObject(moduleConfig) && moduleConfig.subPackages
|
|
133
|
+
? moduleConfig.subPackages
|
|
134
|
+
: isObject(moduleConfig) ? [moduleConfig] : moduleConfig;
|
|
135
|
+
return adaptMpCgContent(subPackages, appName);
|
|
136
|
+
};
|
|
137
|
+
|
|
97
138
|
/**
|
|
98
139
|
* 获取模块module.config.json中的配置信息
|
|
99
140
|
* @param {array} modules 用户要编译的模块列表
|
|
100
141
|
* @param { string } appName 小程序的名称
|
|
101
|
-
* @param {boolean} notFindIsQuit 找不到配置文件是否退出
|
|
102
142
|
*/
|
|
103
|
-
function getModulesConfig(modules = [], appName
|
|
143
|
+
function getModulesConfig(modules = [], appName) {
|
|
104
144
|
const modulesConfig = [];
|
|
105
145
|
modules.forEach((moduleItem) => {
|
|
106
|
-
if (!moduleItem.path) {
|
|
107
|
-
throw new Error(`${moduleItem.moduleName}模块路径配置没有找到`);
|
|
108
|
-
}
|
|
109
146
|
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
147
|
let moduleConfig;
|
|
117
148
|
try {
|
|
118
149
|
moduleConfig = JSON.parse(fs.readFileSync(moduleConfigPath, 'utf-8'));
|
|
@@ -121,26 +152,15 @@ function getModulesConfig(modules = [], appName, notFindIsQuit) {
|
|
|
121
152
|
}
|
|
122
153
|
|
|
123
154
|
// 兼容历史逻辑,后续可删除--- start
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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;
|
|
155
|
+
const subPackages = adaptSubPackages(moduleConfig, appName);
|
|
156
|
+
const dependencies = adaptDependencies(moduleConfig.dependencies, subPackages);
|
|
157
|
+
moduleConfig = {
|
|
158
|
+
...(isObject(moduleConfig) ? moduleConfig : {}),
|
|
159
|
+
subPackages,
|
|
160
|
+
dependencies,
|
|
161
|
+
};
|
|
141
162
|
// 兼容逻辑--- end
|
|
142
|
-
|
|
143
|
-
modulesConfig.push(moduleItem);
|
|
163
|
+
modulesConfig.push(moduleConfig);
|
|
144
164
|
});
|
|
145
165
|
|
|
146
166
|
return modulesConfig;
|
|
@@ -154,40 +174,98 @@ function getModulesConfig(modules = [], appName, notFindIsQuit) {
|
|
|
154
174
|
const getSubPackages = (modules) => {
|
|
155
175
|
const newSubPackages = [];
|
|
156
176
|
modules.forEach((module) => {
|
|
157
|
-
(module.subPackages || []).forEach((
|
|
177
|
+
(module.subPackages || []).forEach((subPackage) => {
|
|
158
178
|
newSubPackages.push({
|
|
159
|
-
|
|
160
|
-
...item,
|
|
179
|
+
...subPackage,
|
|
161
180
|
});
|
|
162
181
|
});
|
|
163
182
|
});
|
|
164
183
|
return newSubPackages;
|
|
165
184
|
};
|
|
166
185
|
|
|
186
|
+
/**
|
|
187
|
+
* 获取分包的root字段
|
|
188
|
+
* @param {*} modulePath 模块的编译路径
|
|
189
|
+
* @param {*} root 分包的root字段
|
|
190
|
+
* @returns
|
|
191
|
+
*/
|
|
192
|
+
const getSubPackageRoot = function (modulePath, root) {
|
|
193
|
+
return root.startsWith(modulePath) ? root : `${modulePath}/${root}`;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// 获取分包的源码路径
|
|
197
|
+
const getSubPackageSrcPath = function (tmsConfig, module, subPackage) {
|
|
198
|
+
const srcModulePath = getAbsolutePath(module.path);
|
|
199
|
+
const buildModulePath = resolve(tmsConfig.outputDir, module.modulePath);
|
|
200
|
+
|
|
201
|
+
const subPackageRoot = getSubPackageRoot(module.modulePath, subPackage.root);
|
|
202
|
+
const buildSubPackagePath = resolve(tmsConfig.outputDir, subPackageRoot);
|
|
203
|
+
|
|
204
|
+
const subPackageRelativeModule = path.relative(buildModulePath, buildSubPackagePath);
|
|
205
|
+
const srcSubPackagePath = path.join(srcModulePath, subPackageRelativeModule);
|
|
206
|
+
|
|
207
|
+
return srcSubPackagePath;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const checkModuleItem = (tmsConfig, tmsModuleItem, moduleConfig) => {
|
|
211
|
+
const newModuleItem = { ...tmsModuleItem, ...moduleConfig };
|
|
212
|
+
|
|
213
|
+
// 兼容逻辑
|
|
214
|
+
if (!newModuleItem.moduleName) newModuleItem.moduleName = newModuleItem.name;
|
|
215
|
+
delete newModuleItem.name;
|
|
216
|
+
|
|
217
|
+
// 参数校验-模块源码路径
|
|
218
|
+
if (!newModuleItem.path) {
|
|
219
|
+
throw new Error(`${newModuleItem.moduleName}模块没有找到path字段,请检查tms.config.js的modules.all>module>path路径`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// 参数校验-模块编译路径
|
|
223
|
+
if (!newModuleItem.modulePath) {
|
|
224
|
+
throw new Error(`${newModuleItem.moduleName}模块的module.config.json中没有找到modulePath字段`);
|
|
225
|
+
}
|
|
226
|
+
// 参数校验-分包校验
|
|
227
|
+
for (const subPackage of newModuleItem.subPackages) {
|
|
228
|
+
if (!subPackage.root) {
|
|
229
|
+
throw new Error(`${newModuleItem.moduleName}模块的module.config.json中没有找到${subPackage.name}分包的root字段`);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// 参数校验-判断分包源码目录是否存在
|
|
233
|
+
const subPackageSrcPath = getSubPackageSrcPath(tmsConfig, newModuleItem, subPackage);
|
|
234
|
+
if (!subPackageSrcPath || !fs.existsSync(subPackageSrcPath)) {
|
|
235
|
+
throw new Error(`没有找到${newModuleItem.moduleName}模块的${subPackage.name}分包源码:${subPackageSrcPath}`);
|
|
236
|
+
}
|
|
237
|
+
subPackage.path = subPackageSrcPath;
|
|
238
|
+
}
|
|
239
|
+
return newModuleItem;
|
|
240
|
+
};
|
|
241
|
+
|
|
167
242
|
/**
|
|
168
243
|
* 获取所有的模块,合并模块的依赖模块
|
|
169
244
|
* @param { object } tmsConfig
|
|
170
245
|
* @param {array} modules
|
|
171
|
-
* @param {boolean}
|
|
246
|
+
* @param {boolean} errorIsQuit 找不到配置文件是否退出
|
|
172
247
|
* @returns
|
|
173
248
|
*/
|
|
174
|
-
const getModulesByMergeDepModules = (tmsConfig, modules,
|
|
249
|
+
const getModulesByMergeDepModules = (tmsConfig, modules, errorIsQuit = false) => {
|
|
175
250
|
const allModules = new Map();
|
|
176
251
|
function dfs(tmsConfig, modules) {
|
|
177
252
|
modules.forEach((moduleItem) => {
|
|
178
|
-
const
|
|
253
|
+
const moduleConfigPath = resolve(moduleItem.path, MODULE_CONFIG_FILENAME);
|
|
254
|
+
if (!fs.existsSync(moduleConfigPath)) {
|
|
255
|
+
if (!allModules.has(moduleItem.moduleName)) {
|
|
256
|
+
allModules.set(moduleItem.moduleName, moduleItem);
|
|
257
|
+
}
|
|
258
|
+
if (errorIsQuit) {
|
|
259
|
+
throw new Error(`${moduleItem.moduleName}模块的配置文件module.config.json在${moduleItem.path}目录下没有找到`);
|
|
260
|
+
}
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const [moduleConfig = {}] = getModulesConfig([moduleItem], tmsConfig.appName);
|
|
179
264
|
|
|
180
265
|
if (!allModules.has(moduleItem.moduleName)) {
|
|
181
|
-
allModules.set(moduleItem.moduleName,
|
|
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
|
-
});
|
|
266
|
+
allModules.set(moduleItem.moduleName, checkModuleItem(tmsConfig, moduleItem, moduleConfig));
|
|
267
|
+
|
|
268
|
+
const dependenciesModules = getModulesByModuleNames(tmsConfig, moduleConfig?.dependencies);
|
|
191
269
|
if (dependenciesModules.length) {
|
|
192
270
|
dfs(tmsConfig, dependenciesModules);
|
|
193
271
|
}
|
|
@@ -207,7 +285,8 @@ module.exports = {
|
|
|
207
285
|
readTmsConfig,
|
|
208
286
|
readTmsPrivateCf,
|
|
209
287
|
getModulesConfig,
|
|
210
|
-
|
|
288
|
+
getModulesByModuleNames,
|
|
211
289
|
getSubPackages,
|
|
212
290
|
getModulesByMergeDepModules,
|
|
291
|
+
getTmsConfig,
|
|
213
292
|
};
|