@tmsfe/tmskit 0.0.21 → 0.0.24

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.
@@ -0,0 +1,118 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const shelljs = require('shelljs');
4
+ const chalk = require('chalk');
5
+ const request = require('request');
6
+ const inquirer = require('inquirer');
7
+ const { infoNoTime } = require('../utils/log');
8
+ const packageJson = require('../../package.json');
9
+ const { VERSION_CACHE_FILE, VERSION_URL } = require('../config/constant');
10
+ const { ensureDirExist } = require('../utils/io');
11
+ const { versionCompare } = require('../utils/widgets');
12
+
13
+ // 获取推荐的tmskit版本
14
+ function getRecommendVersion() {
15
+ return new Promise((resolve, reject) => {
16
+ request(`${VERSION_URL}?v=${new Date().getTime()}`, (error, response, body) => {
17
+ if (!error && response && response.statusCode === 200) {
18
+ resolve(JSON.parse(body));
19
+ } else {
20
+ reject(response.statusCode);
21
+ }
22
+ });
23
+ });
24
+ }
25
+
26
+ // 获取当前用户tmskit的版本
27
+ function getUserTmskitVersion() {
28
+ const data = shelljs.exec('tmskit -v', { async: false, silent: true });
29
+ if (data.code === 0) {
30
+ // tmskit 0.0.21 => 0.0.21
31
+ return data.slice(7);
32
+ }
33
+ return '0.0.0';
34
+ }
35
+
36
+ // 询问用户是否安装最新版本
37
+ function isInstallLatestVersion() {
38
+ return inquirer.prompt([
39
+ {
40
+ type: 'confirm',
41
+ name: 'isInstall',
42
+ message: '是否安装最新版本',
43
+ choices: [{
44
+ name: '否',
45
+ value: false,
46
+ }, {
47
+ name: '是',
48
+ value: true,
49
+ }],
50
+ },
51
+ ]);
52
+ }
53
+
54
+ // 获取tmskit的版本是否推荐过
55
+ function getVersionIsRecommend(version) {
56
+ const filePath = VERSION_CACHE_FILE;
57
+ if (!fs.existsSync(filePath)) {
58
+ return false;
59
+ }
60
+ const content = require(filePath);
61
+ return content?.[version];
62
+ }
63
+
64
+ // 设置tmskit版本推荐过
65
+ function setVersionRecommend(version, isRecommend) {
66
+ const filePath = VERSION_CACHE_FILE;
67
+ if (!fs.existsSync(filePath)) {
68
+ const dir = path.dirname(filePath);
69
+ ensureDirExist(dir);
70
+ fs.writeFileSync(filePath, '{}');
71
+ }
72
+ const content = require(filePath);
73
+ content[version] = isRecommend;
74
+ fs.writeFileSync(filePath, JSON.stringify(content, null, 2));
75
+ }
76
+
77
+ // 推荐理由
78
+ function displayRecommends(arr = []) {
79
+ infoNoTime(chalk.green('构建工具有新的版本~~:'));
80
+ arr.forEach((item) => {
81
+ infoNoTime(chalk.green(item));
82
+ });
83
+ }
84
+
85
+ // 推荐tmskit的安装版本
86
+ async function recommendVersion() {
87
+ try {
88
+ // 获取当前用户安装的版本
89
+ const tmskitVersion = getUserTmskitVersion();
90
+ // 获取官方推荐版本
91
+ const recommendRes = await getRecommendVersion();
92
+ const recommendVersion = recommendRes.version;
93
+ // 当前用户安装版本小于官方推荐版本 && 该版本没有推荐过
94
+ if (versionCompare(tmskitVersion, recommendVersion) === -1 && !getVersionIsRecommend(recommendVersion)) {
95
+ displayRecommends(recommendRes.version_detail);
96
+ // 设置该版本推荐过
97
+ setVersionRecommend(recommendVersion, true);
98
+ // 询问用户是否安装最新版本
99
+ const installRes = await isInstallLatestVersion(recommendRes);
100
+ if (installRes.isInstall) {
101
+ // mac
102
+ if (process.platform === 'darwin') {
103
+ infoNoTime(`将执行以下命令: ${chalk.green(`sudo npm install -g ${packageJson.name}@${recommendVersion}`)}`);
104
+ shelljs.exec(`sudo npm install -g ${packageJson.name}@${recommendVersion}`, { async: false, silent: false });
105
+ process.exit(-1);
106
+ } else {
107
+ // window
108
+ infoNoTime(`请使用超级管理员执行以下命令: ${chalk.green(`npm install -g ${packageJson.name}@${recommendVersion}`)}`);
109
+ process.exit(-1);
110
+ }
111
+ }
112
+ }
113
+ } catch {}
114
+ }
115
+
116
+ module.exports = {
117
+ recommendVersion,
118
+ };
@@ -0,0 +1,30 @@
1
+ const request = require('request');
2
+ const shelljs = require('shelljs');
3
+ const apiUrl = 'https://tim.map.qq.com/basic/tmskit/upload';
4
+
5
+ function getGitUser() {
6
+ const res = shelljs.exec('git config user.name', { async: false, silent: true });
7
+ return res.stdout;
8
+ }
9
+
10
+ const report = (name, attrs = {}) => {
11
+ try {
12
+ const param = [];
13
+ param[27] = name;
14
+ param[28] = 'tmskit';
15
+ param[29] = getGitUser();
16
+ param[30] = JSON.stringify(attrs);
17
+
18
+ for (let i = 0; i < 40; i++) {
19
+ if (!param[i]) param[i] = null;
20
+ };
21
+
22
+ request.post({ url: apiUrl, json: { param } });
23
+ // (error, response, body) => {
24
+ // console.log('body:', body);
25
+ // }
26
+ } catch (e) {
27
+ }
28
+ };
29
+
30
+ module.exports = report;
@@ -12,28 +12,29 @@ const { fail } = require('../utils/log');
12
12
 
