@tmsfe/tmskit 0.0.5 → 0.0.8
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/README.md +18 -2
- package/dist/index.cjs.js +2286 -1258
- package/package.json +24 -8
- package/src/compile/build.js +5 -0
- package/src/compile/compile.js +90 -0
- package/src/compile/dev.js +129 -0
- package/src/compile/plugins/less.js +116 -0
- package/src/compile/plugins/mpCommonDep.js +131 -0
- package/src/compile/plugins/mpJsonDep.js +112 -0
- package/src/compile/plugins/mpWxmlDep.js +194 -0
- package/src/compile/plugins/postcss-font-base64.js +72 -0
- package/src/compile/plugins/replaceEnv.js +29 -0
- package/src/compile/plugins/utils/pluginError.js +25 -0
- package/src/config/constant.js +11 -10
- package/src/config/defaultTmsConfig.js +9 -10
- package/src/core/buildAppJson.js +166 -0
- package/src/{utils → core}/checkDependencies.js +6 -6
- package/src/core/cloneModules.js +203 -0
- package/src/core/handleError.js +18 -0
- package/src/core/isInIt.js +69 -0
- package/src/{utils/mpCiUtils.js → core/mpCi.js} +0 -1
- package/src/core/npm.js +218 -0
- package/src/core/symbolicLink.js +24 -0
- package/src/core/tmsMpconfig.js +234 -0
- package/src/entry.js +50 -8
- package/src/index.js +31 -15
- package/src/init.js +2 -7
- package/src/scripts/create/index.js +2 -2
- package/src/scripts/run/build/index.js +3 -3
- package/src/scripts/run/dev/index.js +27 -52
- package/src/scripts/run/index.js +70 -36
- package/src/scripts/run/init/index.js +40 -42
- package/src/scripts/run/install/index.js +21 -29
- package/src/utils/findCssImport.js +30 -0
- package/src/utils/global.js +19 -33
- package/src/utils/io.js +86 -0
- package/src/utils/log.js +3 -0
- package/src/utils/widgets.js +59 -51
- package/CHANGELOG.md +0 -0
- package/main.js +0 -3
- package/rollup.config.js +0 -179
- package/src/utils/buildAppJson.js +0 -144
- package/src/utils/cliUtils.js +0 -35
- package/src/utils/cloneModules.js +0 -90
- package/src/utils/npmUtils.js +0 -126
- package/src/utils/tkitUtils.js +0 -84
- package/src/webpack/base.js +0 -65
- package/src/webpack/build.js +0 -21
- package/src/webpack/buildServer.js +0 -34
- package/src/webpack/dev.js +0 -31
- package/src/webpack/devServer.js +0 -37
- package/src/webpack/plugins/entryExtractPlugin/index.js +0 -28
- package/src/webpack/utils.js +0 -244
package/rollup.config.js
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import ts from 'rollup-plugin-typescript2';
|
|
3
|
-
import babel from '@rollup/plugin-babel';
|
|
4
|
-
import json from '@rollup/plugin-json';
|
|
5
|
-
import replace from 'rollup-plugin-replace';
|
|
6
|
-
import nodeResolve from 'rollup-plugin-node-resolve';
|
|
7
|
-
import commonjs from '@rollup/plugin-commonjs';
|
|
8
|
-
|
|
9
|
-
if (!process.env.TARGET) {
|
|
10
|
-
throw new Error('TARGET package must be specified via --environment flag.');
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// 指定包地址
|
|
14
|
-
const packageDir = path.resolve(__dirname);
|
|
15
|
-
// 包名
|
|
16
|
-
const name = path.basename(packageDir);
|
|
17
|
-
// 环境
|
|
18
|
-
const interEnv = process.env.INTER_ENV || 'public';
|
|
19
|
-
|
|
20
|
-
// eslint-disable-next-line
|
|
21
|
-
const packageJson = require('./package.json');
|
|
22
|
-
|
|
23
|
-
const packageOptions = packageJson.buildOptions || {};
|
|
24
|
-
|
|
25
|
-
function resolve(name) { // eslint-disable-line
|
|
26
|
-
return path.resolve(packageDir, name);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const outputConfig = {
|
|
30
|
-
esm: {
|
|
31
|
-
dir: resolve('dist'),
|
|
32
|
-
entryFileNames: '[name].js',
|
|
33
|
-
format: 'es',
|
|
34
|
-
},
|
|
35
|
-
'esm-browser': {
|
|
36
|
-
dir: resolve('dist'),
|
|
37
|
-
entryFileNames: '[name].esm-browser.js',
|
|
38
|
-
format: 'es',
|
|
39
|
-
},
|
|
40
|
-
cjs: {
|
|
41
|
-
dir: resolve('dist'),
|
|
42
|
-
entryFileNames: '[name].cjs.js',
|
|
43
|
-
format: 'cjs',
|
|
44
|
-
},
|
|
45
|
-
global: {
|
|
46
|
-
dir: resolve('dist'),
|
|
47
|
-
entryFileNames: '[name].global.js',
|
|
48
|
-
format: 'iife',
|
|
49
|
-
},
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const defaultFormats = ['esm'];
|
|
53
|
-
// 打包格式
|
|
54
|
-
const packageFormats = packageOptions.formats || defaultFormats;
|
|
55
|
-
|
|
56
|
-
const packageConfigs = packageFormats.map(format => createConfig(format, outputConfig[format]));
|
|
57
|
-
|
|
58
|
-
export default packageConfigs;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* format rollup package configuration object
|
|
62
|
-
* @param {string} format 文件打包格式
|
|
63
|
-
* @param {object} output 输出对象
|
|
64
|
-
* @param {array} plugins 插件
|
|
65
|
-
* @returns {object} rollup配置对象
|
|
66
|
-
*/
|
|
67
|
-
function createConfig(format, output, plugins = []) {
|
|
68
|
-
if (!output) {
|
|
69
|
-
// eslint-disable-next-line
|
|
70
|
-
console.log(require('chalk').yellow(`invalid format: "${format}"`)); // eslint-disable-line no-console
|
|
71
|
-
process.exit(1);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
output.sourceMap = true; // eslint-disable-line
|
|
75
|
-
output.externalLiveBindings = false; // eslint-disable-line
|
|
76
|
-
|
|
77
|
-
// 兼容不同的包的入口文件 -- 后面会统一为index.ts
|
|
78
|
-
const entryFile = setEntryFile(name);
|
|
79
|
-
|
|
80
|
-
// 定制化cjs的插件和esmodule的插件。
|
|
81
|
-
const extralPlugin = setEsPlugin(output.format);
|
|
82
|
-
|
|
83
|
-
return {
|
|
84
|
-
input: entryFile,
|
|
85
|
-
output,
|
|
86
|
-
external: [],
|
|
87
|
-
plugins: [
|
|
88
|
-
json({
|
|
89
|
-
namedExports: false,
|
|
90
|
-
}),
|
|
91
|
-
commonjs({
|
|
92
|
-
ignoreDynamicRequires: true,
|
|
93
|
-
// include: 'node_modules/**', // Default: undefined
|
|
94
|
-
browser: true,
|
|
95
|
-
}),
|
|
96
|
-
babel({
|
|
97
|
-
plugins: ['@babel/plugin-proposal-nullish-coalescing-operator', '@babel/plugin-proposal-optional-chaining'],
|
|
98
|
-
// exclude: ['node_modules/**'],
|
|
99
|
-
}),
|
|
100
|
-
// terser(),
|
|
101
|
-
createReplacePlugin(),
|
|
102
|
-
...extralPlugin,
|
|
103
|
-
...plugins,
|
|
104
|
-
],
|
|
105
|
-
onwarn: (msg, warn) => {
|
|
106
|
-
if (!/Circular/.test(msg)) {
|
|
107
|
-
warn(msg);
|
|
108
|
-
}
|
|
109
|
-
},
|
|
110
|
-
treeshake: {
|
|
111
|
-
moduleSideEffects: false,
|
|
112
|
-
},
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* 兼容现在的包会存在不同入口的情况。 后面会统一为index.ts
|
|
118
|
-
* @param { string } packageName 包名称
|
|
119
|
-
* @returns { Object } entryFile
|
|
120
|
-
*/
|
|
121
|
-
function setEntryFile(packageName) {
|
|
122
|
-
let entryFile = resolve('src/index.js');
|
|
123
|
-
|
|
124
|
-
switch (packageName) {
|
|
125
|
-
case 'tms-core':
|
|
126
|
-
entryFile = {
|
|
127
|
-
index: resolve('src/index'),
|
|
128
|
-
request: resolve('src/request'),
|
|
129
|
-
cloudService: resolve('src/cloudService'),
|
|
130
|
-
};
|
|
131
|
-
if (interEnv !== 'public') {
|
|
132
|
-
entryFile['index-proxy'] = resolve('src/index-proxy');
|
|
133
|
-
}
|
|
134
|
-
break;
|
|
135
|
-
case 'tms-websdk':
|
|
136
|
-
entryFile = resolve('src/index.ts');
|
|
137
|
-
break;
|
|
138
|
-
default:
|
|
139
|
-
entryFile = resolve('src/index.js');
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
return entryFile;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* 定制化cjs和esmodule的插件
|
|
147
|
-
* @param { string } format 打包格式
|
|
148
|
-
* @returns { Array<plugin> } 插件数组
|
|
149
|
-
*/
|
|
150
|
-
function setEsPlugin(format) {
|
|
151
|
-
const extralPlugins = [];
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
if (format.indexOf('es') > -1) {
|
|
155
|
-
extralPlugins.push(ts({
|
|
156
|
-
tsconfig: resolve('tsconfig.json'),
|
|
157
|
-
}));
|
|
158
|
-
};
|
|
159
|
-
if (format.indexOf('iife') > -1) {
|
|
160
|
-
extralPlugins.push(nodeResolve({ jsnext: true, preferBuiltins: true, browser: true }));
|
|
161
|
-
}
|
|
162
|
-
return extralPlugins;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* 创建变量替换插件
|
|
167
|
-
* @returns {function} 插件函数
|
|
168
|
-
*/
|
|
169
|
-
function createReplacePlugin() {
|
|
170
|
-
const replacements = {
|
|
171
|
-
tmsCore: interEnv === 'public' ? '@tmsfe/tms-core' : '@tmsfe/tms-core',
|
|
172
|
-
INTER_ENV_VB: JSON.stringify(interEnv || 'private'),
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
return replace({
|
|
176
|
-
values: replacements,
|
|
177
|
-
preventAssignment: true,
|
|
178
|
-
});
|
|
179
|
-
}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const { DEFAULT_MODULE_DIR, MODULE_CONFIG_FILENAME } = require('../config/constant');
|
|
3
|
-
const { fail } = require('./log');
|
|
4
|
-
const { resolve, isObject, isArray } = require('./widgets');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 获取每个模块下面的信息,并且收集,后续更新到appJson里面
|
|
8
|
-
* @param { object } file 操作目录下面所有的文件
|
|
9
|
-
* @param { string } appName 小程序的名称
|
|
10
|
-
*/
|
|
11
|
-
function setModuleConfig(file, appName, moduleDir) {
|
|
12
|
-
const content = file.contents ? JSON.parse(file.contents.toString()) : JSON.parse(file);
|
|
13
|
-
|
|
14
|
-
if (isObject(content)) {
|
|
15
|
-
content.root = content.root.indexOf(moduleDir) > -1 ? content.root : `${moduleDir}/${content.root}`;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (isArray(content)) {
|
|
19
|
-
let i = content.length - 1;
|
|
20
|
-
while (i >= 0) {
|
|
21
|
-
let current = content[i];
|
|
22
|
-
current.root = current.root.indexOf(moduleDir) > -1 ? current.root : `${moduleDir}/${current.root}`;
|
|
23
|
-
|
|
24
|
-
if (appName && current.mpConfig && current.mpConfig[appName]) {
|
|
25
|
-
current = { ...current, ...current.mpConfig[appName] };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
delete current.mpConfig;
|
|
29
|
-
delete current.isSubpackages;
|
|
30
|
-
|
|
31
|
-
content[i] = current;
|
|
32
|
-
i--; // eslint-disable-line
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return content;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 递归获取本地所有模块的配置信息
|
|
40
|
-
*/
|
|
41
|
-
function getLocalModuleConfig(modules = [], appName, moduleDir, moduleConfigFilename) {
|
|
42
|
-
const modulesConfig = {};
|
|
43
|
-
|
|
44
|
-
modules.forEach(({ path }) => {
|
|
45
|
-
const moduleConfigPath = resolve(path, moduleConfigFilename);
|
|
46
|
-
if (fs.existsSync(moduleConfigPath)) {
|
|
47
|
-
const content = fs.readFileSync(moduleConfigPath, 'utf-8');
|
|
48
|
-
modulesConfig[moduleConfigPath] = setModuleConfig(content, appName, moduleDir);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
return modulesConfig;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 更新appJson里面的首页配置
|
|
56
|
-
* @param { object } appJson appJson信息
|
|
57
|
-
* @param { array } mainPackages 小程序主包信息
|
|
58
|
-
* @returns { object } appJson小程序主页配置信息
|
|
59
|
-
*/
|
|
60
|
-
function updateMainPackages(appJson, mainPackages = []) {
|
|
61
|
-
let foundMainPackages = appJson.subpackages.filter(subpackage => mainPackages.includes(subpackage.name));
|
|
62
|
-
if (foundMainPackages.length === 0) {
|
|
63
|
-
// 没找到主包
|
|
64
|
-
foundMainPackages = [appJson.subpackages[0]];
|
|
65
|
-
}
|
|
66
|
-
// 拼装 app.pages
|
|
67
|
-
foundMainPackages.forEach((subpackage) => {
|
|
68
|
-
if (!subpackage.pages || !subpackage.pages.length) {
|
|
69
|
-
fail(`主包 ${subpackage} 不能没有 pages`);
|
|
70
|
-
process.exit(-1);
|
|
71
|
-
}
|
|
72
|
-
subpackage.pages.forEach((page) => {
|
|
73
|
-
appJson.pages.push(`${subpackage.root}/${page}`);
|
|
74
|
-
});
|
|
75
|
-
if (subpackage.plugins) {
|
|
76
|
-
Object.assign(appJson.plugins, subpackage.plugins);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
// 去掉 subpackages 中的主包配置
|
|
81
|
-
const foundMainPackageNames = foundMainPackages.map(item => item.name);
|
|
82
|
-
// eslint-disable-next-line
|
|
83
|
-
appJson.subpackages = appJson.subpackages.filter(subpackage => !foundMainPackageNames.includes(subpackage.name));
|
|
84
|
-
|
|
85
|
-
return appJson;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* 获取app.json内容
|
|
89
|
-
* @param {string} sourceAppJsonPath
|
|
90
|
-
* @returns
|
|
91
|
-
*/
|
|
92
|
-
const getAppJsonContent = (sourceAppJsonPath) => {
|
|
93
|
-
if (!fs.existsSync(sourceAppJsonPath)) {
|
|
94
|
-
fail(`当前路径 ${sourceAppJsonPath} 没找到app.json`);
|
|
95
|
-
process.exit(1);
|
|
96
|
-
}
|
|
97
|
-
const appJson = JSON.parse(fs.readFileSync(sourceAppJsonPath), 'utf-8');
|
|
98
|
-
// 加入默认值
|
|
99
|
-
appJson.subpackages = [];
|
|
100
|
-
appJson.pages = [];
|
|
101
|
-
appJson.plugins = {};
|
|
102
|
-
delete appJson.entranceDeclare;
|
|
103
|
-
return appJson;
|
|
104
|
-
};
|
|
105
|
-
/**
|
|
106
|
-
* 更新app.json中的subpackages
|
|
107
|
-
* @param {Object} appJson
|
|
108
|
-
* @param {Object} modulesConfig
|
|
109
|
-
*/
|
|
110
|
-
const updateSubpackages = (appJson, modulesConfig) => {
|
|
111
|
-
// eslint-disable-next-line
|
|
112
|
-
for (const name in modulesConfig) {
|
|
113
|
-
const moduleInfo = isObject(modulesConfig[name]) ? [modulesConfig[name]] : modulesConfig[name];
|
|
114
|
-
// eslint-disable-next-line
|
|
115
|
-
appJson.subpackages = appJson.subpackages.concat(moduleInfo);
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* 动态生成编译后的app.json
|
|
122
|
-
* @param {object} tmsConfig
|
|
123
|
-
* @param {array} modules
|
|
124
|
-
* @returns
|
|
125
|
-
*/
|
|
126
|
-
function buildOutputAppJson(tmsConfig, modules) {
|
|
127
|
-
// 获取当前 modules 下的所有子模块的配置内容
|
|
128
|
-
const modulesConfig = getLocalModuleConfig(modules, tmsConfig.appName, DEFAULT_MODULE_DIR, MODULE_CONFIG_FILENAME);
|
|
129
|
-
// 获取app.json的配置
|
|
130
|
-
const appJson = getAppJsonContent(resolve('./app.json'));
|
|
131
|
-
// 更新app.json中的subpackages
|
|
132
|
-
updateSubpackages(appJson, modulesConfig);
|
|
133
|
-
// 更新主包,需在subpackages处理完成后执行, pages/
|
|
134
|
-
updateMainPackages(appJson, tmsConfig.mainPackages, DEFAULT_MODULE_DIR);
|
|
135
|
-
|
|
136
|
-
fs.writeFileSync(resolve(`${tmsConfig.webpack.outputDir}/app.json`), JSON.stringify(appJson, null, 2), 'utf8');
|
|
137
|
-
|
|
138
|
-
return appJson;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
module.exports = {
|
|
142
|
-
setModuleConfig,
|
|
143
|
-
buildOutputAppJson,
|
|
144
|
-
};
|
package/src/utils/cliUtils.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const cp = require('child_process');
|
|
3
|
-
|
|
4
|
-
const getDevtoolCliPath = async () => {
|
|
5
|
-
let cliPath;
|
|
6
|
-
if (process.platform === 'darwin') {
|
|
7
|
-
cliPath = '/Applications/wechatwebdevtools.app/Contents/MacOS/cli';
|
|
8
|
-
} else if (process.platform === 'win32') {
|
|
9
|
-
cliPath = 'C:/Program Files (x86)/Tencent/微信web开发者工具/cli.bat';
|
|
10
|
-
} else {
|
|
11
|
-
// 其余平台暂不支持
|
|
12
|
-
throw new Error('unsupported platform');
|
|
13
|
-
}
|
|
14
|
-
const exists = await new Promise(resolve => fs.exists(cliPath, resolve));
|
|
15
|
-
if (!exists) {
|
|
16
|
-
// 找不到开发工具
|
|
17
|
-
throw new Error(`未找到微信小程序开发者工具,请确认是否安装,如未安装请前往
|
|
18
|
-
https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 下载`);
|
|
19
|
-
}
|
|
20
|
-
return cliPath;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const openDevtool = async (path) => {
|
|
24
|
-
const cliPath = await getDevtoolCliPath();
|
|
25
|
-
const child = cp.spawn(cliPath, ['open', '--project', path], {
|
|
26
|
-
detached: true,
|
|
27
|
-
stdio: 'inherit',
|
|
28
|
-
});
|
|
29
|
-
child.unref();
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
module.exports = {
|
|
33
|
-
getDevtoolCliPath,
|
|
34
|
-
openDevtool,
|
|
35
|
-
};
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
const MetalSmith = require('metalsmith');
|
|
2
|
-
const { getGlobalInstance } = require('./global.js');
|
|
3
|
-
const { downloadRepoForGit, resolve } = require('./widgets');
|
|
4
|
-
const { fail } = require('./log');
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const shelljs = require('shelljs');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 对克隆下来的模块进行相应的文件处理操作,比如收集处理模块信息,进行信息缓存等操作
|
|
10
|
-
* @param { string } sourceDir 缓存文件夹
|
|
11
|
-
* @param { string } targetDir 目标文件夹
|
|
12
|
-
* @param { arrary } ignore
|
|
13
|
-
* @returns { undefined } no return
|
|
14
|
-
*/
|
|
15
|
-
function moveFile(sourceDir, targetDir, ignore = []) {
|
|
16
|
-
// 删除不是文件夹的文件
|
|
17
|
-
return new Promise((resolve) => {
|
|
18
|
-
MetalSmith(__dirname)
|
|
19
|
-
.ignore(ignore)
|
|
20
|
-
.source(sourceDir)
|
|
21
|
-
.destination(targetDir)
|
|
22
|
-
.build((e) => {
|
|
23
|
-
if (e) {
|
|
24
|
-
fail(e); // eslint-disable-line
|
|
25
|
-
console.log('MetalSmith 详细的错误信息:', e);
|
|
26
|
-
}
|
|
27
|
-
resolve();
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 下载目标模块
|
|
34
|
-
* @param { string } sourceDir 缓存文件夹
|
|
35
|
-
* @param { string } targetDir 目标文件夹
|
|
36
|
-
* @returns { array } modules 描述模块的列表
|
|
37
|
-
*/
|
|
38
|
-
async function cloneModules(sourceDir, targetDir, modules) {
|
|
39
|
-
// 根据小程序的配置文件下载模块, 并且处理信息
|
|
40
|
-
for (const moduleInfo of modules) { // eslint-disable-line
|
|
41
|
-
if (moduleInfo.repoInfo) {
|
|
42
|
-
await downLoadAndMoveModule(sourceDir, targetDir, moduleInfo);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 下载模块信息并且将它移动到对应的位置
|
|
49
|
-
* @param { string } sourceDir 代码缓存文件夹
|
|
50
|
-
* @param { string } targetDir 代码要放到的目标文件夹
|
|
51
|
-
* @returns { array } moduleInfo 描述模块的信息
|
|
52
|
-
*/
|
|
53
|
-
async function downLoadAndMoveModule(sourceDir, targetDir, moduleInfo) {
|
|
54
|
-
const { repoInfo: { buildGitTag, httpRepoUrl }, path } = moduleInfo;
|
|
55
|
-
|
|
56
|
-
// 源码临时存在的源目录
|
|
57
|
-
let sourcePath = resolve(sourceDir, path);
|
|
58
|
-
// 源码要放到目标目录
|
|
59
|
-
const targetPath = resolve(targetDir, path);
|
|
60
|
-
// 设置模块的构建分支
|
|
61
|
-
const cloneBranch = buildGitTag && typeof buildGitTag === 'string' ? buildGitTag : 'master';
|
|
62
|
-
|
|
63
|
-
// 检查缓存中有没有
|
|
64
|
-
const globalInstance = getGlobalInstance();
|
|
65
|
-
const moduleInCache = globalInstance.getModuleCache(httpRepoUrl, cloneBranch);
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
if (!moduleInCache) {
|
|
69
|
-
await downloadRepoForGit(httpRepoUrl, sourcePath, cloneBranch);
|
|
70
|
-
globalInstance.setModuleCache(httpRepoUrl, cloneBranch, sourcePath);
|
|
71
|
-
} else {
|
|
72
|
-
sourcePath = globalInstance.getModuleCache(httpRepoUrl, cloneBranch).dest;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (fs.existsSync(targetPath)) {
|
|
76
|
-
shelljs.rm('-rf', targetPath);
|
|
77
|
-
}
|
|
78
|
-
await moveFile(sourcePath, targetPath, [
|
|
79
|
-
'node_modules',
|
|
80
|
-
'.git',
|
|
81
|
-
]);
|
|
82
|
-
} catch (e) {
|
|
83
|
-
fail(`downLoadAndMoveModule ${e}`); // eslint-disable-line
|
|
84
|
-
process.exit(-1);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
module.exports = {
|
|
89
|
-
cloneModules,
|
|
90
|
-
};
|
package/src/utils/npmUtils.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 本文件主要负责项目或者分包依赖的npm的安装
|
|
3
|
-
*/
|
|
4
|
-
const fs = require('fs');
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const shell = require('shelljs');
|
|
7
|
-
const LOG = require('./log');
|
|
8
|
-
|
|
9
|
-
const dirpath = process.cwd(); // 项目根目录
|
|
10
|
-
|
|
11
|
-
const getTarNpmFilename = targetDir => `${targetDir.replaceAll('/', '-')}.tar.gz`;
|
|
12
|
-
|
|
13
|
-
// 缓存npm包
|
|
14
|
-
const npmCache = function (targetDir, cacheDir) {
|
|
15
|
-
if (!fs.existsSync(cacheDir)) {
|
|
16
|
-
fs.mkdirSync(cacheDir);
|
|
17
|
-
}
|
|
18
|
-
const tarNpmFilename = getTarNpmFilename(targetDir);
|
|
19
|
-
const tarNpmFilePath = `${cacheDir}/${tarNpmFilename}`;
|
|
20
|
-
if (fs.existsSync(tarNpmFilePath)) {
|
|
21
|
-
shell.rm('-rf', tarNpmFilePath);
|
|
22
|
-
}
|
|
23
|
-
const cmd = `tar -zcvf ${tarNpmFilePath} ./node_modules`;
|
|
24
|
-
shell.exec(cmd, { async: true, silent: true });
|
|
25
|
-
// tar -zcvf /Users/odile/.tmskit/node_modules.tar.gz ./node_modules
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
// 获取缓存npm包
|
|
29
|
-
const getNpmCache = function (targetDir, cacheDir) {
|
|
30
|
-
const tarNpmFilename = getTarNpmFilename(targetDir);
|
|
31
|
-
const tarNpmFilePath = `${cacheDir}/${tarNpmFilename}`;
|
|
32
|
-
if (fs.existsSync(tarNpmFilePath)) {
|
|
33
|
-
const cmd = `tar -zxvf ${tarNpmFilePath} -C ./`;
|
|
34
|
-
shell.exec(cmd, { async: false, silent: true });
|
|
35
|
-
}
|
|
36
|
-
// tar -zxvf /Users/odile/.tmskit/node_modules.tar.gz -C ./
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
// 遍历安装指定目录下所有项目的npm依赖
|
|
41
|
-
const npmInstallAll = async (modules, contextDir, cacheDir) => {
|
|
42
|
-
const packageJsonFiles = await findAllPackageJson(modules, contextDir);
|
|
43
|
-
|
|
44
|
-
await Promise.all(packageJsonFiles.map(file => new Promise((resolve) => {
|
|
45
|
-
const dir = path.dirname(file);
|
|
46
|
-
shell.cd(dir);
|
|
47
|
-
if (!fs.existsSync(`${dir}/node_modules`)) {
|
|
48
|
-
getNpmCache(dir, cacheDir);
|
|
49
|
-
}
|
|
50
|
-
shell.exec('npx pnpm install --prod --registry http://mirrors.tencent.com/npm/', { silent: false });
|
|
51
|
-
resolve();
|
|
52
|
-
npmCache(dir, cacheDir);
|
|
53
|
-
})));
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 递归查找指定条件的文件
|
|
58
|
-
* @param {String} startPath 开始查找的根路径
|
|
59
|
-
* @param {String} filter 匹配的字符串
|
|
60
|
-
* @returns {Array<String>} 查找到的文件路径列表
|
|
61
|
-
*/
|
|
62
|
-
const findFilesByFilter = (startPath, filter) => {
|
|
63
|
-
const result = [];
|
|
64
|
-
/**
|
|
65
|
-
* 根据指定的筛选器查找文件
|
|
66
|
-
* @param {String} startPath 开始查找的文件夹路径
|
|
67
|
-
* @param {String} filter 筛选器
|
|
68
|
-
* @returns {Undefined} 无需返回值
|
|
69
|
-
*/
|
|
70
|
-
const find = (startPath, filter) => {
|
|
71
|
-
// 目录不存在
|
|
72
|
-
if (!fs.existsSync(startPath)) {
|
|
73
|
-
LOG.fail(`${startPath}目录不存在`);
|
|
74
|
-
process.exit(-1);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// 当前目录下的所有文件 / 文件夹
|
|
79
|
-
const exceptDir = ['node_modules', 'miniprogram_npm'];
|
|
80
|
-
if (exceptDir.find(item => startPath.indexOf(item) > -1)) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
const files = fs.readdirSync(startPath);
|
|
84
|
-
files.forEach((file) => {
|
|
85
|
-
const filename = path.join(startPath, file);
|
|
86
|
-
const stat = fs.lstatSync(filename);
|
|
87
|
-
// 当前文件是文件夹类型,继续递归
|
|
88
|
-
if (stat.isDirectory()) {
|
|
89
|
-
find(filename, filter);
|
|
90
|
-
} else if (filename.indexOf(filter) >= 0) {
|
|
91
|
-
// 文件类型
|
|
92
|
-
result.push(filename);
|
|
93
|
-
};
|
|
94
|
-
});
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
find(startPath, filter);
|
|
98
|
-
return result;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* 找到项目中所有的package.json文件
|
|
103
|
-
* @param {Array<String>} subRoots 需要安装npm依赖的路径
|
|
104
|
-
* @param {String} contextDir 命令运行的目录
|
|
105
|
-
* @returns {Array<String>} 找到的所有package.json文件的路径
|
|
106
|
-
*/
|
|
107
|
-
const findAllPackageJson = (subRoots = [], contextDir) => {
|
|
108
|
-
const packageJsonName = 'package.json'; // 查找文件名
|
|
109
|
-
const cwd = contextDir || dirpath;
|
|
110
|
-
const result = [path.join(cwd, packageJsonName)]; // 默认填充根目录下的package.json
|
|
111
|
-
|
|
112
|
-
subRoots.forEach((subRoot) => {
|
|
113
|
-
const toppath = path.join(cwd, subRoot.root); // 从该目录开始查找package.json文件
|
|
114
|
-
const list = findFilesByFilter(toppath, packageJsonName);
|
|
115
|
-
|
|
116
|
-
result.push(...list);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
return result;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
module.exports = {
|
|
124
|
-
npmInstallAll,
|
|
125
|
-
findAllPackageJson,
|
|
126
|
-
};
|
package/src/utils/tkitUtils.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
const loadash = require('lodash');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const { TMS_NAME, TMS_CONFIG_FILENAME, MODULE_CONFIG_FILENAME } = require('../config/constant');
|
|
4
|
-
const { resolve, isObject } = require('./widgets');
|
|
5
|
-
const { setModuleConfig } = require('./buildAppJson');
|
|
6
|
-
const defaultTmsConfig = require('../config/defaultTmsConfig');
|
|
7
|
-
const { fail } = require('./log');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 读取tms.config.json
|
|
11
|
-
* @param env {string} 环境变量
|
|
12
|
-
*/
|
|
13
|
-
const readTmsConfig = function (env) {
|
|
14
|
-
const tmsConfigPath = resolve(TMS_CONFIG_FILENAME);
|
|
15
|
-
if (!fs.existsSync(tmsConfigPath)) {
|
|
16
|
-
fail('当前执行目录没有tms.config.js的配置项,请进行配置');
|
|
17
|
-
process.exit(1);
|
|
18
|
-
}
|
|
19
|
-
const tmsConfigFn = require(tmsConfigPath);
|
|
20
|
-
const tmsConfig = tmsConfigFn({
|
|
21
|
-
env,
|
|
22
|
-
});
|
|
23
|
-
// 合并默认值
|
|
24
|
-
loadash.mergeWith(tmsConfig, defaultTmsConfig);
|
|
25
|
-
return tmsConfig;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 从tms.config.json中检索用户传入的有效modules
|
|
30
|
-
* @param { object } tmsConfig
|
|
31
|
-
* @param { array } modules
|
|
32
|
-
* @returns
|
|
33
|
-
*/
|
|
34
|
-
const checkModules = function (tmsConfig, modules) {
|
|
35
|
-
const targetModules = [];
|
|
36
|
-
modules.forEach((moduleName) => {
|
|
37
|
-
const module = tmsConfig.modules.find(module => module.name === moduleName);
|
|
38
|
-
module && targetModules.push(module);
|
|
39
|
-
});
|
|
40
|
-
if (targetModules.length === 0) {
|
|
41
|
-
fail(`你启动的模块无效,尝试 ${TMS_NAME} -m moduleName`);
|
|
42
|
-
process.exit(1);
|
|
43
|
-
}
|
|
44
|
-
return targetModules;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* tms.config.js的modules 合并 module.config.json的配置项
|
|
49
|
-
* @param {array} modules
|
|
50
|
-
* @param {string} appName
|
|
51
|
-
* @param {string} moduleDir
|
|
52
|
-
* @returns
|
|
53
|
-
*/
|
|
54
|
-
const tmsModulesMergeLocalModuleCfg = (modules, appName, moduleDir) => {
|
|
55
|
-
const newModules = [];
|
|
56
|
-
modules.forEach(({ path: relativePath, name: moduleName }, moduleIndex) => {
|
|
57
|
-
const moduleConfigPath = resolve(relativePath, MODULE_CONFIG_FILENAME);
|
|
58
|
-
if (fs.existsSync(moduleConfigPath)) {
|
|
59
|
-
let moduleConfigContent = fs.readFileSync(moduleConfigPath, 'utf-8');
|
|
60
|
-
moduleConfigContent = setModuleConfig(moduleConfigContent, appName, moduleDir);
|
|
61
|
-
const moduleContentArr = isObject(moduleConfigContent) ? [moduleConfigContent] : moduleConfigContent;
|
|
62
|
-
moduleContentArr.forEach(({ name }, moduleContentArrIndex) => {
|
|
63
|
-
if (name === moduleName) {
|
|
64
|
-
newModules.push({
|
|
65
|
-
...modules[moduleIndex],
|
|
66
|
-
...moduleContentArr[moduleContentArrIndex],
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
} else {
|
|
71
|
-
newModules.push({
|
|
72
|
-
...modules[moduleIndex],
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
return newModules;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
module.exports = {
|
|
81
|
-
readTmsConfig,
|
|
82
|
-
checkModules,
|
|
83
|
-
tmsModulesMergeLocalModuleCfg,
|
|
84
|
-
};
|