@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/src/entry.js CHANGED
@@ -1,14 +1,25 @@
1
1
  module.exports = [
2
2
  {
3
3
  command: 'create <project-name>',
4
- description: '创建新的应用',
4
+ description: '创建项目',
5
5
  action: (projectName) => {
6
6
  require('./scripts/create')(projectName);
7
7
  },
8
8
  },
9
+ {
10
+ command: 'install-cmd <npm-name>',
11
+ description: '安装扩展命令',
12
+ options: [
13
+ ['--registry [registry]', 'npm源'],
14
+ ],
15
+ action: (npmName, cmd) => {
16
+ const res = require('./scripts/extend-cmd');
17
+ res.installCmd(npmName, cmd);
18
+ },
19
+ },
9
20
  {
10
21
  name: 'run',
11
- type: 'child',
22
+ type: 'parent',
12
23
  description: '项目开发使用的命令',
13
24
  commands: [
14
25
  {
@@ -56,18 +67,6 @@ module.exports = [
56
67
  require('./scripts/run/index')('build', cmd);
57
68
  },
58
69
  },
59
- // 对外暂不暴露该命令
60
- // {
61
- // command: 'init',
62
- // description: '模块配置初始化项目(eg: 下载第三方模块代码、安装依赖、生成app.json等)',
63
- // options: [
64
- // ['-m, --module [moduleName]', '模块名称'],
65
- // ['-e, --env [env]', '环境变量'],
66
- // ],
67
- // action: (cmd) => {
68
- // require('./scripts/run/index')('init', cmd);
69
- // },
70
- // },
71
70
  ],
72
71
  },
73
72
  ];
package/src/index.js CHANGED
@@ -1,57 +1,98 @@
1
+ /* eslint-disable no-param-reassign */
1
2
  const chalk = require('chalk');
2
3
  const commander = require('commander');
3
- const { suggestCommands } = require('./utils/widgets');
4
- const { info } = require('./utils/log');
4
+ const path = require('path');
5
+ const fs = require('fs');
6
+ const { resolve } = require('./utils/widgets');
7
+ const { infoNoTime } = require('./utils/log');
8
+ const { getTmsConfig } = require('../src/core/tmsMpconfig');
5
9
  const { TMS_NAME } = require('./config/constant.js');
6
10
  const commands = require('./entry');
7
- const init = require('./init');
8
-
9
- init();
11
+ const check = require('./check');
12
+ const { loadExtendCmd } = require('./scripts/extend-cmd/index.js');
13
+ const report = require('./core/report');
10
14
 
15
+ check();
11
16
  const program = new commander.Command(TMS_NAME);
12
-
13
17
  program
14
18
  .version(`${TMS_NAME} ${require('../package.json').version}`, '-v, -V, --version');
15
19
 
