@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.
@@ -1,5 +1,4 @@
1
1
  /* eslint-disable no-param-reassign */
2
- const loadash = require('lodash');
3
2
  const init = require('./init/index');
4
3
  const dev = require('./dev/index');
5
4
  const build = require('./build/index');
@@ -8,11 +7,10 @@ const cloud = require('./cloud/index');
8
7
  const { fail, info } = require('../../utils/log');
9
8
  const { global } = require('../../utils/global');
10
9
  const {
11
- readTmsConfig,
12
- readTmsPrivateCf,
13
- checkModules,
10
+ getTmsConfig,
14
11
  getModulesByMergeDepModules,
15
12
  getSubPackages,
13
+ getModulesByModuleNames,
16
14
  } = require('../../core/tmsMpconfig');
17
15
 
18
16
  const handleModuleArg = (cmd) => {
@@ -28,7 +26,7 @@ const handleModuleArg = (cmd) => {
28
26
  * @param {Object} modulePrivateCfg 私有配置里的模块
29
27
  * @param {Array} moduleAll 当前小程序全部模块
30
28
  */
31
- const getSpecificModules = (moduleArg, modules) => {
29
+ const getSpecificModuleNames = (moduleArg, modules) => {
32
30
  if (moduleArg.length > 0) {
33
31
  return moduleArg;
34
32
  }
@@ -46,86 +44,22 @@ const getSpecificModules = (moduleArg, modules) => {
46
44
  return all.map(item => item.moduleName);
47
45
  };
48
46
 
49
- /**
50
- * 合并tms.config.js 与 tms.private.config.js的配置项
51
- * @param {*} tmsConfig
52
- * @param {*} tmsPrivateCf
53
- * @returns
54
- */
55
- const mergeConfig = (tmsConfig, tmsPrivateCf) => {
56
- const modules = {};
57
- if (Array.isArray(tmsConfig.modules)) {
58
- modules.all = tmsConfig.modules;
59
- tmsConfig.modules = modules;
60
- }
61
- // 合并默认值
62
- const res = loadash.mergeWith(tmsConfig, tmsPrivateCf, (objValue, srcValue) => {
63
- if (loadash.isArray(objValue) && objValue[0] && loadash.isObject(objValue[0])) {
64
- return objValue.concat(srcValue);
65
- }
66
- });
67
-
68
- return res;
69
- };
70
-
71
47
  async function run(commandName, cmd) {
72
48
  try {
73
- // 用户本地的私有项目配置
74
- const tmsPrivateCf = readTmsPrivateCf();
75
- const { env = tmsPrivateCf?.env } = cmd;
76
- let tmsConfig = readTmsConfig(env);
77
- tmsConfig = mergeConfig(tmsConfig, tmsPrivateCf);
78
-
79
- // 处理module参数
80
- const specificModules = getSpecificModules(
81
- handleModuleArg(cmd),
82
- tmsConfig.modules,
83
- );
84
-
85
- const modules = checkModules(
86
- tmsConfig,
87
- [
88
- ...new Set([
89
- ...tmsConfig.mainPackages,
90
- ...specificModules,
91
- ]),
92
- ],
93
- true,
94
- );
95
-
96
- // 获取所有模块,合并模块依赖的模块
97
- const newModules = getModulesByMergeDepModules(tmsConfig, modules);
98
- // 获取所有的分包
99
- const subPackages = getSubPackages(newModules);
49
+ // 用户本地的配置
50
+ const tmsConfig = getTmsConfig();
100
51
 
101
52
  // 缓存数据
102
53
  global.setData({
103
- env,
104
54
  cmd,
105
55
  tmsConfig,
106
56
  });
107
57
 
108
- switch (commandName) {
109
- case 'init':
110
- init(tmsConfig, newModules);
111
- return;
112
- case 'dev':
113
- global.setData('isDev', true);
114
- dev(tmsConfig, newModules);
115
- return;
116
- case 'cloud':
117
- cloud(tmsConfig);
118
- return;
119
- case 'install':
120
- install(tmsConfig, subPackages, false);
121
- return;
122
- case 'build':
123
- global.setData('isDev', false);
124
- build(tmsConfig, newModules);
125
- return;
126
- default:
127
- return;
58
+ if (commandName === 'cloud') {
59
+ cloud(tmsConfig);
60
+ return;
128
61
  }
62
+ otherCommands(tmsConfig, commandName, cmd);
129
63
  } catch (error) {
130
64
  const errMsg = typeof error === 'object' ? error.message : error;
131
65
  fail(`构建出现错误: ${errMsg}`);
@@ -134,4 +68,40 @@ async function run(commandName, cmd) {
134
68
  }
135
69
  }
136
70
 
71
+ function otherCommands(tmsConfig, commandName, cmd) {
72
+ // 处理module参数
73
+ const specificModuleNames = getSpecificModuleNames(
74
+ handleModuleArg(cmd),
75
+ tmsConfig.modules,
76
+ );
77
+
78
+ // moduleNames => ['home', 'car']
79
+ const moduleNames = [...new Set([...specificModuleNames])];
80
+ const modules = getModulesByModuleNames(tmsConfig, moduleNames, false);
81
+
82
+ // 获取所有模块,合并模块依赖的模块
83
+ const newModules = getModulesByMergeDepModules(tmsConfig, modules, false);
84
+ // 获取所有的分包
85
+ const subPackages = getSubPackages(newModules);
86
+
87
+ switch (commandName) {
88
+ case 'init':
89
+ init(tmsConfig, newModules);
90
+ return;
91
+ case 'dev':
92
+ global.setData('isDev', true);
93
+ dev(tmsConfig, newModules);
94
+ return;
95
+ case 'install':
96
+ install(tmsConfig, subPackages, false);
97
+ return;
98
+ case 'build':
99
+ global.setData('isDev', false);
100
+ build(tmsConfig, newModules);
101
+ return;
102
+ default:
103
+ return;
104
+ }
105
+ }
106
+
137
107
  module.exports = run;
@@ -26,25 +26,6 @@ const cpFilesToOutput = function (tmsConfig, defaultFiles) {
26
26
  });
27
27
  };
28
28
 
29
- /**
30
- * 校验相关配置项
31
- * @param {*} targetModules
32
- * @returns
33
- */
34
- function checkConfig(targetModules) {
35
- for (const item of targetModules) {
36
- if (!item.root) {
37
- throw new Error(`检查${item.name} module.config.json的root字段`);
38
- }
39
-
40
- // 判断源码目录是否有该模块
41
- if (item.path && !fs.existsSync(resolve(item.path))) {
42
- throw new Error(`${item.path}模块代码路径不存在, 请检查tms.config.js的${item.name}模块的path`);
43
- }
44
- }
45
- return true;
46
- }
47
-
48
29
  async function task(tmsConfig, targetModules) {
49
30
  // 下载和移动代码
50
31
  await createTask(
@@ -58,8 +39,6 @@ async function task(tmsConfig, targetModules) {
58
39
  // 获取所有的分包
59
40
  const newSubPackages = getSubPackages(newModules);
60
41
 
61
- checkConfig(newSubPackages);
62
-
63
42
  // 拷贝相关配置文件到输出目录
64
43
  await createTask(
65
44
  cpFilesToOutput,
@@ -68,8 +47,7 @@ async function task(tmsConfig, targetModules) {
68
47
  )(tmsConfig, DEFAULT_COPY_CONFIG);
69
48
 
70
49
  // install
71
- await install(tmsConfig, newSubPackages);
72
-
50
+ await install(tmsConfig, newSubPackages, true);
73
51
 
74
52
  // 动态生成编译后的app.json;
75
53
  await createTask(
@@ -90,7 +68,7 @@ async function init(tmsConfig, targetModules) {
90
68
  return taskRes;
91
69
  } catch (error) {
92
70
  const errMsg = typeof error === 'object' ? error.message : error;
93
- fail(`初始化流程出现错误${errMsg}`);
71
+ fail(`初始化流程出现错误: ${errMsg}`);
94
72
  info('详细的错误信息', error);
95
73
  process.exit(1);
96
74
  }
@@ -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
7
  const { setCache, getCache } = require('../../../core/cache');
8
8
  const { CACHE_DIR } = require('../../../config/constant');
9
9
  const { npmInstallAll } = require('../../../core/npm');
10
- const { fail, info } = require('../../../utils/log');
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, modules, useCache = true) {
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, modules, useCache);
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, useCache = true) {
26
26
  mpCiInstall,
27
27
  '开始构建miniprogram_npm',
28
28
  '构建miniprogram_npm 完成',
29
- )(tmsConfig, modules, false);
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, modules, useCache);
36
+ )(tmsConfig, subPackages, useCache);
37
37
  }
38
38
  shelljs.cd(cwd);
39
39
  }
40
40
 
41
- async function npmInstall(tmsConfig, modules, useCache) {
41
+ async function npmInstall(tmsConfig, subPackages, useCache) {
42
42
  // 如果依赖没有更新和使用缓存数据(则命中缓存)
43
- if (!isDependenciesUpdate(modules, resolve('./'), tmsConfig.outputDir) && useCache) {
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
- modules.forEach((item) => {
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 = resolve(item.path, 'package.json');
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(modules, resolve(tmsConfig.outputDir), `${CACHE_DIR}/node_modules`);
55
+ await npmInstallAll(subPackages, resolve(tmsConfig.outputDir), `${CACHE_DIR}/node_modules`);
60
56
  return { isCache: false };
61
57
  }
62
58
 
63
59
  // 构建miniprogram_npm
64
- async function mpCiInstall(tmsConfig, modules, useCache) {
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
- modules.forEach((item) => {
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);
@@ -209,6 +209,14 @@ function findFiles(globPath, filter = []) {
209
209
  });
210
210
  }
211
211
 
212
+ // 获取绝对路径
213
+ function getAbsolutePath(pathDir, cwd = '') {
214
+ let newPath = pathDir;
215
+ newPath = newPath.startsWith('/') ? newPath : resolve(cwd, newPath);
216
+ newPath = newPath.endsWith('/') ? newPath.slice(0, newPath.length - 1) : newPath;
217
+ return newPath;
218
+ }
219
+
212
220
  module.exports = {
213
221
  resolve,
214
222
  isObject,
@@ -223,4 +231,5 @@ module.exports = {
223
231
  relativeCwdPath,
224
232
  filterField,
225
233
  findFiles,
234
+ getAbsolutePath,
226
235
  };