@tmsfe/tmskit 0.0.18 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tmskit",
3
- "version": "0.0.18",
3
+ "version": "0.0.21",
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": {
@@ -22,12 +22,11 @@ const checkNodeVersion = (wanted, id) => {
22
22
 
23
23
  /**
24
24
  * 检查运行环境
25
- * @returns {Undefined} 无需返回值
26
25
  */
27
- function initCliContext() {
26
+ function check() {
28
27
  // 执行操作前检查node版本
29
28
  // 旧版本node直接提示升级,退出脚本
30
29
  checkNodeVersion(requiredVersion, packName);
31
30
  }
32
31
 
33
- module.exports = initCliContext;
32
+ module.exports = check;
@@ -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, isDev) => {
4
+ dev(tmsConfig, newModules, 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, filterField } = require('../utils/widgets');
6
+ const { resolve, mergeMap, filterField, getAbsolutePath } = 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,63 +30,62 @@ function excludeGlob(glob) {
30
30
  return Array.from(otherArr);
31
31
  }
32
32
 
33
- function adaptPath(pathDir) {
34
- let newPath = pathDir;
35
- newPath = newPath.startsWith('/') ? newPath : resolve(newPath);
36
- newPath = newPath.endsWith('/') ? newPath.slice(0, newPath.length - 1) : newPath;
37
- return newPath;
38
- }
39
- module.exports = async (tmsConfig, newModules, isDev = true) => {
33
+ module.exports = async (tmsConfig, modules, isDev = true) => {
40
34
  const compileTasksMap = new Map();
41
35
 
42
36
  // 监听根目录的文件
43
37
  mergeMap(compileTasksMap, compile(tmsConfig, {
44
38
  glob: {
45
39
  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
40
  },
49
41
  module: { from: resolve(), to: resolve(tmsConfig.outputDir) },
50
42
  destPath: resolve(tmsConfig.outputDir),
51
43
  srcOption: { allowEmpty: true },
52
44
  isDev,
53
45
  }));
54
-
55
46
  // 监听模块的文件
56
- for (let module of newModules) {
47
+ for (let moduleItem of modules) {
57
48
  // 处理默认参数
58
- module = {
49
+ moduleItem = {
59
50
  ...{ exclude: [] },
60
- ...module,
51
+ ...moduleItem,
61
52
  };
62
- const modulePath = adaptPath(module.path);
53
+ const srcModulePath = getAbsolutePath(moduleItem.path);
54
+ const buildModulePath = resolve(tmsConfig.outputDir, moduleItem.modulePath);
63
55
 
64
56
  if (isDev) {
65
57
  // 监听模块配置文件
66
58
  watch(
67
- [`${modulePath}/**/module.config.json`],
59
+ [`${srcModulePath}/**/module.config.json`],
68
60
  { events: watchEvents },
69
- () => buildOutputAppJson(tmsConfig, newModules, isDev),
70
- { from: modulePath, to: modulePath },
61
+ () => buildOutputAppJson(tmsConfig, modules, isDev),
62
+ { from: srcModulePath, to: buildModulePath },
71
63
  );
72
64
  }
73
65
 
74
- const excludes = module.exclude.map(key => `!${resolve(key)}`);
66
+ const excludes = moduleItem.exclude.map((ePath) => {
67
+ const newPath = getAbsolutePath(ePath, srcModulePath);
68
+ const ext = path.extname(ePath).slice(1);
69
+ if (ext) {
70
+ return `!${resolve(srcModulePath, newPath)}`;
71
+ }
72
+ return `!${resolve(srcModulePath, newPath)}/**/*`;
73
+ });
75
74
  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],
75
+ js: [`${srcModulePath}/**/*.{js,ts,wxs}`, ...excludes],
76
+ json: [`${srcModulePath}/**/*.json`, `!${srcModulePath}/**/module.config.json`, ...excludes],
77
+ wxss: [`${srcModulePath}/**/*.{less,wxss,scss,sass,styl}`, ...excludes],
78
+ wxml: [`${srcModulePath}/**/*.wxml`, ...excludes],
79
+ image: [`${srcModulePath}/**/*.{png,jpg,jpeg,gif,svg}`, ...excludes],
81
80
  };
82
81
 
83
82
  mergeMap(compileTasksMap, compile(tmsConfig, {
84
83
  glob: {
85
84
  ...glob,
86
- other: [`${modulePath}/**/*`, ...excludeGlob(glob)],
85
+ other: [`${srcModulePath}/**/*`, ...excludeGlob(glob)],
87
86
  },
88
- destPath: resolve(tmsConfig.outputDir, module.root),
89
- module: { from: module.path, to: module.root },
87
+ destPath: buildModulePath,
88
+ module: { from: srcModulePath, to: buildModulePath },
90
89
  srcOption: { allowEmpty: true },
91
90
  isDev,
92
91
  }));
@@ -95,8 +94,8 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
95
94
  // 静态资源目录-拷贝
96
95
  if (tmsConfig?.static && tmsConfig?.static.length > 0) {
97
96
  for (const item of tmsConfig.static) {
98
- item.from = adaptPath(item.from);
99
- item.to = adaptPath(item.to);
97
+ item.from = getAbsolutePath(item.from);
98
+ item.to = getAbsolutePath(item.to);
100
99
 
101
100
  let glob = {};
102
101
  const ext = path.extname(item.from).slice(1);
@@ -106,7 +105,7 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
106
105
  glob = {
107
106
  js: [`${item.from}/**/*.{js,ts,wxs}`],
108
107
  json: [`${item.from}/**/*.json`],
109
- wxss: [`${item.from}/**/*.{less,wxss}`],
108
+ wxss: [`${item.from}/**/*.{less,wxss,scss,sass,styl}`],
110
109
  wxml: [`${item.from}/**/*.wxml`],
111
110
  image: [`${item.from}/**/*.{png,jpg,jpeg,gif,svg}`],
112
111
  };
@@ -142,7 +141,7 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
142
141
  watch(
143
142
  [resolve('app.json')],
144
143
  { ignoreInitial: false, events: watchEvents },
145
- () => buildOutputAppJson(tmsConfig, newModules, isDev),
144
+ () => buildOutputAppJson(tmsConfig, modules, isDev),
146
145
  { from: resolve(), to: resolve(tmsConfig.outputDir) },
147
146
  );
148
147
  // 监听其他文件
@@ -150,14 +149,14 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
150
149
  watch(globValue, { ignoreInitial: true, events: watchEvents }, taskFn, module);
151
150
  });
152
151
  } else {
153
- buildOutputAppJson(tmsConfig, newModules, isDev);
152
+ buildOutputAppJson(tmsConfig, modules, isDev);
154
153
  }
155
154
  eTime = new Date().getTime() - sTime;
156
155
  if (typeof tmsConfig?.hooks?.afterCompile === 'function') {
157
156
  await tmsConfig?.hooks?.afterCompile({
158
157
  isDev,
159
158
  tmsConfig: filterField(tmsConfig, ['gitAccount']),
160
- modules: newModules,
159
+ modules,
161
160
  });
162
161
  }
163
162
  spinner.succeed(chalk.green(`首次编译完成, 耗时${eTime / 1000}s, 微信开发者工具打开项目即可预览。`));
@@ -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/tmskit-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';
@@ -48,11 +55,28 @@ const ENV = {
48
55
 
49
56
  const TEMPLATE_TKIT_DIR = '_tmskit';
50
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
+
73
+
51
74
  export {
52
75
  HOME_DIR,
53
76
  CACHE_DIR,
77
+ CACHE_FILE,
54
78
  TEMPLATE_DIR,
55
- TEMPLATE_PATH,
79
+ TEMPLATE_NAME,
56
80
  TMS_NAME,
57
81
  TMS_CONFIG_FILENAME,
58
82
  TMS_PRIVATE_FILENAME,
@@ -64,4 +88,6 @@ export {
64
88
  ENV,
65
89
  TEMPLATE_TKIT_DIR,
66
90
  DEFAULT_CLOUD_MODULE_DIR,
91
+ CREATE_TEMPLATE_QUESTION,
92
+ TEMPLATE_URL,
67
93
  };
@@ -3,10 +3,9 @@
3
3
  */
4
4
  /* eslint-disable no-param-reassign */
5
5
  const fs = require('fs');
6
- const { MODULE_CONFIG_FILENAME } = require('../config/constant');
7
- const { getModuleConfig } = require('./tmsMpconfig');
8
- const { fail } = require('../utils/log');
9
- const { resolve, isObject, filterField } = 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
 
@@ -58,19 +57,7 @@ const getAppJsonContent = (sourceAppJsonPath) => {
58
57
  appJson.pages = [];
59
58
  return appJson;
60
59
  };
61
- /**
62
- * 更新app.json中的subpackages
63
- * @param {Object} appJson
64
- * @param {Object} modulesConfigs
65
- */
66
- const updateSubpackages = (appJson, modulesConfigs) => {
67
- // eslint-disable-next-line
68
- for (const modulePath in modulesConfigs) {
69
- const moduleInfo = isObject(modulesConfigs[modulePath]) ? [modulesConfigs[modulePath]] : modulesConfigs[modulePath];
70
- appJson.subpackages = appJson.subpackages.concat(moduleInfo);
71
- }
72
- appJson.subpackages.sort((item1, item2) => item1.name.localeCompare(item2.name));
73
- };
60
+
74
61
 
75
62
  /**
76
63
  * 处理合并subpackages后的appjson, 整理重复不合法的地方
@@ -109,7 +96,7 @@ const fixAppJson = (appJson) => {
109
96
  appJson[key] = preloadRuleMap;
110
97
  }
111
98
  });
112
- return filterField(subp, [...arrOfFileType, ...objOfFileType, 'dependencies']);
99
+ return filterField(subp, [...arrOfFileType, ...objOfFileType, 'dependencies', 'path']);
113
100
  });
114
101
  // 如果plugins重复,则错误提示
115
102
  const pluginsErrMsg = Object.keys(pluginsMap).map((pk) => {
@@ -133,12 +120,17 @@ const fixAppJson = (appJson) => {
133
120
  */
134
121
  function buildOutputAppJson(tmsConfig, modules) {
135
122
  try {
136
- // 获取当前 modules 下的所有子模块的配置内容
137
- const modulesConfigs = getModuleConfig(modules, tmsConfig.appName, MODULE_CONFIG_FILENAME);
123
+ // 获取所有模块,合并模块依赖的模块
124
+ const modulesConfig = getModulesConfig(modules, tmsConfig.appName, false);
125
+ // 获取所有的分包
126
+ const subPackages = getSubPackages(modulesConfig);
138
127
  // 获取app.json的配置
139
128
  const appJson = getAppJsonContent(resolve('./app.json'));
129
+
140
130
  // 更新app.json中的subpackages
141
- updateSubpackages(appJson, modulesConfigs);
131
+ appJson.subpackages = subPackages;
132
+ appJson.subpackages.sort((item1, item2) => item1.name.localeCompare(item2.name));
133
+
142
134
  // 处理appJson中重复||冲突的地方
143
135
  fixAppJson(appJson);
144
136
  // 更新主包,需在subpackages处理完成后执行, pages/
@@ -157,6 +149,7 @@ function buildOutputAppJson(tmsConfig, modules) {
157
149
  return appJson;
158
150
  } catch (e) {
159
151
  handleError(`生成app.json出现错误: ${e}`);
152
+ info(e);
160
153
  }
161
154
  }
162
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
+ };
@@ -1,6 +1,6 @@
1
1
  const fs = require('fs');
2
2
  const semver = require('semver');
3
- const { resolve } = require('../utils/widgets');
3
+ const { resolve, getAbsolutePath } = require('../utils/widgets');
4
4
  const path = require('path');
5
5
  const shelljs = require('shelljs');
6
6
  const { handleError } = require('./handleError');
@@ -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 collectPackageJson(subPackages, cwd, outputDir) {
22
15
  const packageJsonName = 'package.json'; // 查找文件名
23
16
  // 1.1根目录的package.json
24
17
  const packageArr = [
@@ -28,8 +21,8 @@ const checkDependencies = (modules, cwd, outputDir) => {
28
21
  },
29
22
  ];
30
23
  // 1.2模块的package.json
31
- modules.forEach((item) => {
32
- const srcPackageDir = path.join(cwd, item.path, 'package.json');
24
+ subPackages.forEach((item) => {
25
+ const srcPackageDir = `${getAbsolutePath(item.path)}/package.json`;
33
26
  if (fs.existsSync(srcPackageDir)) {
34
27
  packageArr.push({
35
28
  srcPackageDir,
@@ -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 {*} subPackages 模块
52
+ * @param {*} cwd 待检查package.json所在的目录 (eg: 当前执行脚本的目录)
53
+ * @param {*} outputDir 待检查node_modules存放的目录 (eg: dist/node_modules)
54
+ * @returns
55
+ */
56
+ const isDependenciesUpdate = (subPackages, cwd, outputDir) => {
57
+ // 步骤1. 收集package.json
58
+ const packageArr = collectPackageJson(subPackages, 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
  };
@@ -21,9 +21,7 @@ function replaceGitUrlAccount(httpRepoUrl, moduleName) {
21
21
  const tmsConfig = global.getData('tmsConfig');
22
22
 
23
23
  let gitUrl = httpRepoUrl;
24
- const { username = '', pass = '' } = tmsConfig?.gitAccout?.[moduleName]
25
- || tmsConfig?.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);
package/src/core/npm.js CHANGED
@@ -30,7 +30,12 @@ const collectNpmTasksMap = (packageJsonFiles, cacheDir) => {
30
30
  const npmTasksMap = new Map();
31
31
  for (const packageJsonPath of packageJsonFiles) {
32
32
  const packageContent = fs.readFileSync(packageJsonPath);
33
- const packageJson = JSON.parse(packageContent);
33
+ let packageJson;
34
+ try {
35
+ packageJson = JSON.parse(packageContent);
36
+ } catch (e) {
37
+ throw new Error(`${packageJsonPath}json解析出现错误:${e}`);
38
+ }
34
39
  const md5Obj = {
35
40
  dependencies: packageJson.dependencies || {},
36
41
  };
@@ -99,9 +104,9 @@ const collectNpmTasksMap = (packageJsonFiles, cacheDir) => {
99
104
 
100
105
 
101
106
  // 遍历安装指定目录下所有项目的npm依赖
102
- const mpNpmInstallAll = async (modules, contextDir, cacheDir) => {
107
+ const npmInstallAll = async (subPackages, contextDir, cacheDir) => {
103
108
  const cwd = process.cwd();
104
- const packageJsonFiles = await findAllPackageJson(modules, contextDir);
109
+ const packageJsonFiles = await findAllPackageJson(subPackages, contextDir);
105
110
 
106
111
  // 收集npm install的任务
107
112
  const npmTasksMap = collectNpmTasksMap(packageJsonFiles, cacheDir);
@@ -118,12 +123,13 @@ const mpNpmInstallAll = async (modules, contextDir, cacheDir) => {
118
123
  return Promise.all(callArr);
119
124
  })
120
125
  .catch((e) => {
121
- handleError(`npm install ${params.packageJsonPath}出现错误:${e}`);
126
+ handleError(`npm install ${params.packageJsonPath}出现错误:${e}`, true);
122
127
  }));
123
128
  });
124
129
 
125
130
  await Promise.all(arrPromises);
126
131
  shell.cd(cwd);
132
+ return packageJsonFiles;
127
133
  };
128
134
 
129
135
  /**
@@ -183,10 +189,6 @@ const findAllPackageJson = (subRoots = [], contextDir) => {
183
189
  const result = [path.join(cwd, packageJsonName)]; // 默认填充根目录下的package.json
184
190
 
185
191
  subRoots.forEach((subRoot) => {
186
- if (!subRoot.root) {
187
- log.fail(`请检查${subRoot.name}的module.config.json是否有root字段`);
188
- process.exit(1);
189
- }
190
192
  const toppath = path.join(cwd, subRoot.root); // 从该目录开始查找package.json文件
191
193
  const list = findFilesByFilter(toppath, packageJsonName);
192
194
 
@@ -215,7 +217,8 @@ function cloudNpmInstall(contextDir) {
215
217
 
216
218
  module.exports = {
217
219
  cloudNpmInstall,
218
- mpNpmInstallAll,
220
+ npmInstallAll,
219
221
  findAllPackageJson,
222
+ findFilesByFilter,
220
223
  };
221
224