@tmsfe/tmskit 0.0.17 → 0.0.20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tmskit",
3
- "version": "0.0.17",
3
+ "version": "0.0.20",
4
4
  "description": "tmskit",
5
5
  "main": "dist/index.cjs",
6
6
  "bin": {
@@ -41,21 +41,16 @@
41
41
  "dependencies": {
42
42
  "ansi-colors": "1.1.0",
43
43
  "anymatch": "^1.3.0",
44
- "fancy-log": "1.3.2",
45
- "glob-parent": "^3.0.1",
46
- "path-is-absolute": "^1.0.1",
47
- "readable-stream": "^2.2.2",
48
- "slash": "^1.0.0",
49
- "vinyl": "^2.1.0",
50
- "vinyl-file": "^2.0.0",
51
44
  "async": "^3.2.2",
52
45
  "chalk": "^4.1.0",
53
46
  "chokidar": "^3.5.3",
54
47
  "commander": "^8.3.0",
55
48
  "copy-webpack-plugin": "^9.1.0",
56
49
  "ejs": "^3.1.5",
50
+ "fancy-log": "1.3.2",
57
51
  "fs-extra": "^10.0.1",
58
52
  "glob-ignore": "^1.0.2",
53
+ "glob-parent": "^3.0.1",
59
54
  "gulp": "^4.0.2",
60
55
  "gulp-watch": "^5.0.1",
61
56
  "htmlparser2": "^7.2.0",
@@ -67,13 +62,20 @@
67
62
  "moment": "^2.29.2",
68
63
  "object-assign": "^4.0.1",
69
64
  "ora": "^5.1.0",
65
+ "path-is-absolute": "^1.0.1",
70
66
  "plugin-error": "^1.0.0",
71
67
  "postcss": "^8.4.6",
72
68
  "precinct": "^8.3.1",
69
+ "readable-stream": "^2.2.2",
73
70
  "replace-ext": "^2.0.0",
71
+ "request": "^2.88.2",
74
72
  "shelljs": "^0.8.4",
73
+ "slash": "^1.0.0",
75
74
  "strip-comments": "^2.0.1",
76
75
  "through2": "^4.0.2",
76
+ "unzipper": "^0.10.11",
77
+ "vinyl": "^2.1.0",
78
+ "vinyl-file": "^2.0.0",
77
79
  "vinyl-sourcemaps-apply": "^0.2.0"
78
80
  },
79
81
  "engines": {
@@ -1,5 +1,5 @@
1
1
  const dev = require('./dev');
2
2
 
3
- module.exports = async (tmsConfig, newModules) => {
4
- dev(tmsConfig, newModules, false);
3
+ module.exports = async (tmsConfig, newModules, subPackages, isDev) => {
4
+ dev(tmsConfig, newModules, subPackages, isDev);
5
5
  };
@@ -3,7 +3,7 @@ const fs = require('fs');
3
3
  const ora = require('ora');
4
4
  const chalk = require('chalk');
5
5
  const { parallel, series } = require('gulp');
6
- const { resolve, mergeMap } = require('../utils/widgets');
6
+ const { resolve, mergeMap, filterField } = require('../utils/widgets');
7
7
  const { buildOutputAppJson } = require('../core/buildAppJson');
8
8
  const { DEFAULT_COPY_CONFIG } = require('../config/constant');
9
9
  const compile = require('./compile');
@@ -30,21 +30,19 @@ function excludeGlob(glob) {
30
30
  return Array.from(otherArr);
31
31
  }
32
32
 
33
- function adaptPath(pathDir) {
33
+ function adaptPath(pathDir, cwd = '') {
34
34
  let newPath = pathDir;
35
- newPath = newPath.startsWith('/') ? newPath : resolve(newPath);
35
+ newPath = newPath.startsWith('/') ? newPath : resolve(cwd, newPath);
36
36
  newPath = newPath.endsWith('/') ? newPath.slice(0, newPath.length - 1) : newPath;
37
37
  return newPath;
38
38
  }
39
- module.exports = async (tmsConfig, newModules, isDev = true) => {
39
+ module.exports = async (tmsConfig, modules, subPackages, isDev = true) => {
40
40
  const compileTasksMap = new Map();
41
41
 
42
42
  // 监听根目录的文件
43
43
  mergeMap(compileTasksMap, compile(tmsConfig, {
44
44
  glob: {
45
45
  json: DEFAULT_COPY_CONFIG.map(item => resolve(item)),
46
- // wxss: ['app.less', 'app.wxss'].map(item => resolve(item)),
47
- // js: ['app.js', 'app.ts'].map(item => resolve(item)),
48
46
  },
49
47
  module: { from: resolve(), to: resolve(tmsConfig.outputDir) },
50
48
  destPath: resolve(tmsConfig.outputDir),
@@ -53,40 +51,47 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
53
51
  }));
54
52
 
55
53
  // 监听模块的文件
56
- for (let module of newModules) {
54
+ for (let pkg of subPackages) {
57
55
  // 处理默认参数
58
- module = {
56
+ pkg = {
59
57
  ...{ exclude: [] },
60
- ...module,
58
+ ...pkg,
61
59
  };
62
- const modulePath = adaptPath(module.path);
60
+ const packagePath = adaptPath(pkg.path);
63
61
 
64
62
  if (isDev) {
65
63
  // 监听模块配置文件
66
64
  watch(
67
- [`${modulePath}/**/module.config.json`],
65
+ [`${packagePath}/**/module.config.json`],
68
66
  { events: watchEvents },
69
- () => buildOutputAppJson(tmsConfig, newModules, isDev),
70
- { from: modulePath, to: modulePath },
67
+ () => buildOutputAppJson(tmsConfig, modules, isDev),
68
+ { from: packagePath, to: packagePath },
71
69
  );
72
70
  }
73
71
 
74
- const excludes = module.exclude.map(key => `!${resolve(key)}`);
72
+ const excludes = pkg.exclude.map((ePath) => {
73
+ const newPath = adaptPath(ePath, packagePath);
74
+ const ext = path.extname(ePath).slice(1);
75
+ if (ext) {
76
+ return `!${resolve(packagePath, newPath)}`;
77
+ }
78
+ return `!${resolve(packagePath, newPath)}/**/*`;
79
+ });
75
80
  const glob = {
76
- js: [`${modulePath}/**/*.{js,ts,wxs}`, ...excludes],
77
- json: [`${modulePath}/**/*.json`, `!${modulePath}/**/module.config.json`, ...excludes],
78
- wxss: [`${modulePath}/**/*.{less,wxss}`, ...excludes],
79
- wxml: [`${modulePath}/**/*.wxml`, ...excludes],
80
- image: [`${modulePath}/**/*.{png,jpg,jpeg,gif,svg}`, ...excludes],
81
+ js: [`${packagePath}/**/*.{js,ts,wxs}`, ...excludes],
82
+ json: [`${packagePath}/**/*.json`, `!${packagePath}/**/module.config.json`, ...excludes],
83
+ wxss: [`${packagePath}/**/*.{less,wxss,scss,sass,styl}`, ...excludes],
84
+ wxml: [`${packagePath}/**/*.wxml`, ...excludes],
85
+ image: [`${packagePath}/**/*.{png,jpg,jpeg,gif,svg}`, ...excludes],
81
86
  };
82
87
 
83
88
  mergeMap(compileTasksMap, compile(tmsConfig, {
84
89
  glob: {
85
90
  ...glob,
86
- other: [`${modulePath}/**/*`, ...excludeGlob(glob)],
91
+ other: [`${packagePath}/**/*`, ...excludeGlob(glob)],
87
92
  },
88
- destPath: resolve(tmsConfig.outputDir, module.root),
89
- module: { from: module.path, to: module.root },
93
+ destPath: resolve(tmsConfig.outputDir, pkg.root),
94
+ module: { from: pkg.path, to: pkg.root },
90
95
  srcOption: { allowEmpty: true },
91
96
  isDev,
92
97
  }));
@@ -106,7 +111,7 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
106
111
  glob = {
107
112
  js: [`${item.from}/**/*.{js,ts,wxs}`],
108
113
  json: [`${item.from}/**/*.json`],
109
- wxss: [`${item.from}/**/*.{less,wxss}`],
114
+ wxss: [`${item.from}/**/*.{less,wxss,scss,sass,styl}`],
110
115
  wxml: [`${item.from}/**/*.wxml`],
111
116
  image: [`${item.from}/**/*.{png,jpg,jpeg,gif,svg}`],
112
117
  };
@@ -142,7 +147,7 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
142
147
  watch(
143
148
  [resolve('app.json')],
144
149
  { ignoreInitial: false, events: watchEvents },
145
- () => buildOutputAppJson(tmsConfig, newModules, isDev),
150
+ () => buildOutputAppJson(tmsConfig, modules, isDev),
146
151
  { from: resolve(), to: resolve(tmsConfig.outputDir) },
147
152
  );
148
153
  // 监听其他文件
@@ -150,11 +155,15 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
150
155
  watch(globValue, { ignoreInitial: true, events: watchEvents }, taskFn, module);
151
156
  });
152
157
  } else {
153
- buildOutputAppJson(tmsConfig, newModules, isDev);
158
+ buildOutputAppJson(tmsConfig, modules, isDev);
154
159
  }
155
160
  eTime = new Date().getTime() - sTime;
156
161
  if (typeof tmsConfig?.hooks?.afterCompile === 'function') {
157
- await tmsConfig?.hooks?.afterCompile({ isDev, tmsConfig, modules: newModules });
162
+ await tmsConfig?.hooks?.afterCompile({
163
+ isDev,
164
+ tmsConfig: filterField(tmsConfig, ['gitAccount']),
165
+ modules,
166
+ });
158
167
  }
159
168
  spinner.succeed(chalk.green(`首次编译完成, 耗时${eTime / 1000}s, 微信开发者工具打开项目即可预览。`));
160
169
  spinner.stop();
@@ -1,25 +1,44 @@
1
+ /* eslint-disable no-restricted-syntax */
1
2
  const watch = require('./plugins/gulp-watch');
2
3
  const path = require('path');
3
4
  const shellJs = require('shelljs');
4
- const { info, warn } = require('../utils/log');
5
+ const { info, warn, fail } = require('../utils/log');
5
6
  const { resolve } = require('../utils/widgets');
6
7
  const { global } = require('../utils/global');
8
+ const { checkPackageVersion } = require('../core/checkDependencies');
7
9
  const { findAllFilesOfDir } = require('../utils/io');
8
10
 
9
11
  const TIP_MAP = {
10
- 'package.json': '若依赖有变动,请重新执行tmskit run dev',
11
- 'module.config.json': '若dependencies字段有变动,需要重新执行tmskit run dev才会生效',
12
+ 'package.json': {
13
+ tip: '依赖版本有变动,请重新执行tmskit run dev',
14
+ check: (sourceFile, targetFile) => {
15
+ const targetArr = targetFile.split('/');
16
+ targetArr[targetArr.length - 1] = 'node_modules';
17
+ const packageArr = [{
18
+ srcPackageDir: sourceFile,
19
+ destNpmDir: targetArr.join('/'),
20
+ }];
21
+ return checkPackageVersion(packageArr);
22
+ },
23
+ },
24
+ 'module.config.json': {
25
+ tip: '若dependencies字段有变动,请重新执行tmskit run dev才会生效',
26
+ check: () => true,
27
+ },
12
28
  };
13
29
 
14
- const logTip = (fileName, tipMap) => {
15
- // eslint-disable-next-line
16
- for (const item in tipMap) {
17
- if (fileName.indexOf(item) > -1) {
18
- warn(`${fileName}有更新, ${tipMap[item]}`);
19
- return;
30
+ const logTip = (fileName, sourceFile, targetFile, tipMap) => {
31
+ try {
32
+ for (const item in tipMap) {
33
+ if (fileName.indexOf(item) > -1 && tipMap[item].check(sourceFile, targetFile)) {
34
+ warn(`${fileName}有更新, ${tipMap[item].tip}`);
35
+ return;
36
+ }
20
37
  }
38
+ info(`${fileName}有更新`);
39
+ } catch (e) {
40
+ fail(`${sourceFile}文件更新提示出现错误: ${e.message}`);
21
41
  }
22
- info(`${fileName}有更新`);
23
42
  };
24
43
 
25
44
  const getTargetFile = (sourceFile, module, outputDir) => {
@@ -46,7 +65,7 @@ module.exports = function (globValue, watchOptions, callback, module) {
46
65
  const fileDirArr = file.replace(/\\/g, '/').split('/');
47
66
  const fileName = fileDirArr.slice(fileDirArr.length - 2).join('/');
48
67
  const targetFile = getTargetFile(file, module, tmsConfig.outputDir);
49
- logTip(fileName, TIP_MAP);
68
+ logTip(fileName, sourceFile, targetFile, TIP_MAP);
50
69
  callback([file], path.dirname(targetFile));
51
70
  }
52
71
  return;
@@ -59,7 +78,7 @@ module.exports = function (globValue, watchOptions, callback, module) {
59
78
  return;
60
79
  }
61
80
 
62
- logTip(sourceFileName, TIP_MAP);
81
+ logTip(sourceFileName, sourceFile, targetFile, TIP_MAP);
63
82
  return callback([sourceFile], path.dirname(targetFile));
64
83
  });
65
84
  };
@@ -7,14 +7,21 @@ const HOME_DIR = os.homedir();
7
7
  // 所有文件的缓存目录
8
8
  const CACHE_DIR = path.resolve(HOME_DIR, '.tmskit');
9
9
 
10
+ // 缓存文件
11
+ const CACHE_FILE = 'cache_file.json';
12
+
10
13
  // 脚手架模板代码所在目录
11
14
  const TEMPLATE_DIR = path.resolve(CACHE_DIR, 'template');
12
15
 
16
+ // 模板的名称
17
+ const TEMPLATE_NAME = 'tmskit-template';
18
+
13
19
  // 第三方模块源码存放的临时缓存目录
14
20
  const MODULE_CODE_DIR = path.resolve(CACHE_DIR, 'modules_code');
15
21
 
16
- // 脚手架模板代码的具体路径
17
- const TEMPLATE_PATH = path.resolve(TEMPLATE_DIR, 'tools/tms-cli-template');
22
+
23
+ // 脚手架模板的远程地址
24
+ const TEMPLATE_URL = 'https://tms-web-1g1czzwka2fd06f2-1301126013.tcloudbaseapp.com/tmskit-template/tmskit-template.zip';
18
25
 
19
26
  // 脚手架的名称
20
27
  const TMS_NAME = 'tmskit';
@@ -47,13 +54,29 @@ const ENV = {
47
54
  };
48
55
 
49
56
  const TEMPLATE_TKIT_DIR = '_tmskit';
50
- const MODULE_CONFIG_INVALID_KEY = ['entranceDeclare', 'entryPagePath'];
57
+
58
+ const CREATE_TEMPLATE_QUESTION = [
59
+ {
60
+ type: 'list',
61
+ name: 'projectType',
62
+ message: '选择你要创建的模板',
63
+ choices: [{
64
+ name: '小程序项目',
65
+ value: 'mp',
66
+ }, {
67
+ name: '小程序模块',
68
+ value: 'mp-module',
69
+ }],
70
+ },
71
+ ];
72
+
51
73
 
52
74
  export {
53
75
  HOME_DIR,
54
76
  CACHE_DIR,
77
+ CACHE_FILE,
55
78
  TEMPLATE_DIR,
56
- TEMPLATE_PATH,
79
+ TEMPLATE_NAME,
57
80
  TMS_NAME,
58
81
  TMS_CONFIG_FILENAME,
59
82
  TMS_PRIVATE_FILENAME,
@@ -64,6 +87,7 @@ export {
64
87
  MODULE_CODE_DIR,
65
88
  ENV,
66
89
  TEMPLATE_TKIT_DIR,
67
- MODULE_CONFIG_INVALID_KEY,
68
90
  DEFAULT_CLOUD_MODULE_DIR,
91
+ CREATE_TEMPLATE_QUESTION,
92
+ TEMPLATE_URL,
69
93
  };
@@ -3,7 +3,7 @@ module.exports = {
3
3
  // 全局的环境配置项
4
4
  envData: {},
5
5
  // 模块配置信息
6
- modules: [],
6
+ modules: {},
7
7
  cloudDir: 'cloud',
8
8
  // 第三方依赖代码需要拷贝到本项目的
9
9
  dependencies: {},
@@ -3,10 +3,9 @@
3
3
  */
4
4
  /* eslint-disable no-param-reassign */
5
5
  const fs = require('fs');
6
- const { MODULE_CONFIG_FILENAME, MODULE_CONFIG_INVALID_KEY } = require('../config/constant');
7
- const { getModuleConfig } = require('./tmsMpconfig');
8
- const { fail } = require('../utils/log');
9
- const { resolve, isObject } = require('../utils/widgets');
6
+ const { getSubPackages, getModulesConfig } = require('./tmsMpconfig');
7
+ const { fail, info } = require('../utils/log');
8
+ const { resolve, filterField } = require('../utils/widgets');
10
9
  const { handleError } = require('./handleError');
11
10
  const { global } = require('../utils/global');
12
11
 
@@ -56,23 +55,9 @@ const getAppJsonContent = (sourceAppJsonPath) => {
56
55
  // 加入默认值
57
56
  appJson.subpackages = [];
58
57
  appJson.pages = [];
59
- // appJson.plugins = {};
60
- delete appJson.entranceDeclare;
61
58
  return appJson;
62
59
  };
63
- /**
64
- * 更新app.json中的subpackages
65
- * @param {Object} appJson
66
- * @param {Object} modulesConfigs
67
- */
68
- const updateSubpackages = (appJson, modulesConfigs) => {
69
- // eslint-disable-next-line
70
- for (const modulePath in modulesConfigs) {
71
- const moduleInfo = isObject(modulesConfigs[modulePath]) ? [modulesConfigs[modulePath]] : modulesConfigs[modulePath];
72
- appJson.subpackages = appJson.subpackages.concat(moduleInfo);
73
- }
74
- appJson.subpackages.sort((item1, item2) => item1.name.localeCompare(item2.name));
75
- };
60
+
76
61
 
77
62
  /**
78
63
  * 处理合并subpackages后的appjson, 整理重复不合法的地方
@@ -83,37 +68,35 @@ const fixAppJson = (appJson) => {
83
68
  const pluginsMap = {};
84
69
  Object.keys(appJson.plugins || {}).forEach(key => pluginsMap[key] = ['app.json']);
85
70
  const subps = subpackages.map((subp) => {
86
- const invalidKeys = [];
71
+ const arrOfFileType = ['requiredBackgroundModes', 'embeddedAppIdList'];
72
+ const objOfFileType = ['preloadRule'];
87
73
  Object.keys(subp).forEach((key) => {
88
- if (key === 'dependencies') {
89
- // eslint-disable-next-line
90
- delete subp.dependencies;
91
- return;
92
- }
93
74
  if (key === 'plugins') {
94
75
  Object.keys(subp.plugins).forEach((pk) => {
95
- pluginsMap[pk] ? pluginsMap[pk].push(`分包${subp.name}`) : pluginsMap[pk] = [`分包${subp.name}`];
76
+ pluginsMap[pk]
77
+ ? pluginsMap[pk].push(`分包${subp.name}`)
78
+ : pluginsMap[pk] = [`分包${subp.name}`];
96
79
  });
97
- return;
98
- }
99
- if (MODULE_CONFIG_INVALID_KEY.indexOf(key) > -1) {
100
- // 如果分包配置中有不支持的key,则错误提醒
101
- invalidKeys.push(key);
102
- return;
103
80
  }
104
- if (['requiredBackgroundModes', 'embeddedAppIdList'].indexOf(key) > -1) {
105
- // 提到appjson最上层处理
81
+ // 分包里数组类型字段,提到appjson最上层
82
+ if (arrOfFileType.indexOf(key) > -1) {
106
83
  const preVal = appJson[key];
107
- preVal ? appJson[key] = Array.from(new Set(preVal
108
- .slice(0).concat(subp[key]))) : appJson[key] = subp[key].slice(0);
109
- return;
84
+ preVal
85
+ ? appJson[key] = Array.from(new Set(preVal.slice(0).concat(subp[key])))
86
+ : appJson[key] = subp[key].slice(0);
87
+ }
88
+ // 分包里对象类型字段,提到appjson最上层
89
+ if (objOfFileType.indexOf(key) > -1) {
90
+ const preloadRuleMap = appJson[key] || {};
91
+ subp[key] && Object.keys(subp[key]).forEach((page) => {
92
+ if (!preloadRuleMap[page]) {
93
+ preloadRuleMap[page] = subp[key][page];
94
+ }
95
+ });
96
+ appJson[key] = preloadRuleMap;
110
97
  }
111
98
  });
112
- if (invalidKeys.length) {
113
- fail(`不支持分包${subp?.name}配置${invalidKeys.join(',')}\n`);
114
- }
115
- invalidKeys.concat(['requiredBackgroundModes', 'embeddedAppIdList']).forEach(k => delete subp[k]);
116
- return subp;
99
+ return filterField(subp, [...arrOfFileType, ...objOfFileType, 'dependencies', 'path']);
117
100
  });
118
101
  // 如果plugins重复,则错误提示
119
102
  const pluginsErrMsg = Object.keys(pluginsMap).map((pk) => {
@@ -124,7 +107,7 @@ const fixAppJson = (appJson) => {
124
107
  })
125
108
  .reduce((pre, cur) => pre + cur, '');
126
109
  if (pluginsErrMsg) {
127
- fail(`plugins配置出现错误:${pluginsErrMsg}`);
110
+ throw new Error(`plugins配置出现错误:${pluginsErrMsg}`);
128
111
  }
129
112
  appJson.subpackages = subps;
130
113
  };
@@ -137,27 +120,36 @@ const fixAppJson = (appJson) => {
137
120
  */
138
121
  function buildOutputAppJson(tmsConfig, modules) {
139
122
  try {
140
- // 获取当前 modules 下的所有子模块的配置内容
141
- const modulesConfigs = getModuleConfig(modules, tmsConfig.appName, MODULE_CONFIG_FILENAME);
123
+ // 获取所有模块,合并模块依赖的模块
124
+ const modulesConfig = getModulesConfig(modules, tmsConfig.appName, false);
125
+ // 获取所有的分包
126
+ const subPackages = getSubPackages(modulesConfig);
142
127
  // 获取app.json的配置
143
128
  const appJson = getAppJsonContent(resolve('./app.json'));
129
+
144
130
  // 更新app.json中的subpackages
145
- updateSubpackages(appJson, modulesConfigs);
131
+ appJson.subpackages = subPackages;
132
+ appJson.subpackages.sort((item1, item2) => item1.name.localeCompare(item2.name));
133
+
146
134
  // 处理appJson中重复||冲突的地方
147
135
  fixAppJson(appJson);
148
136
  // 更新主包,需在subpackages处理完成后执行, pages/
149
137
  updateMainPackages(appJson, tmsConfig.mainPackages);
150
138
 
151
139
  fs.writeFileSync(resolve(`${tmsConfig.outputDir}/app.json`), JSON.stringify(appJson, null, 2), 'utf8');
140
+
152
141
  if (typeof tmsConfig?.hooks?.updateAppJson === 'function') {
153
142
  tmsConfig?.hooks?.updateAppJson({
154
- tmsConfig, module, appJson, isDev: global.getData('isDev'),
143
+ tmsConfig: filterField(tmsConfig, ['gitAccount']),
144
+ modules,
145
+ appJson,
146
+ isDev: global.getData('isDev'),
155
147
  });
156
148
  }
157
-
158
149
  return appJson;
159
150
  } catch (e) {
160
151
  handleError(`生成app.json出现错误: ${e}`);
152
+ info(e);
161
153
  }
162
154
  }
163
155
 
@@ -0,0 +1,36 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { CACHE_FILE, CACHE_DIR } = require('../config/constant');
4
+ const { ensureDirExist } = require('../utils/io');
5
+
6
+ function getCache(projectDir, type) {
7
+ const filePath = `${CACHE_DIR}/${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 = `${CACHE_DIR}/${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
+ };
@@ -10,15 +10,8 @@ const getLatestVersion = (npmName) => {
10
10
  return data.stdout || '0.0.0';
11
11
  };
12
12
 
13
- /**
14
- * 检查package.json的依赖大于node_module的版本,则返回true
15
- * @param {*} modules 模块
16
- * @param {*} cwd 待检查package.json所在的目录 (eg: 当前执行脚本的目录)
17
- * @param {*} outputDir 待检查node_modules存放的目录 (eg: dist/node_modules)
18
- * @returns
19
- */
20
- const checkDependencies = (modules, cwd, outputDir) => {
21
- // 步骤1. 收集package.json
13
+ // 收集package.json
14
+ function collectHasPackageJson(modules, cwd, outputDir) {
22
15
  const packageJsonName = 'package.json'; // 查找文件名
23
16
  // 1.1根目录的package.json
24
17
  const packageArr = [
@@ -37,17 +30,40 @@ const checkDependencies = (modules, cwd, outputDir) => {
37
30
  });
38
31
  }
39
32
  });
33
+ return packageArr;
34
+ }
40
35
 
36
+ // 读取package.json的依赖
37
+ function readPackageDependencies(srcPackageDir) {
38
+ const packageJson = fs.readFileSync(srcPackageDir, 'utf-8');
39
+ let dependencies = {};
40
+ try {
41
+ const json = packageJson ? JSON.parse(packageJson) : {};
42
+ dependencies = json?.dependencies || {};
43
+ } catch (e) {
44
+ handleError(`解析${srcPackageDir}报错,请检查是否是正确的json配置项 ${e}`);
45
+ }
46
+ return dependencies;
47
+ }
48
+
49
+ /**
50
+ * 检查package.json的依赖大于node_module的版本,则返回true
51
+ * @param {*} modules 模块
52
+ * @param {*} cwd 待检查package.json所在的目录 (eg: 当前执行脚本的目录)
53
+ * @param {*} outputDir 待检查node_modules存放的目录 (eg: dist/node_modules)
54
+ * @returns
55
+ */
56
+ const isDependenciesUpdate = (modules, cwd, outputDir) => {
57
+ // 步骤1. 收集package.json
58
+ const packageArr = collectHasPackageJson(modules, cwd, outputDir);
41
59
  // 步骤2. 比较package.json的依赖与node_modules依赖的版本号
60
+ return checkPackageVersion(packageArr);
61
+ };
62
+
63
+ // 比较package.json的依赖与node_modules依赖的版本号
64
+ const checkPackageVersion = (packageArr) => {
42
65
  for (const item of packageArr) {
43
- const packageJson = fs.readFileSync(item.srcPackageDir, 'utf-8');
44
- let dependencies = {};
45
- try {
46
- const json = JSON.parse(packageJson);
47
- dependencies = json?.dependencies ? json?.dependencies : {};
48
- } catch (e) {
49
- handleError(`解析${item.srcPackageDir}报错,请检查是否是正确的json配置项 ${e}`);
50
- }
66
+ const dependencies = readPackageDependencies(item.srcPackageDir);
51
67
  const dependenciesKeys = Object.keys(dependencies);
52
68
  for (const key of dependenciesKeys) {
53
69
  const depPath = path.join(item.destNpmDir, key);
@@ -73,5 +89,6 @@ const checkDependencies = (modules, cwd, outputDir) => {
73
89
  };
74
90
 
75
91
  module.exports = {
76
- checkDependencies,
92
+ isDependenciesUpdate,
93
+ checkPackageVersion,
77
94
  };
@@ -18,12 +18,10 @@ const { global } = require('../utils/global');
18
18
  */
19
19
  function replaceGitUrlAccount(httpRepoUrl, moduleName) {
20
20
  // 用户本地的私有项目配置(用来配置环境\模块信息\账号信息)
21
- const tmsPrivateCf = global.getData('tmsPrivateCf');
21
+ const tmsConfig = global.getData('tmsConfig');
22
22
 
23
23
  let gitUrl = httpRepoUrl;
24
- const { username = '', pass = '' } = tmsPrivateCf?.gitAccout?.[moduleName]
25
- || tmsPrivateCf?.gitAccount?.[moduleName]
26
- || {};
24
+ const { username = '', pass = '' } = tmsConfig?.gitAccount?.[moduleName] || {};
27
25
 
28
26
  const urlPrefixReg = /http(s)?:\/\//;
29
27
  if (username && pass && urlPrefixReg.test(gitUrl)) {
@@ -122,10 +120,10 @@ function collectDownLoadTasksMap(sourceDir, targetDir, modules) {
122
120
  const downloadTasksMap = new Map();
123
121
  for (const moduleInfo of modules) {
124
122
  if (moduleInfo.repoInfo) {
125
- const { repoInfo: { buildGitTag: branch, httpRepoUrl, path: gitPath = '' }, path, name } = moduleInfo;
123
+ const { repoInfo: { buildGitTag: branch, httpRepoUrl, path: gitPath = '' }, path, moduleName } = moduleInfo;
126
124
 
127
125
  // 处理仓库权限问题
128
- const gitUrl = replaceGitUrlAccount(httpRepoUrl, name);
126
+ const gitUrl = replaceGitUrlAccount(httpRepoUrl, moduleName);
129
127
  // 根据gitUrl与branch计算md5
130
128
  const md5Key = md5ByGitUrlBranch(gitUrl, branch);
131
129
  // git源码临时存在的源目录
@@ -191,8 +189,8 @@ function collectDownLoadTasksMap(sourceDir, targetDir, modules) {
191
189
  */
192
190
  function checkRemoteModGitUrlBranch(sourceDir, moduleInfo) {
193
191
  if (moduleInfo.repoInfo) {
194
- const { repoInfo: { buildGitTag, httpRepoUrl }, name } = moduleInfo;
195
- const gitUrl = replaceGitUrlAccount(httpRepoUrl, name);
192
+ const { repoInfo: { buildGitTag, httpRepoUrl }, moduleName } = moduleInfo;
193
+ const gitUrl = replaceGitUrlAccount(httpRepoUrl, moduleName);
196
194
  const md5Key = md5ByGitUrlBranch(gitUrl, buildGitTag);
197
195
  if (!fs.existsSync(`${sourceDir}/${md5Key}`)) {
198
196
  return true;
@@ -1,8 +1,14 @@
1
1
  const { fail } = require('../utils/log');
2
2
  const { global } = require('../utils/global');
3
3
 
4
- function handleError(error) {
4
+ function handleError(error, isQuit = false) {
5
5
  const errMsg = typeof error === 'object' ? error.message : error;
6
+
7
+ if (isQuit) {
8
+ fail(errMsg);
9
+ process.exit(1);
10
+ }
11
+
6
12
  const isDev = global.getData('isDev');
7
13
  if (isDev) {
8
14
  fail(errMsg);