13
13
  /**
14
14
  * 读取tms.config.js
15
- * @param env {string} 环境变量
15
+ * @param {string} configPath tms.config.js的路径
16
16
  */
17
- const readTmsConfig = function () {
18
- const tmsConfigPath = resolve(TMS_CONFIG_FILENAME);
17
+ const readTmsConfig = function (configPath) {
18
+ const tmsConfigPath = configPath ? `${configPath}/${TMS_CONFIG_FILENAME}` : resolve(TMS_CONFIG_FILENAME);
19
19
  if (!fs.existsSync(tmsConfigPath)) {
20
- fail('当前执行目录没有tms.config.js的配置项,请进行配置');
20
+ fail(`${path.dirname(tmsConfigPath)}没有找到tms.config.js,请进行配置`);
21
21
  process.exit(1);
22
22
  }
23
23
  const tmsConfigFn = require(tmsConfigPath);
24
24
  const tmsConfig = typeof tmsConfigFn === 'function' ? tmsConfigFn() : tmsConfigFn;
25
25
 
26
26
  // 合并默认值
27
- return loadash.mergeWith(defaultTmsConfig, tmsConfig);
27
+ return loadash.mergeWith({}, defaultTmsConfig, tmsConfig);
28
28
  };
29
29
 
30
30
 
31
31
  /**
32
32
  * 读取tms.private.config.js
33
+ * @param {string} configPath tms.private.config.js的路径
33
34
  */
34
- const readTmsPrivateCf = function () {
35
+ const readTmsPrivateCf = function (configPath) {
35
36
  let tmsPrivateCf = {};
36
- const tmsPrivatePath = resolve(TMS_PRIVATE_FILENAME);
37
+ const tmsPrivatePath = configPath ? `${configPath}/${TMS_PRIVATE_FILENAME}` : resolve(TMS_PRIVATE_FILENAME);
37
38
  if (fs.existsSync(tmsPrivatePath)) {
38
39
  const tmsPrivateFn = require(tmsPrivatePath);
39
40
  tmsPrivateCf = typeof tmsPrivateFn === 'function' ? tmsPrivateFn() : tmsPrivateFn;
@@ -44,11 +45,12 @@ const readTmsPrivateCf = function () {
44
45
 
45
46
  /**
46
47
  * 获取tms.config.js, tms.private.config.js的配置项
48
+ * @param {string} configPath config.js的路径
47
49
  * @returns
48
50
  */
49
- const getTmsConfig = () => {
50
- const tmsPrivateCf = readTmsPrivateCf();
51
- const tmsConfig = readTmsConfig();
51
+ const getTmsConfig = (configPath) => {
52
+ const tmsPrivateCf = readTmsPrivateCf(configPath);
53
+ const tmsConfig = readTmsConfig(configPath);
52
54
 
53
55
  const modules = {};
54
56
  if (Array.isArray(tmsConfig.modules)) {
package/src/entry.js CHANGED
@@ -6,9 +6,20 @@ module.exports = [
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,71 +1,98 @@
1
+ /* eslint-disable no-param-reassign */
1
2
  const chalk = require('chalk');
2
3
  const commander = require('commander');
3
- const { suggestCommands, resolve } = 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');
5
8
  const { getTmsConfig } = require('../src/core/tmsMpconfig');
6
9
  const { TMS_NAME } = require('./config/constant.js');
7
10
  const commands = require('./entry');
8
11
  const check = require('./check');
9
- const fs = require('fs');
12
+ const { loadExtendCmd } = require('./scripts/extend-cmd/index.js');
13
+ const report = require('./core/report');
10
14
 
11
15
  check();
12
-
13
16
  const program = new commander.Command(TMS_NAME);
14
-
15
17
  program
16
18
  .version(`${TMS_NAME} ${require('../package.json').version}`, '-v, -V, --version');
17
19
 
20
+ // 注册命令底层实现
18
21
  function registerCommand(program, commands) {
19
22
  commands.forEach((cmd) => {
20
- if (cmd.type === 'child') {
23
+ if (cmd.type === 'parent') {
21
24
  const childProgram = new commander.Command(cmd.name);
22
25
  cmd.usage && childProgram.usage(cmd.usage);
23
26
  cmd.description && childProgram.description(cmd.description);
24
27
  registerCommand(childProgram, cmd.commands);
25
-
26
28
  program.addCommand(childProgram);
27
29
  } else {
28
30
  const command = program.command(cmd.command);
29
-
30
31
  cmd.usage && command.usage(cmd.usage);
31
-
32
32
  cmd.description && command.description(cmd.description);
33
-
34
33
  cmd.options?.forEach(opt => command.option(...opt));
35
-
34
+ // 上报
35
+ command.hook('preAction', (thisCommand) => {
36
+ report(`${thisCommand._name}-pre`);
37
+ });
38
+ // 上报
39
+ command.hook('postAction', (thisCommand) => {
40
+ report(`${thisCommand._name}-post`);
41
+ });
36
42
  command.action(cmd.action);
37
43
  }
38
44
  });
39
45
  }
40
46
 
41
- function register() {
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) {
42
59
  // 注册脚手架内部命令
43
60
  registerCommand(program, commands);
44
61
 
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
- }
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));
51
72
  }
52
73
  }
53
- register();
54
-
55
- program.on('--help', () => {
56
- info(` Run ${chalk.cyan(`${TMS_NAME} <command> --help`)} for detailed usage of given command.`);
57
- });
74
+ registerAllCmds(program, commands);
58
75
 
59
76
  // 捕获未注册的命令
60
77
  program
61
78
  .arguments('<command>')
79
+ .option('-c, --config <value>', '配置', (value) => {
80
+ // 注册指定配置的扩展命令
81
+ registerExtendCommand(program, path.dirname(resolve(value)));
82
+ })
62
83
  .action((cmd) => {
63
- program.outputHelp();
64
- info(` ${chalk.red(`Unknown command ${chalk.yellow(cmd)}.`)}`);
65
- suggestCommands(cmd);
66
- 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`);
67
90
  });
68
91
 
92
+ program.on('--help', () => {
93
+ infoNoTime(`Run ${chalk.cyan(`${TMS_NAME} <command> --help`)} for detailed usage of given command.`);
94
+ });
95
+
69
96
  if (!process.argv.slice(2).length) {
70
97
  program.outputHelp();
71
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,6 +2,7 @@ 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
  // 开始构建前,清理输出目录
@@ -10,11 +11,12 @@ async function build(tmsConfig, targetModules) {
10
11
  const { modules: newModules } = await init(tmsConfig, targetModules);
11
12
 
12
13
  const isDev = false;
13
- if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
14
- await tmsConfig?.hooks?.beforeCompile({
14
+ if (typeof tmsConfig?.hooks?.beforeFirstCompile === 'function') {
15
+ await tmsConfig?.hooks?.beforeFirstCompile({
15
16
  isDev,
16
17
  tmsConfig: filterField(tmsConfig, ['gitAccount']),
17
18
  modules: newModules });
19
+ report('hooks:beforeCompile');
18
20
  };
19
21
  compileBuild(tmsConfig, newModules, isDev);
20
22
  }
@@ -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,19 +31,23 @@ 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);
37
42
 
38
43
  info('当前dev启动的有效模块', newModules.map(item => item.moduleName).sort());
39
- if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
40
- await tmsConfig?.hooks?.beforeCompile({
44
+ if (typeof tmsConfig?.hooks?.beforeFirstCompile === 'function') {
45
+ await tmsConfig?.hooks?.beforeFirstCompile({
41
46
  isDev: true,
42
47
  tmsConfig: filterField(tmsConfig, ['gitAccount']),
43
48
  modules: newModules,
44
49
  });
50
+ report('hooks:beforeCompile');
45
51
  };
46
52
  delOtherPackages(tmsConfig, subPackages);
47
53
  compileDev(tmsConfig, newModules, true);
@@ -6,6 +6,7 @@ const install = require('./install/index');
6
6
  const cloud = require('./cloud/index');
7
7
  const { fail, info } = require('../../utils/log');
8
8
  const { global } = require('../../utils/global');
9
+ const report = require('../../core/report');
9
10
  const {
10
11
  getTmsConfig,
11
12
  getModulesByMergeDepModules,
@@ -45,10 +46,9 @@ const getSpecificModuleNames = (moduleArg, modules) => {
45
46
  };
46
47
 
47
48
  async function run(commandName, cmd) {
49
+ // 用户本地的配置
50
+ const tmsConfig = getTmsConfig();
48
51
  try {
49
- // 用户本地的配置
50
- const tmsConfig = getTmsConfig();
51
-
52
52
  // 缓存数据
53
53
  global.setData({
54
54
  cmd,
@@ -57,11 +57,13 @@ async function run(commandName, cmd) {
57
57
 
58
58
  if (commandName === 'cloud') {
59
59
  cloud(tmsConfig);
60
+ report('run:cloud', { appName: tmsConfig?.appName });
60
61
  return;
61
62
  }
62
63
  otherCommands(tmsConfig, commandName, cmd);
63
64
  } catch (error) {
64
65
  const errMsg = typeof error === 'object' ? error.message : error;
66
+ report('run', { errMsg, appName: tmsConfig?.appName });
65
67
  fail(`构建出现错误: ${errMsg}`);
66
68
  info('详细错误信息', error);
67
69
  process.exit(1);
@@ -87,17 +89,21 @@ function otherCommands(tmsConfig, commandName, cmd) {
87
89
  switch (commandName) {
88
90
  case 'init':
89
91
  init(tmsConfig, newModules);
92
+ report('run:init', { appName: tmsConfig?.appName });
90
93
  return;
91
94
  case 'dev':
92
95
  global.setData('isDev', true);
93
96
  dev(tmsConfig, newModules);
97
+ report('run:dev', { appName: tmsConfig?.appName });
94
98
  return;
95
99
  case 'install':
96
100
  install(tmsConfig, subPackages, false);
101
+ report('run:install', { appName: tmsConfig?.appName });
97
102
  return;
98
103
  case 'build':
99
104
  global.setData('isDev', false);
100
105
  build(tmsConfig, newModules);
106
+ report('run:build', { appName: tmsConfig?.appName });
101
107
  return;
102
108
  default:
103
109
  return;
@@ -0,0 +1,36 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { NPM_CACHE_FILE } = require('../../../config/constant');
4
+ const { ensureDirExist } = require('../../../utils/io');
5
+
6
+ function getCache(projectDir, type) {
7
+ const filePath = NPM_CACHE_FILE;
8
+ if (!fs.existsSync(filePath)) {
9
+ return null;
10
+ }
11
+ const content = require(filePath);
12
+ return content?.[projectDir]?.[type];
13
+ }
14
+
15
+
16
+ function setCache(projectDir, type = 'miniprogram_npm', data) {
17
+ const filePath = NPM_CACHE_FILE;
18
+ if (!fs.existsSync(filePath)) {
19
+ const dir = path.dirname(filePath);
20
+ ensureDirExist(dir);
21
+ fs.writeFileSync(filePath, '{}');
22
+ }
23
+ const content = require(filePath);
24
+ if (!content[projectDir]) {
25
+ content[projectDir] = {};
26
+ }
27
+ content[projectDir] = {
28
+ [type]: data,
29
+ };
30
+ fs.writeFileSync(filePath, JSON.stringify(content, null, 2));
31
+ }
32
+
33
+ module.exports = {
34
+ setCache,
35
+ getCache,
36
+ };
@@ -4,8 +4,8 @@ const path = require('path');
4
4
  const io = require('../../../utils/io');
5
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
10
  const { info } = require('../../../utils/log');
11
11
  const { fileMd5 } = require('../../../utils/md5');
@@ -52,7 +52,7 @@ async function npmInstall(tmsConfig, subPackages, useCache) {
52
52
  if (fs.existsSync(modulePackagePath)) shelljs.cp('-Rf', modulePackagePath, outputModuleDir);
53
53
  });
54
54
 
55
- await npmInstallAll(subPackages, resolve(tmsConfig.outputDir), `${CACHE_DIR}/node_modules`);
55
+ await npmInstallAll(subPackages, resolve(tmsConfig.outputDir), NODE_MODULES_DIR);
56
56
  return { isCache: false };
57
57
  }
58
58
 
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
  };