20
+ // 注册命令底层实现
16
21
  function registerCommand(program, commands) {
17
22
  commands.forEach((cmd) => {
18
- if (cmd.type === 'child') {
23
+ if (cmd.type === 'parent') {
19
24
  const childProgram = new commander.Command(cmd.name);
20
25
  cmd.usage && childProgram.usage(cmd.usage);
21
26
  cmd.description && childProgram.description(cmd.description);
22
27
  registerCommand(childProgram, cmd.commands);
23
-
24
28
  program.addCommand(childProgram);
25
29
  } else {
26
30
  const command = program.command(cmd.command);
27
-
28
31
  cmd.usage && command.usage(cmd.usage);
29
-
30
32
  cmd.description && command.description(cmd.description);
31
-
32
33
  cmd.options?.forEach(opt => command.option(...opt));
33
-
34
+ // 上报
35
+ command.hook('preAction', (thisCommand) => {
36
+ report(`${thisCommand._name}-pre`);
37
+ });
38
+ // 上报
39
+ command.hook('postAction', (thisCommand) => {
40
+ report(`${thisCommand._name}-post`);
41
+ });
34
42
  command.action(cmd.action);
35
43
  }
36
44
  });
37
45
  }
38
46
 
39
- registerCommand(program, commands);
47
+ // 注册扩展命令
48
+ function registerExtendCommand(program, configPath) {
49
+ const tmsConfig = getTmsConfig(configPath);
50
+ if (tmsConfig?.commands) {
51
+ const commands = typeof tmsConfig.commands === 'function' ? tmsConfig.commands() : tmsConfig.commands;
52
+ if (Array.isArray(commands)) {
53
+ registerCommand(program, commands);
54
+ }
55
+ }
56
+ }
57
+ // 注册所有的命令
58
+ function registerAllCmds(program, commands) {
59
+ // 注册脚手架内部命令
60
+ registerCommand(program, commands);
40
61
 
41
- program.on('--help', () => {
42
- info(` Run ${chalk.cyan(`${TMS_NAME} <command> --help`)} for detailed usage of given command.`);
43
- });
62
+ // 注册npm包扩展命令
63
+ const cmdConfigs = loadExtendCmd();
64
+ cmdConfigs.forEach((cmdConfig) => {
65
+ registerExtendCommand(program, path.dirname(cmdConfig));
66
+ });
67
+
68
+ // 注册当前目录扩展命令
69
+ const tmsConfigPath = resolve('./tms.config.js');
70
+ if (fs.existsSync(tmsConfigPath)) {
71
+ registerExtendCommand(program, path.dirname(tmsConfigPath));
72
+ }
73
+ }
74
+ registerAllCmds(program, commands);
44
75
 
45
76
  // 捕获未注册的命令
46
77
  program
47
78
  .arguments('<command>')
79
+ .option('-c, --config <value>', '配置', (value) => {
80
+ // 注册指定配置的扩展命令
81
+ registerExtendCommand(program, path.dirname(resolve(value)));
82
+ })
48
83
  .action((cmd) => {
49
- program.outputHelp();
50
- info(` ${chalk.red(`Unknown command ${chalk.yellow(cmd)}.`)}`);
51
- suggestCommands(cmd);
52
- process.exitCode = 1;
84
+ infoNoTime(chalk.yellow(`
85
+ 没有找到${cmd}命令
86
+ 你可以通过${chalk.green('tmskit install-cmd <npm-name>')}安装扩展命令, ${chalk.green('扩展命令列表:https://www.npmjs.com/search?q=tmskit-cmd')}
87
+ 你也可以通过 ${chalk.green('tmskit <command> --config=../tms.config.js')} 指定本地的扩展命令
88
+ `));
89
+ report(`${cmd}-not-find`);
53
90
  });
54
91
 
92
+ program.on('--help', () => {
93
+ infoNoTime(`Run ${chalk.cyan(`${TMS_NAME} <command> --help`)} for detailed usage of given command.`);
94
+ });
95
+
55
96
  if (!process.argv.slice(2).length) {
56
97
  program.outputHelp();
57
98
  process.exit(-1);
@@ -15,6 +15,7 @@ const { fail, succeed, info } = require('../../utils/log');
15
15
  const generator = require('./generator');
16
16
  const request = require('request');
17
17
  const unzip = require('unzipper');
18
+ const report = require('../../core/report');
18
19
 
19
20
  /**
20
21
  * 如果该目录下面存在文件,换个名字
@@ -71,6 +72,7 @@ async function create(projectName) {
71
72
  const cwd = process.cwd();
72
73
  const targetDir = path.resolve(cwd, projectName);
73
74
  const { projectType } = await inquirer.prompt(CREATE_TEMPLATE_QUESTION);
75
+ report('create-start', { projectType });
74
76
 
75
77
  // 创建项目目录
76
78
  await createProjectDir(targetDir);
@@ -101,7 +103,7 @@ async function create(projectName) {
101
103
  }
102
104
  shelljs.rm('-rf', resolve(projectName, TEMPLATE_TKIT_DIR));
103
105
  }
104
-
106
+ report('create-success', { projectType });
105
107
  succeed('项目创建完成.');
106
108
  })
107
109
  .catch((err) => {
@@ -0,0 +1,74 @@
1
+
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const shellJs = require('shelljs');
5
+ const { ensureDirExist } = require('../../utils/io');
6
+ const { EXTEND_CMD } = require('../../config/constant');
7
+ const { createTask } = require('../../utils/widgets');
8
+ const { info, fail } = require('../../utils/log');
9
+
10
+ /**
11
+ * 下载扩展命令的npm包
12
+ * @param {string} npmName npm包名
13
+ * @param {object} cmd {registry: 'http://mirrors.tencent.com/npm/'} 用户输入执行install-cmd的参数
14
+ */
15
+ async function installCmd(npmName, cmd) {
16
+ try {
17
+ const cmdPackageJson = `${EXTEND_CMD}/package.json`;
18
+ if (!fs.existsSync(cmdPackageJson)) {
19
+ ensureDirExist(EXTEND_CMD);
20
+ fs.writeFileSync(cmdPackageJson, JSON.stringify({ dependencies: {} }, null, 2));
21
+ }
22
+ shellJs.cd(EXTEND_CMD);
23
+ await createTask(npmName => new Promise((resolve, reject) => {
24
+ const registry = cmd.registry ? `--registry=${cmd.registry}` : '';
25
+ shellJs.exec(
26
+ `npm install --save ${npmName} ${registry}`,
27
+ { cwd: EXTEND_CMD, silent: true },
28
+ (code, stdout, stderr) => {
29
+ if (code !== 0) {
30
+ reject(stderr);
31
+ }
32
+ resolve();
33
+ },
34
+ );
35
+ }), `开始下载${npmName}`, `下载${npmName}完成`)(npmName);
36
+ } catch (e) {
37
+ fail('构建出现错误:');
38
+ info(e);
39
+ process.exit(1);
40
+ }
41
+ }
42
+
43
+
44
+ /**
45
+ * 加载扩展命令的npm包
46
+ * @returns
47
+ */
48
+ function loadExtendCmd() {
49
+ const cmdPackageJson = `${EXTEND_CMD}/package.json`;
50
+ const cmdNpmDir = `${EXTEND_CMD}/node_modules`;
51
+ if (fs.existsSync(cmdPackageJson)) {
52
+ const content = fs.readFileSync(cmdPackageJson, 'utf8');
53
+ const json = JSON.parse(content);
54
+ const deps = json.dependencies || {};
55
+
56
+ const cmdConfigs = [];
57
+ Object.keys(deps).forEach((name) => {
58
+ // 检索cmd的npm包
59
+ if (!/^tmskit-cmd-|^@[^/]+\/tmskit-cmd-/.test(name)) return false;
60
+ const cmdConfig = path.join(cmdNpmDir, name, 'tms.config.js');
61
+ if (fs.existsSync(cmdConfig)) {
62
+ cmdConfigs.push(cmdConfig);
63
+ }
64
+ });
65
+ return cmdConfigs;
66
+ }
67
+ return [];
68
+ }
69
+
70
+
71
+ module.exports = {
72
+ installCmd,
73
+ loadExtendCmd,
74
+ };
@@ -2,12 +2,13 @@ const shelljs = require('shelljs');
2
2
  const { resolve, filterField } = require('../../../utils/widgets');
3
3
  const init = require('../init/index');
4
4
  const compileBuild = require('../../../compile/build');
5
+ const report = require('../../../core/report');
5
6
 
6
7
  async function build(tmsConfig, targetModules) {
7
8
  // 开始构建前,清理输出目录
8
9
  await shelljs.rm('-rf', resolve(tmsConfig.outputDir));
9
10
 
10
- const { modules: newModules, subPackages } = await init(tmsConfig, targetModules);
11
+ const { modules: newModules } = await init(tmsConfig, targetModules);
11
12
 
12
13
  const isDev = false;
13
14
  if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
@@ -15,8 +16,9 @@ async function build(tmsConfig, targetModules) {
15
16
  isDev,
16
17
  tmsConfig: filterField(tmsConfig, ['gitAccount']),
17
18
  modules: newModules });
19
+ report('hooks:beforeCompile');
18
20
  };
19
- compileBuild(tmsConfig, newModules, subPackages, isDev);
21
+ compileBuild(tmsConfig, newModules, isDev);
20
22
  }
