@tmsfe/tmskit 0.0.20 → 0.0.21
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/dist/index.cjs.js +737 -700
- package/package.json +1 -1
- package/src/{init.js → check.js} +2 -3
- package/src/compile/build.js +2 -2
- package/src/compile/dev.js +23 -29
- package/src/core/checkDependencies.js +7 -7
- package/src/core/npm.js +3 -6
- package/src/core/symbolicLink.js +17 -19
- package/src/core/tmsMpconfig.js +140 -63
- package/src/entry.js +1 -1
- package/src/index.js +18 -4
- package/src/scripts/run/build/index.js +2 -2
- package/src/scripts/run/cloud/index.js +0 -2
- package/src/scripts/run/dev/index.js +1 -1
- package/src/scripts/run/index.js +45 -75
- package/src/scripts/run/init/index.js +2 -24
- package/src/scripts/run/install/index.js +13 -17
- package/src/utils/widgets.js +9 -0
package/package.json
CHANGED
package/src/{init.js → check.js}
RENAMED
|
@@ -22,12 +22,11 @@ const checkNodeVersion = (wanted, id) => {
|
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* 检查运行环境
|
|
25
|
-
* @returns {Undefined} 无需返回值
|
|
26
25
|
*/
|
|
27
|
-
function
|
|
26
|
+
function check() {
|
|
28
27
|
// 执行操作前检查node版本
|
|
29
28
|
// 旧版本node直接提示升级,退出脚本
|
|
30
29
|
checkNodeVersion(requiredVersion, packName);
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
module.exports =
|
|
32
|
+
module.exports = check;
|
package/src/compile/build.js
CHANGED
package/src/compile/dev.js
CHANGED
|
@@ -3,7 +3,7 @@ const fs = require('fs');
|
|
|
3
3
|
const ora = require('ora');
|
|
4
4
|
const chalk = require('chalk');
|
|
5
5
|
const { parallel, series } = require('gulp');
|
|
6
|
-
const { resolve, mergeMap, filterField } = require('../utils/widgets');
|
|
6
|
+
const { resolve, mergeMap, filterField, getAbsolutePath } = require('../utils/widgets');
|
|
7
7
|
const { buildOutputAppJson } = require('../core/buildAppJson');
|
|
8
8
|
const { DEFAULT_COPY_CONFIG } = require('../config/constant');
|
|
9
9
|
const compile = require('./compile');
|
|
@@ -30,13 +30,7 @@ function excludeGlob(glob) {
|
|
|
30
30
|
return Array.from(otherArr);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
let newPath = pathDir;
|
|
35
|
-
newPath = newPath.startsWith('/') ? newPath : resolve(cwd, newPath);
|
|
36
|
-
newPath = newPath.endsWith('/') ? newPath.slice(0, newPath.length - 1) : newPath;
|
|
37
|
-
return newPath;
|
|
38
|
-
}
|
|
39
|
-
module.exports = async (tmsConfig, modules, subPackages, isDev = true) => {
|
|
33
|
+
module.exports = async (tmsConfig, modules, isDev = true) => {
|
|
40
34
|
const compileTasksMap = new Map();
|
|
41
35
|
|
|
42
36
|
// 监听根目录的文件
|
|
@@ -49,49 +43,49 @@ module.exports = async (tmsConfig, modules, subPackages, isDev = true) => {
|
|
|
49
43
|
srcOption: { allowEmpty: true },
|
|
50
44
|
isDev,
|
|
51
45
|
}));
|
|
52
|
-
|
|
53
46
|
// 监听模块的文件
|
|
54
|
-
for (let
|
|
47
|
+
for (let moduleItem of modules) {
|
|
55
48
|
// 处理默认参数
|
|
56
|
-
|
|
49
|
+
moduleItem = {
|
|
57
50
|
...{ exclude: [] },
|
|
58
|
-
...
|
|
51
|
+
...moduleItem,
|
|
59
52
|
};
|
|
60
|
-
const
|
|
53
|
+
const srcModulePath = getAbsolutePath(moduleItem.path);
|
|
54
|
+
const buildModulePath = resolve(tmsConfig.outputDir, moduleItem.modulePath);
|
|
61
55
|
|
|
62
56
|
if (isDev) {
|
|
63
57
|
// 监听模块配置文件
|
|
64
58
|
watch(
|
|
65
|
-
[`${
|
|
59
|
+
[`${srcModulePath}/**/module.config.json`],
|
|
66
60
|
{ events: watchEvents },
|
|
67
61
|
() => buildOutputAppJson(tmsConfig, modules, isDev),
|
|
68
|
-
{ from:
|
|
62
|
+
{ from: srcModulePath, to: buildModulePath },
|
|
69
63
|
);
|
|
70
64
|
}
|
|
71
65
|
|
|
72
|
-
const excludes =
|
|
73
|
-
const newPath =
|
|
66
|
+
const excludes = moduleItem.exclude.map((ePath) => {
|
|
67
|
+
const newPath = getAbsolutePath(ePath, srcModulePath);
|
|
74
68
|
const ext = path.extname(ePath).slice(1);
|
|
75
69
|
if (ext) {
|
|
76
|
-
return `!${resolve(
|
|
70
|
+
return `!${resolve(srcModulePath, newPath)}`;
|
|
77
71
|
}
|
|
78
|
-
return `!${resolve(
|
|
72
|
+
return `!${resolve(srcModulePath, newPath)}/**/*`;
|
|
79
73
|
});
|
|
80
74
|
const glob = {
|
|
81
|
-
js: [`${
|
|
82
|
-
json: [`${
|
|
83
|
-
wxss: [`${
|
|
84
|
-
wxml: [`${
|
|
85
|
-
image: [`${
|
|
75
|
+
js: [`${srcModulePath}/**/*.{js,ts,wxs}`, ...excludes],
|
|
76
|
+
json: [`${srcModulePath}/**/*.json`, `!${srcModulePath}/**/module.config.json`, ...excludes],
|
|
77
|
+
wxss: [`${srcModulePath}/**/*.{less,wxss,scss,sass,styl}`, ...excludes],
|
|
78
|
+
wxml: [`${srcModulePath}/**/*.wxml`, ...excludes],
|
|
79
|
+
image: [`${srcModulePath}/**/*.{png,jpg,jpeg,gif,svg}`, ...excludes],
|
|
86
80
|
};
|
|
87
81
|
|
|
88
82
|
mergeMap(compileTasksMap, compile(tmsConfig, {
|
|
89
83
|
glob: {
|
|
90
84
|
...glob,
|
|
91
|
-
other: [`${
|
|
85
|
+
other: [`${srcModulePath}/**/*`, ...excludeGlob(glob)],
|
|
92
86
|
},
|
|
93
|
-
destPath:
|
|
94
|
-
module: { from:
|
|
87
|
+
destPath: buildModulePath,
|
|
88
|
+
module: { from: srcModulePath, to: buildModulePath },
|
|
95
89
|
srcOption: { allowEmpty: true },
|
|
96
90
|
isDev,
|
|
97
91
|
}));
|
|
@@ -100,8 +94,8 @@ module.exports = async (tmsConfig, modules, subPackages, isDev = true) => {
|
|
|
100
94
|
// 静态资源目录-拷贝
|
|
101
95
|
if (tmsConfig?.static && tmsConfig?.static.length > 0) {
|
|
102
96
|
for (const item of tmsConfig.static) {
|
|
103
|
-
item.from =
|
|
104
|
-
item.to =
|
|
97
|
+
item.from = getAbsolutePath(item.from);
|
|
98
|
+
item.to = getAbsolutePath(item.to);
|
|
105
99
|
|
|
106
100
|
let glob = {};
|
|
107
101
|
const ext = path.extname(item.from).slice(1);
|
|
@@ -1,6 +1,6 @@
|
|
|
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');
|
|
@@ -11,7 +11,7 @@ const getLatestVersion = (npmName) => {
|
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
// 收集package.json
|
|
14
|
-
function
|
|
14
|
+
function collectPackageJson(subPackages, cwd, outputDir) {
|
|
15
15
|
const packageJsonName = 'package.json'; // 查找文件名
|
|
16
16
|
// 1.1根目录的package.json
|
|
17
17
|
const packageArr = [
|
|
@@ -21,8 +21,8 @@ function collectHasPackageJson(modules, cwd, outputDir) {
|
|
|
21
21
|
},
|
|
22
22
|
];
|
|
23
23
|
// 1.2模块的package.json
|
|
24
|
-
|
|
25
|
-
const srcPackageDir =
|
|
24
|
+
subPackages.forEach((item) => {
|
|
25
|
+
const srcPackageDir = `${getAbsolutePath(item.path)}/package.json`;
|
|
26
26
|
if (fs.existsSync(srcPackageDir)) {
|
|
27
27
|
packageArr.push({
|
|
28
28
|
srcPackageDir,
|
|
@@ -48,14 +48,14 @@ function readPackageDependencies(srcPackageDir) {
|
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
50
|
* 检查package.json的依赖大于node_module的版本,则返回true
|
|
51
|
-
* @param {*}
|
|
51
|
+
* @param {*} subPackages 模块
|
|
52
52
|
* @param {*} cwd 待检查package.json所在的目录 (eg: 当前执行脚本的目录)
|
|
53
53
|
* @param {*} outputDir 待检查node_modules存放的目录 (eg: dist/node_modules)
|
|
54
54
|
* @returns
|
|
55
55
|
*/
|
|
56
|
-
const isDependenciesUpdate = (
|
|
56
|
+
const isDependenciesUpdate = (subPackages, cwd, outputDir) => {
|
|
57
57
|
// 步骤1. 收集package.json
|
|
58
|
-
const packageArr =
|
|
58
|
+
const packageArr = collectPackageJson(subPackages, cwd, outputDir);
|
|
59
59
|
// 步骤2. 比较package.json的依赖与node_modules依赖的版本号
|
|
60
60
|
return checkPackageVersion(packageArr);
|
|
61
61
|
};
|
package/src/core/npm.js
CHANGED
|
@@ -104,9 +104,9 @@ const collectNpmTasksMap = (packageJsonFiles, cacheDir) => {
|
|
|
104
104
|
|
|
105
105
|
|
|
106
106
|
// 遍历安装指定目录下所有项目的npm依赖
|
|
107
|
-
const npmInstallAll = async (
|
|
107
|
+
const npmInstallAll = async (subPackages, contextDir, cacheDir) => {
|
|
108
108
|
const cwd = process.cwd();
|
|
109
|
-
const packageJsonFiles = await findAllPackageJson(
|
|
109
|
+
const packageJsonFiles = await findAllPackageJson(subPackages, contextDir);
|
|
110
110
|
|
|
111
111
|
// 收集npm install的任务
|
|
112
112
|
const npmTasksMap = collectNpmTasksMap(packageJsonFiles, cacheDir);
|
|
@@ -189,10 +189,6 @@ const findAllPackageJson = (subRoots = [], contextDir) => {
|
|
|
189
189
|
const result = [path.join(cwd, packageJsonName)]; // 默认填充根目录下的package.json
|
|
190
190
|
|
|
191
191
|
subRoots.forEach((subRoot) => {
|
|
192
|
-
if (!subRoot.root) {
|
|
193
|
-
log.fail(`请检查${subRoot.name}的module.config.json是否有root字段`);
|
|
194
|
-
process.exit(1);
|
|
195
|
-
}
|
|
196
192
|
const toppath = path.join(cwd, subRoot.root); // 从该目录开始查找package.json文件
|
|
197
193
|
const list = findFilesByFilter(toppath, packageJsonName);
|
|
198
194
|
|
|
@@ -223,5 +219,6 @@ module.exports = {
|
|
|
223
219
|
cloudNpmInstall,
|
|
224
220
|
npmInstallAll,
|
|
225
221
|
findAllPackageJson,
|
|
222
|
+
findFilesByFilter,
|
|
226
223
|
};
|
|
227
224
|
|
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,8 +4,9 @@
|
|
|
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
|
|
|
@@ -13,16 +14,14 @@ const { fail } = require('../utils/log');
|
|
|
13
14
|
* 读取tms.config.js
|
|
14
15
|
* @param env {string} 环境变量
|
|
15
16
|
*/
|
|
16
|
-
const readTmsConfig = function (
|
|
17
|
+
const readTmsConfig = function () {
|
|
17
18
|
const tmsConfigPath = resolve(TMS_CONFIG_FILENAME);
|
|
18
19
|
if (!fs.existsSync(tmsConfigPath)) {
|
|
19
20
|
fail('当前执行目录没有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
27
|
return loadash.mergeWith(defaultTmsConfig, tmsConfig);
|
|
@@ -36,30 +35,53 @@ const readTmsPrivateCf = function () {
|
|
|
36
35
|
let tmsPrivateCf = {};
|
|
37
36
|
const tmsPrivatePath = resolve(TMS_PRIVATE_FILENAME);
|
|
38
37
|
if (fs.existsSync(tmsPrivatePath)) {
|
|
39
|
-
|
|
38
|
+
const tmsPrivateFn = require(tmsPrivatePath);
|
|
39
|
+
tmsPrivateCf = typeof tmsPrivateFn === 'function' ? tmsPrivateFn() : tmsPrivateFn;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
return tmsPrivateCf;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
|
-
*
|
|
46
|
+
* 获取tms.config.js, tms.private.config.js的配置项
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
const getTmsConfig = () => {
|
|
50
|
+
const tmsPrivateCf = readTmsPrivateCf();
|
|
51
|
+
const tmsConfig = readTmsConfig();
|
|
52
|
+
|
|
53
|
+
const modules = {};
|
|
54
|
+
if (Array.isArray(tmsConfig.modules)) {
|
|
55
|
+
modules.all = tmsConfig.modules;
|
|
56
|
+
tmsConfig.modules = modules;
|
|
57
|
+
}
|
|
58
|
+
// 合并默认值
|
|
59
|
+
const res = loadash.mergeWith(tmsConfig, tmsPrivateCf, (objValue, srcValue) => {
|
|
60
|
+
if (loadash.isArray(objValue) && objValue[0] && loadash.isObject(objValue[0])) {
|
|
61
|
+
return objValue.concat(srcValue);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return res;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 根据moduleNames获取modules
|
|
47
70
|
* @param { object } tmsConfig
|
|
48
|
-
* @param { array }
|
|
71
|
+
* @param { array } moduleNames
|
|
72
|
+
* @param {boolean} errorIsQuit 找不到配置文件是否退出
|
|
49
73
|
* @returns
|
|
50
74
|
*/
|
|
51
|
-
const
|
|
75
|
+
const getModulesByModuleNames = function (tmsConfig, moduleNames = []) {
|
|
52
76
|
const targetModules = [];
|
|
53
|
-
|
|
77
|
+
moduleNames.forEach((moduleName) => {
|
|
54
78
|
const module = tmsConfig.modules.all.find(module => module.moduleName === moduleName);
|
|
55
|
-
|
|
79
|
+
if (!module) {
|
|
80
|
+
throw new Error(`你启动的模块${moduleName}在tms.config.js的modules.all中没有注册`);
|
|
81
|
+
}
|
|
82
|
+
targetModules.push(module);
|
|
56
83
|
});
|
|
57
84
|
|
|
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
85
|
return targetModules;
|
|
64
86
|
};
|
|
65
87
|
|
|
@@ -94,25 +116,32 @@ function adaptMpCgContent(fileContent, appName) {
|
|
|
94
116
|
return content;
|
|
95
117
|
}
|
|
96
118
|
|
|
119
|
+
const adaptDependencies = function (dependencies, subPackages) {
|
|
120
|
+
const newDependencies = dependencies || [];
|
|
121
|
+
subPackages.forEach((item) => {
|
|
122
|
+
if (item.dependencies) {
|
|
123
|
+
dependencies = newDependencies.concat(item.dependencies);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return newDependencies;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const adaptSubPackages = function (moduleConfig, appName) {
|
|
130
|
+
const subPackages = isObject(moduleConfig) && moduleConfig.subPackages
|
|
131
|
+
? moduleConfig.subPackages
|
|
132
|
+
: isObject(moduleConfig) ? [moduleConfig] : moduleConfig;
|
|
133
|
+
return adaptMpCgContent(subPackages, appName);
|
|
134
|
+
};
|
|
135
|
+
|
|
97
136
|
/**
|
|
98
137
|
* 获取模块module.config.json中的配置信息
|
|
99
138
|
* @param {array} modules 用户要编译的模块列表
|
|
100
139
|
* @param { string } appName 小程序的名称
|
|
101
|
-
* @param {boolean} notFindIsQuit 找不到配置文件是否退出
|
|
102
140
|
*/
|
|
103
|
-
function getModulesConfig(modules = [], appName
|
|
141
|
+
function getModulesConfig(modules = [], appName) {
|
|
104
142
|
const modulesConfig = [];
|
|
105
143
|
modules.forEach((moduleItem) => {
|
|
106
|
-
if (!moduleItem.path) {
|
|
107
|
-
throw new Error(`${moduleItem.moduleName}模块路径配置没有找到`);
|
|
108
|
-
}
|
|
109
144
|
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
145
|
let moduleConfig;
|
|
117
146
|
try {
|
|
118
147
|
moduleConfig = JSON.parse(fs.readFileSync(moduleConfigPath, 'utf-8'));
|
|
@@ -121,26 +150,15 @@ function getModulesConfig(modules = [], appName, notFindIsQuit) {
|
|
|
121
150
|
}
|
|
122
151
|
|
|
123
152
|
// 兼容历史逻辑,后续可删除--- 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;
|
|
153
|
+
const subPackages = adaptSubPackages(moduleConfig, appName);
|
|
154
|
+
const dependencies = adaptDependencies(moduleConfig.dependencies, subPackages);
|
|
155
|
+
moduleConfig = {
|
|
156
|
+
...(isObject(moduleConfig) ? moduleConfig : {}),
|
|
157
|
+
subPackages,
|
|
158
|
+
dependencies,
|
|
159
|
+
};
|
|
141
160
|
// 兼容逻辑--- end
|
|
142
|
-
|
|
143
|
-
modulesConfig.push(moduleItem);
|
|
161
|
+
modulesConfig.push(moduleConfig);
|
|
144
162
|
});
|
|
145
163
|
|
|
146
164
|
return modulesConfig;
|
|
@@ -154,40 +172,98 @@ function getModulesConfig(modules = [], appName, notFindIsQuit) {
|
|
|
154
172
|
const getSubPackages = (modules) => {
|
|
155
173
|
const newSubPackages = [];
|
|
156
174
|
modules.forEach((module) => {
|
|
157
|
-
(module.subPackages || []).forEach((
|
|
175
|
+
(module.subPackages || []).forEach((subPackage) => {
|
|
158
176
|
newSubPackages.push({
|
|
159
|
-
|
|
160
|
-
...item,
|
|
177
|
+
...subPackage,
|
|
161
178
|
});
|
|
162
179
|
});
|
|
163
180
|
});
|
|
164
181
|
return newSubPackages;
|
|
165
182
|
};
|
|
166
183
|
|
|
184
|
+
/**
|
|
185
|
+
* 获取分包的root字段
|
|
186
|
+
* @param {*} modulePath 模块的编译路径
|
|
187
|
+
* @param {*} root 分包的root字段
|
|
188
|
+
* @returns
|
|
189
|
+
*/
|
|
190
|
+
const getSubPackageRoot = function (modulePath, root) {
|
|
191
|
+
return root.startsWith(modulePath) ? root : `${modulePath}/${root}`;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// 获取分包的源码路径
|
|
195
|
+
const getSubPackageSrcPath = function (tmsConfig, module, subPackage) {
|
|
196
|
+
const srcModulePath = getAbsolutePath(module.path);
|
|
197
|
+
const buildModulePath = resolve(tmsConfig.outputDir, module.modulePath);
|
|
198
|
+
|
|
199
|
+
const subPackageRoot = getSubPackageRoot(module.modulePath, subPackage.root);
|
|
200
|
+
const buildSubPackagePath = resolve(tmsConfig.outputDir, subPackageRoot);
|
|
201
|
+
|
|
202
|
+
const subPackageRelativeModule = path.relative(buildModulePath, buildSubPackagePath);
|
|
203
|
+
const srcSubPackagePath = path.join(srcModulePath, subPackageRelativeModule);
|
|
204
|
+
|
|
205
|
+
return srcSubPackagePath;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const checkModuleItem = (tmsConfig, tmsModuleItem, moduleConfig) => {
|
|
209
|
+
const newModuleItem = { ...tmsModuleItem, ...moduleConfig };
|
|
210
|
+
|
|
211
|
+
// 兼容逻辑
|
|
212
|
+
if (!newModuleItem.moduleName) newModuleItem.moduleName = newModuleItem.name;
|
|
213
|
+
delete newModuleItem.name;
|
|
214
|
+
|
|
215
|
+
// 参数校验-模块源码路径
|
|
216
|
+
if (!newModuleItem.path) {
|
|
217
|
+
throw new Error(`${newModuleItem.moduleName}模块没有找到path字段,请检查tms.config.js的modules.all>module>path路径`);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// 参数校验-模块编译路径
|
|
221
|
+
if (!newModuleItem.modulePath) {
|
|
222
|
+
throw new Error(`${newModuleItem.moduleName}模块的module.config.json中没有找到modulePath字段`);
|
|
223
|
+
}
|
|
224
|
+
// 参数校验-分包校验
|
|
225
|
+
for (const subPackage of newModuleItem.subPackages) {
|
|
226
|
+
if (!subPackage.root) {
|
|
227
|
+
throw new Error(`${newModuleItem.moduleName}模块的module.config.json中没有找到${subPackage.name}分包的root字段`);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// 参数校验-判断分包源码目录是否存在
|
|
231
|
+
const subPackageSrcPath = getSubPackageSrcPath(tmsConfig, newModuleItem, subPackage);
|
|
232
|
+
if (!subPackageSrcPath || !fs.existsSync(subPackageSrcPath)) {
|
|
233
|
+
throw new Error(`没有找到${newModuleItem.moduleName}模块的${subPackage.name}分包源码:${subPackageSrcPath}`);
|
|
234
|
+
}
|
|
235
|
+
subPackage.path = subPackageSrcPath;
|
|
236
|
+
}
|
|
237
|
+
return newModuleItem;
|
|
238
|
+
};
|
|
239
|
+
|
|
167
240
|
/**
|
|
168
241
|
* 获取所有的模块,合并模块的依赖模块
|
|
169
242
|
* @param { object } tmsConfig
|
|
170
243
|
* @param {array} modules
|
|
171
|
-
* @param {boolean}
|
|
244
|
+
* @param {boolean} errorIsQuit 找不到配置文件是否退出
|
|
172
245
|
* @returns
|
|
173
246
|
*/
|
|
174
|
-
const getModulesByMergeDepModules = (tmsConfig, modules,
|
|
247
|
+
const getModulesByMergeDepModules = (tmsConfig, modules, errorIsQuit = false) => {
|
|
175
248
|
const allModules = new Map();
|
|
176
249
|
function dfs(tmsConfig, modules) {
|
|
177
250
|
modules.forEach((moduleItem) => {
|
|
178
|
-
const
|
|
251
|
+
const moduleConfigPath = resolve(moduleItem.path, MODULE_CONFIG_FILENAME);
|
|
252
|
+
if (!fs.existsSync(moduleConfigPath)) {
|
|
253
|
+
if (!allModules.has(moduleItem.moduleName)) {
|
|
254
|
+
allModules.set(moduleItem.moduleName, moduleItem);
|
|
255
|
+
}
|
|
256
|
+
if (errorIsQuit) {
|
|
257
|
+
throw new Error(`${moduleItem.moduleName}模块的配置文件module.config.json在${moduleItem.path}目录下没有找到`);
|
|
258
|
+
}
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const [moduleConfig = {}] = getModulesConfig([moduleItem], tmsConfig.appName);
|
|
179
262
|
|
|
180
263
|
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
|
-
});
|
|
264
|
+
allModules.set(moduleItem.moduleName, checkModuleItem(tmsConfig, moduleItem, moduleConfig));
|
|
265
|
+
|
|
266
|
+
const dependenciesModules = getModulesByModuleNames(tmsConfig, moduleConfig?.dependencies);
|
|
191
267
|
if (dependenciesModules.length) {
|
|
192
268
|
dfs(tmsConfig, dependenciesModules);
|
|
193
269
|
}
|
|
@@ -207,7 +283,8 @@ module.exports = {
|
|
|
207
283
|
readTmsConfig,
|
|
208
284
|
readTmsPrivateCf,
|
|
209
285
|
getModulesConfig,
|
|
210
|
-
|
|
286
|
+
getModulesByModuleNames,
|
|
211
287
|
getSubPackages,
|
|
212
288
|
getModulesByMergeDepModules,
|
|
289
|
+
getTmsConfig,
|
|
213
290
|
};
|
package/src/entry.js
CHANGED
package/src/index.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
const chalk = require('chalk');
|
|
2
2
|
const commander = require('commander');
|
|
3
|
-
const { suggestCommands } = require('./utils/widgets');
|
|
3
|
+
const { suggestCommands, resolve } = require('./utils/widgets');
|
|
4
4
|
const { info } = require('./utils/log');
|
|
5
|
+
const { getTmsConfig } = require('../src/core/tmsMpconfig');
|
|
5
6
|
const { TMS_NAME } = require('./config/constant.js');
|
|
6
7
|
const commands = require('./entry');
|
|
7
|
-
const
|
|
8
|
+
const check = require('./check');
|
|
9
|
+
const fs = require('fs');
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
check();
|
|
10
12
|
|
|
11
13
|
const program = new commander.Command(TMS_NAME);
|
|
12
14
|
|
|
@@ -36,7 +38,19 @@ function registerCommand(program, commands) {
|
|
|
36
38
|
});
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
|
|
41
|
+
function register() {
|
|
42
|
+
// 注册脚手架内部命令
|
|
43
|
+
registerCommand(program, commands);
|
|
44
|
+
|
|
45
|
+
// 注册扩展命令
|
|
46
|
+
if (fs.existsSync(resolve('tms.config.js'))) {
|
|
47
|
+
const tmsConfig = getTmsConfig();
|
|
48
|
+
if (Array.isArray(tmsConfig?.commands)) {
|
|
49
|
+
registerCommand(program, tmsConfig.commands);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
register();
|
|
40
54
|
|
|
41
55
|
program.on('--help', () => {
|
|
42
56
|
info(` Run ${chalk.cyan(`${TMS_NAME} <command> --help`)} for detailed usage of given command.`);
|
|
@@ -7,7 +7,7 @@ async function build(tmsConfig, targetModules) {
|
|
|
7
7
|
// 开始构建前,清理输出目录
|
|
8
8
|
await shelljs.rm('-rf', resolve(tmsConfig.outputDir));
|
|
9
9
|
|
|
10
|
-
const { modules: newModules
|
|
10
|
+
const { modules: newModules } = await init(tmsConfig, targetModules);
|
|
11
11
|
|
|
12
12
|
const isDev = false;
|
|
13
13
|
if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
|
|
@@ -16,7 +16,7 @@ async function build(tmsConfig, targetModules) {
|
|
|
16
16
|
tmsConfig: filterField(tmsConfig, ['gitAccount']),
|
|
17
17
|
modules: newModules });
|
|
18
18
|
};
|
|
19
|
-
compileBuild(tmsConfig, newModules,
|
|
19
|
+
compileBuild(tmsConfig, newModules, isDev);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
module.exports = build;
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
const { symLink } = require('../../../core/symbolicLink');
|
|
2
2
|
const { handleError } = require('../../../core/handleError');
|
|
3
|
-
const { succeed } = require('../../../utils/log');
|
|
4
3
|
|
|
5
4
|
module.exports = async (tmsConfig) => {
|
|
6
5
|
try {
|
|
7
6
|
await symLink(tmsConfig);
|
|
8
|
-
succeed('云函数创建软链成功');
|
|
9
7
|
} catch (e) {
|
|
10
8
|
handleError(`创建软链错误: ${e}`);
|
|
11
9
|
}
|