@tmsfe/tmskit 0.0.19 → 0.0.22
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 -1
- package/dist/index.cjs.js +1772 -1860
- package/package.json +13 -14
- 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 +33 -72
- package/src/compile/plugins/mpProjectJson.js +13 -0
- package/src/compile/watch.js +79 -28
- package/src/config/constant.js +48 -9
- package/src/core/buildAppJson.js +17 -21
- package/src/core/checkDependencies.js +41 -21
- package/src/core/cloneModules.js +8 -7
- package/src/core/handleError.js +7 -1
- package/src/core/npm.js +13 -27
- 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 +189 -97
- package/src/entry.js +16 -17
- package/src/index.js +61 -20
- package/src/scripts/create/generator.js +1 -2
- package/src/scripts/create/index.js +75 -32
- package/src/scripts/extend-cmd/index.js +74 -0
- package/src/scripts/run/build/index.js +7 -4
- package/src/scripts/run/cloud/index.js +0 -2
- package/src/scripts/run/dev/index.js +22 -15
- package/src/scripts/run/index.js +50 -60
- package/src/scripts/run/init/index.js +13 -34
- package/src/{core → scripts/run/install}/cache.js +4 -4
- package/src/scripts/run/install/index.js +18 -21
- package/src/utils/log.js +3 -0
- package/src/utils/widgets.js +58 -34
|
@@ -2,22 +2,22 @@ const shelljs = require('shelljs');
|
|
|
2
2
|
const fs = require('fs');
|
|
3
3
|
const path = require('path');
|
|
4
4
|
const io = require('../../../utils/io');
|
|
5
|
-
const { createTask, resolve } = require('../../../utils/widgets');
|
|
5
|
+
const { createTask, resolve, getAbsolutePath } = require('../../../utils/widgets');
|
|
6
6
|
const { buildMpNpm } = require('../../../core/mpCi');
|
|
7
|
-
const { setCache, getCache } = require('
|
|
8
|
-
const {
|
|
7
|
+
const { setCache, getCache } = require('./cache');
|
|
8
|
+
const { NODE_MODULES_DIR } = require('../../../config/constant');
|
|
9
9
|
const { npmInstallAll } = require('../../../core/npm');
|
|
10
|
-
const {
|
|
10
|
+
const { info } = require('../../../utils/log');
|
|
11
11
|
const { fileMd5 } = require('../../../utils/md5');
|
|
12
12
|
const { isDependenciesUpdate } = require('../../../core/checkDependencies');
|
|
13
13
|
|
|
14
|
-
async function install(tmsConfig,
|
|
14
|
+
async function install(tmsConfig, subPackages, useCache = true) {
|
|
15
15
|
const cwd = process.cwd();
|
|
16
16
|
const npmInstallRes = await createTask(
|
|
17
17
|
npmInstall,
|
|
18
18
|
'小程序 开始npm install',
|
|
19
19
|
'小程序npm install 完成',
|
|
20
|
-
)(tmsConfig,
|
|
20
|
+
)(tmsConfig, subPackages, useCache);
|
|
21
21
|
|
|
22
22
|
// 如果npm install 没有命中缓存,则说明node_module有更新,此时必须构建miniprogram_npm
|
|
23
23
|
if (!npmInstallRes.isCache) {
|
|
@@ -26,48 +26,44 @@ async function install(tmsConfig, modules, env, useCache = true) {
|
|
|
26
26
|
mpCiInstall,
|
|
27
27
|
'开始构建miniprogram_npm',
|
|
28
28
|
'构建miniprogram_npm 完成',
|
|
29
|
-
)(tmsConfig,
|
|
29
|
+
)(tmsConfig, subPackages, false);
|
|
30
30
|
} else {
|
|
31
31
|
// 构建miniprogram_npm
|
|
32
32
|
await createTask(
|
|
33
33
|
mpCiInstall,
|
|
34
34
|
'开始构建miniprogram_npm',
|
|
35
35
|
'构建miniprogram_npm 完成',
|
|
36
|
-
)(tmsConfig,
|
|
36
|
+
)(tmsConfig, subPackages, useCache);
|
|
37
37
|
}
|
|
38
38
|
shelljs.cd(cwd);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
async function npmInstall(tmsConfig,
|
|
41
|
+
async function npmInstall(tmsConfig, subPackages, useCache) {
|
|
42
42
|
// 如果依赖没有更新和使用缓存数据(则命中缓存)
|
|
43
|
-
if (!isDependenciesUpdate(
|
|
43
|
+
if (!isDependenciesUpdate(subPackages, resolve('./'), tmsConfig.outputDir) && useCache) {
|
|
44
44
|
info('node_modules命中缓存');
|
|
45
45
|
return { isCache: true };
|
|
46
46
|
}
|
|
47
47
|
// 拷贝模块的package.json到编译输出目录
|
|
48
|
-
|
|
48
|
+
subPackages.forEach((item) => {
|
|
49
49
|
const outputModuleDir = resolve(`${tmsConfig.outputDir}/${item.root}`);
|
|
50
|
-
if (!fs.existsSync(resolve(item.path))) {
|
|
51
|
-
fail(`${item.path}模块代码路径不存在, 请检查tms.config.js的${item.name}模块的path`);
|
|
52
|
-
process.exit(1);
|
|
53
|
-
}
|
|
54
50
|
io.ensureDirExist(outputModuleDir);
|
|
55
|
-
const modulePackagePath =
|
|
51
|
+
const modulePackagePath = `${getAbsolutePath(item.path)}/package.json`;
|
|
56
52
|
if (fs.existsSync(modulePackagePath)) shelljs.cp('-Rf', modulePackagePath, outputModuleDir);
|
|
57
53
|
});
|
|
58
54
|
|
|
59
|
-
await npmInstallAll(
|
|
55
|
+
await npmInstallAll(subPackages, resolve(tmsConfig.outputDir), NODE_MODULES_DIR);
|
|
60
56
|
return { isCache: false };
|
|
61
57
|
}
|
|
62
58
|
|
|
63
59
|
// 构建miniprogram_npm
|
|
64
|
-
async function mpCiInstall(tmsConfig,
|
|
60
|
+
async function mpCiInstall(tmsConfig, subPackages, useCache) {
|
|
65
61
|
const packageJsonFiles = [];
|
|
66
62
|
const rootPackFile = resolve(`${tmsConfig.outputDir}/package.json`);
|
|
67
63
|
if (fs.existsSync(rootPackFile)) {
|
|
68
64
|
packageJsonFiles.push(rootPackFile);
|
|
69
65
|
}
|
|
70
|
-
|
|
66
|
+
subPackages.forEach((item) => {
|
|
71
67
|
const packageJsonFile = resolve(`${tmsConfig.outputDir}/${item.root}/package.json`);
|
|
72
68
|
if (fs.existsSync(packageJsonFile)) {
|
|
73
69
|
packageJsonFiles.push(packageJsonFile);
|
|
@@ -121,8 +117,9 @@ async function getMNPMd5(cwd) {
|
|
|
121
117
|
shelljs.cd(cwd);
|
|
122
118
|
if (fs.existsSync('miniprogram_npm')) {
|
|
123
119
|
shelljs.exec('tar -cvf ./miniprogram_npm.tar.gz ./miniprogram_npm', { cwd, ...shellJsOption });
|
|
124
|
-
const
|
|
125
|
-
|
|
120
|
+
const tarDir = path.join(cwd, './miniprogram_npm.tar.gz');
|
|
121
|
+
const md5Value = await fileMd5(tarDir);
|
|
122
|
+
shelljs.rm('-rf', tarDir);
|
|
126
123
|
return md5Value;
|
|
127
124
|
}
|
|
128
125
|
return '';
|
package/src/utils/log.js
CHANGED
|
@@ -40,9 +40,12 @@ const warn = (message) => {
|
|
|
40
40
|
|
|
41
41
|
const info = (...args) => console.log(`${moment().format('YYYY-MM-DD HH:mm:ss')}`, ...args);
|
|
42
42
|
|
|
43
|
+
const infoNoTime = (...args) => console.log(...args);
|
|
44
|
+
|
|
43
45
|
module.exports = {
|
|
44
46
|
fail,
|
|
45
47
|
succeed,
|
|
46
48
|
warn,
|
|
47
49
|
info,
|
|
50
|
+
infoNoTime,
|
|
48
51
|
};
|
package/src/utils/widgets.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
|
|
2
|
-
const program = require('commander');
|
|
3
|
-
const leven = require('leven');
|
|
4
2
|
const ora = require('ora');
|
|
5
3
|
const path = require('path');
|
|
6
4
|
const fs = require('fs');
|
|
7
5
|
const shelljs = require('shelljs');
|
|
8
6
|
const glob = require('glob-ignore');
|
|
9
7
|
const { info } = require('./log');
|
|
10
|
-
const chalk = require('chalk');
|
|
11
8
|
const shelljsOptions = { slient: true, async: false };
|
|
12
9
|
|
|
13
10
|
// 获取当前目录
|
|
@@ -16,28 +13,6 @@ function resolve(...args) {
|
|
|
16
13
|
return path.resolve(cwd, ...args);
|
|
17
14
|
};
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 用户输入命令时,进行提示
|
|
22
|
-
* @param {String} unknownCommand 非预期的命令
|
|
23
|
-
* @returns {Undefined} 无需返回值
|
|
24
|
-
*/
|
|
25
|
-
const suggestCommands = (unknownCommand) => {
|
|
26
|
-
const availableCommands = program.commands.map(cmd => cmd._name);
|
|
27
|
-
|
|
28
|
-
let suggestion;
|
|
29
|
-
availableCommands.forEach((cmd) => {
|
|
30
|
-
const isBestMatch = leven(cmd, unknownCommand) < leven(suggestion || '', unknownCommand);
|
|
31
|
-
if (leven(cmd, unknownCommand) < 3 && isBestMatch) {
|
|
32
|
-
suggestion = cmd;
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
if (suggestion) {
|
|
37
|
-
info(` ${chalk.red(`Did you mean ${chalk.yellow(suggestion)}?`)}`);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
|
|
41
16
|
/**
|
|
42
17
|
* 判断变量是否是一个数组
|
|
43
18
|
* @param { unknown } obj 变量
|
|
@@ -109,13 +84,15 @@ function pullRepoForGit(dest, branch) {
|
|
|
109
84
|
|
|
110
85
|
/**
|
|
111
86
|
* npm 下载依赖
|
|
112
|
-
* @param {
|
|
87
|
+
* @param {string} dir 下载npm的目录
|
|
88
|
+
* @param {object} npm npm的配置 (见下获取npm源入参)
|
|
113
89
|
* @returns
|
|
114
90
|
*/
|
|
115
|
-
function npmInstall(dir) {
|
|
91
|
+
function npmInstall(dir, npmConfig) {
|
|
116
92
|
return new Promise((resolve, reject) => {
|
|
93
|
+
const registry = getNpmRegistry(npmConfig);
|
|
117
94
|
shelljs.exec(
|
|
118
|
-
|
|
95
|
+
`npm install --production ${registry}`,
|
|
119
96
|
{ cwd: dir, silent: true },
|
|
120
97
|
(code, stdout, stderr) => {
|
|
121
98
|
if (code !== 0) {
|
|
@@ -127,6 +104,28 @@ function npmInstall(dir) {
|
|
|
127
104
|
});
|
|
128
105
|
}
|
|
129
106
|
|
|
107
|
+
// 获取npm源
|
|
108
|
+
// 入参:{
|
|
109
|
+
// registry: "https://registry.npmjs.org/",
|
|
110
|
+
// scope: {
|
|
111
|
+
// "@tencent": "http://mirrors.tencent.com/npm/"
|
|
112
|
+
// }
|
|
113
|
+
// }
|
|
114
|
+
// 出参: --@tencent:registry=http://mirrors.tencent.com/npm/ --registry=https://registry.npmjs.org/
|
|
115
|
+
function getNpmRegistry(npmConfig = {}) {
|
|
116
|
+
let resRegistry = '';
|
|
117
|
+
if (npmConfig.registry) {
|
|
118
|
+
resRegistry = ` --registry=${npmConfig.registry}`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (isObject(npmConfig.scope)) {
|
|
122
|
+
Object.keys(npmConfig.scope).forEach((key) => {
|
|
123
|
+
resRegistry += ` --${key}:registry=${npmConfig.scope[key]}`;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return resRegistry;
|
|
127
|
+
}
|
|
128
|
+
|
|
130
129
|
/**
|
|
131
130
|
* 计算各项任务耗时
|
|
132
131
|
* @param {Number} start 任务开始时间
|
|
@@ -167,11 +166,7 @@ const camelize = str => str.replace(/-(\w)/g, (a, c) => (c ? c.toUpperCase() : '
|
|
|
167
166
|
|
|
168
167
|
const mergeMap = function (obj, src) {
|
|
169
168
|
for (const [k, v] of src) {
|
|
170
|
-
|
|
171
|
-
obj.set(k, obj.get(k) + v);
|
|
172
|
-
} else {
|
|
173
|
-
obj.set(k, v);
|
|
174
|
-
}
|
|
169
|
+
obj.set(k, v);
|
|
175
170
|
}
|
|
176
171
|
|
|
177
172
|
return obj;
|
|
@@ -209,6 +204,33 @@ function findFiles(globPath, filter = []) {
|
|
|
209
204
|
});
|
|
210
205
|
}
|
|
211
206
|
|
|
207
|
+
// 获取绝对路径
|
|
208
|
+
function getAbsolutePath(pathDir, cwd = '') {
|
|
209
|
+
let newPath = pathDir;
|
|
210
|
+
newPath = newPath.startsWith('/') ? newPath : resolve(cwd, newPath);
|
|
211
|
+
newPath = newPath.endsWith('/') ? newPath.slice(0, newPath.length - 1) : newPath;
|
|
212
|
+
return newPath;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// 版本比较 => 1(大于), 0(等于), -1(小于)
|
|
216
|
+
function versionCompare(v1, v2) {
|
|
217
|
+
// 将两个版本号拆成数组
|
|
218
|
+
const arr1 = v1.split('.');
|
|
219
|
+
const arr2 = v2.split('.');
|
|
220
|
+
const minLength = Math.min(arr1.length, arr2.length);
|
|
221
|
+
// 依次比较版本号每一位大小
|
|
222
|
+
for (let i = 0; i < minLength; i++) {
|
|
223
|
+
if (parseInt(arr1[i], 10) !== parseInt(arr2[i], 10)) {
|
|
224
|
+
return (parseInt(arr1[i], 10) > parseInt(arr2[i], 10)) ? 1 : -1;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
// 若前几位分隔相同,则按分隔数比较。
|
|
228
|
+
if (arr1.length === arr2.length) {
|
|
229
|
+
return 0;
|
|
230
|
+
}
|
|
231
|
+
return (arr1.length > arr2.length) ? 1 : -1;
|
|
232
|
+
}
|
|
233
|
+
|
|
212
234
|
module.exports = {
|
|
213
235
|
resolve,
|
|
214
236
|
isObject,
|
|
@@ -216,11 +238,13 @@ module.exports = {
|
|
|
216
238
|
createTask,
|
|
217
239
|
downloadRepoForGit,
|
|
218
240
|
pullRepoForGit,
|
|
219
|
-
suggestCommands,
|
|
220
241
|
camelize,
|
|
221
242
|
npmInstall,
|
|
222
243
|
mergeMap,
|
|
223
244
|
relativeCwdPath,
|
|
224
245
|
filterField,
|
|
225
246
|
findFiles,
|
|
247
|
+
getAbsolutePath,
|
|
248
|
+
getNpmRegistry,
|
|
249
|
+
versionCompare,
|
|
226
250
|
};
|