21
23
 
22
24
  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
  }
@@ -5,7 +5,9 @@ const init = require('../init/index');
5
5
  const { getModulesByMergeDepModules, getSubPackages } = require('../../../core/tmsMpconfig');
6
6
  const { info } = require('../../../utils/log');
7
7
  const { global } = require('../../../utils/global');
8
- const { CACHE_DIR } = require('../../../config/constant');
8
+ const { MODULE_CODE_DIR, NODE_MODULES_DIR } = require('../../../config/constant');
9
+ const report = require('../../../core/report');
10
+ const { recommendVersion } = require('../../../core/recommendVersion');
9
11
 
10
12
 
11
13
  // 用户编译分包时,需要将dist中其他分包(主包不能删除)的内容删除,否则其他分包的内容混入到主包(导致主包的体积超2M)
@@ -29,8 +31,11 @@ async function dev(tmsConfig, targetModules) {
29
31
  const { noCache } = global.getData('cmd');
30
32
  if (noCache) {
31
33
  shelljs.rm('-rf', resolve(tmsConfig.outputDir));
32
- shelljs.rm('-rf', CACHE_DIR);
34
+ shelljs.rm('-rf', MODULE_CODE_DIR);
35
+ shelljs.rm('-rf', NODE_MODULES_DIR);
33
36
  }
37
+ // 推荐tmskit的版本
38
+ await recommendVersion();
34
39
 
35
40
  // 初始化操作
36
41
  const { subPackages, modules: newModules } = await init(tmsConfig, targetModules);
@@ -42,9 +47,10 @@ async function dev(tmsConfig, targetModules) {
42
47
  tmsConfig: filterField(tmsConfig, ['gitAccount']),
43
48
  modules: newModules,
44
49
  });
50
+ report('hooks:beforeCompile');
45
51
  };
46
52
  delOtherPackages(tmsConfig, subPackages);
47
- compileDev(tmsConfig, newModules, subPackages, true);
53
+ compileDev(tmsConfig, newModules, true);
48
54
  }
49
55
 
50
56
  module.exports = dev;
@@ -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');
@@ -7,12 +6,12 @@ const install = require('./install/index');
7
6
  const cloud = require('./cloud/index');
8
7
  const { fail, info } = require('../../utils/log');
9
8
  const { global } = require('../../utils/global');
9
+ const report = require('../../core/report');
10
10
  const {
11
- readTmsConfig,
12
- readTmsPrivateCf,
13
- checkModules,
11
+ getTmsConfig,
14
12
  getModulesByMergeDepModules,
15
13
  getSubPackages,
14
+ getModulesByModuleNames,
16
15
  } = require('../../core/tmsMpconfig');
17
16
 
18
17
  const handleModuleArg = (cmd) => {
@@ -28,7 +27,7 @@ const handleModuleArg = (cmd) => {
28
27
  * @param {Object} modulePrivateCfg 私有配置里的模块
29
28
  * @param {Array} moduleAll 当前小程序全部模块
30
29
  */
31
- const getSpecificModules = (moduleArg, modules) => {
30
+ const getSpecificModuleNames = (moduleArg, modules) => {
32
31
  if (moduleArg.length > 0) {
33
32
  return moduleArg;
34
33
  }
@@ -46,92 +45,69 @@ const getSpecificModules = (moduleArg, modules) => {
46
45
  return all.map(item => item.moduleName);
47
46
  };
48
47
 
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
48
  async function run(commandName, cmd) {
49
+ // 用户本地的配置
50
+ const tmsConfig = getTmsConfig();
72
51
  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);
100
-
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
+ report('run:cloud', { appName: tmsConfig?.appName });
61
+ return;
128
62
  }
63
+ otherCommands(tmsConfig, commandName, cmd);
129
64
  } catch (error) {
130
65
  const errMsg = typeof error === 'object' ? error.message : error;
66
+ report('run', { errMsg, appName: tmsConfig?.appName });
131
67
  fail(`构建出现错误: ${errMsg}`);
132
68
  info('详细错误信息', error);
133
69
  process.exit(1);
134
70
  }
135
71
  }
136
72
 
73
+ function otherCommands(tmsConfig, commandName, cmd) {
74
+ // 处理module参数
75
+ const specificModuleNames = getSpecificModuleNames(
76
+ handleModuleArg(cmd),
77
+ tmsConfig.modules,
78
+ );
79
+
80
+ // moduleNames => ['home', 'car']
81
+ const moduleNames = [...new Set([...specificModuleNames])];
82
+ const modules = getModulesByModuleNames(tmsConfig, moduleNames, false);
83
+
84
+ // 获取所有模块,合并模块依赖的模块
85
+ const newModules = getModulesByMergeDepModules(tmsConfig, modules, false);
86
+ // 获取所有的分包
87
+ const subPackages = getSubPackages(newModules);
88
+
89
+ switch (commandName) {
90
+ case 'init':
91
+ init(tmsConfig, newModules);
92
+ report('run:init', { appName: tmsConfig?.appName });
93
+ return;
94
+ case 'dev':
95
+ global.setData('isDev', true);
96
+ dev(tmsConfig, newModules);
97
+ report('run:dev', { appName: tmsConfig?.appName });
98
+ return;
99
+ case 'install':
100
+ install(tmsConfig, subPackages, false);
101
+ report('run:install', { appName: tmsConfig?.appName });
102
+ return;
103
+ case 'build':
104
+ global.setData('isDev', false);
105
+ build(tmsConfig, newModules);
106
+ report('run:build', { appName: tmsConfig?.appName });
107
+ return;
108
+ default:
109
+ return;
110
+ }
111
+ }
112
+
137
113
  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
  }
@@ -1,10 +1,10 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
- const { CACHE_FILE, CACHE_DIR } = require('../config/constant');
4
- const { ensureDirExist } = require('../utils/io');
3
+ const { NPM_CACHE_FILE } = require('../../../config/constant');
4
+ const { ensureDirExist } = require('../../../utils/io');
5
5
 
6
6
  function getCache(projectDir, type) {
7
- const filePath = `${CACHE_DIR}/${CACHE_FILE}`;
7
+ const filePath = NPM_CACHE_FILE;
8
8
  if (!fs.existsSync(filePath)) {
9
9
  return null;
10
10
  }
@@ -14,7 +14,7 @@ function getCache(projectDir, type) {
14
14
 
15
15
 
16
16
  function setCache(projectDir, type = 'miniprogram_npm', data) {
17
- const filePath = `${CACHE_DIR}/${CACHE_FILE}`;
17
+ const filePath = NPM_CACHE_FILE;
18
18
  if (!fs.existsSync(filePath)) {
19
19
  const dir = path.dirname(filePath);
20
20
  ensureDirExist(dir);
@@ -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('../../../core/cache');
8
- const { CACHE_DIR } = require('../../../config/constant');
7
+ const { setCache, getCache } = require('./cache');
8
+ const { NODE_MODULES_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), NODE_MODULES_DIR);
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);