@tmsfe/tmskit 0.0.5-beta.5 → 0.0.7

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.
Files changed (42) hide show
  1. package/README.md +27 -25
  2. package/dist/index.cjs.js +779 -664
  3. package/main.js +3 -3
  4. package/package.json +75 -69
  5. package/src/config/constant.js +71 -70
  6. package/src/config/defaultTmsConfig.js +16 -16
  7. package/src/entry.js +60 -60
  8. package/src/gulp/build.js +5 -8
  9. package/src/gulp/compile.js +81 -87
  10. package/src/gulp/dev.js +102 -108
  11. package/src/gulp/plugins/less.js +116 -116
  12. package/src/gulp/plugins/mpCommonDep.js +131 -131
  13. package/src/gulp/plugins/mpJsonDep.js +108 -108
  14. package/src/gulp/plugins/mpWxmlDep.js +194 -194
  15. package/src/gulp/plugins/postcss-font-base64.js +72 -72
  16. package/src/gulp/{replaceEnv.js → plugins/replaceEnv.js} +29 -29
  17. package/src/gulp/plugins/utils/pluginError.js +25 -25
  18. package/src/index.js +62 -62
  19. package/src/init.js +33 -33
  20. package/src/scripts/create/ask.js +63 -63
  21. package/src/scripts/create/generator.js +25 -25
  22. package/src/scripts/create/ignoreFiles.js +7 -7
  23. package/src/scripts/create/index.js +72 -72
  24. package/src/scripts/create/render.js +19 -19
  25. package/src/scripts/run/build/index.js +17 -16
  26. package/src/scripts/run/dev/index.js +84 -67
  27. package/src/scripts/run/index.js +68 -63
  28. package/src/scripts/run/init/index.js +87 -80
  29. package/src/scripts/run/install/index.js +29 -31
  30. package/src/utils/buildAppJson.js +221 -200
  31. package/src/utils/checkDependencies.js +77 -77
  32. package/src/utils/cliUtils.js +35 -35
  33. package/src/utils/cloneModules.js +116 -91
  34. package/src/utils/findCssImport.js +30 -30
  35. package/src/utils/global.js +36 -36
  36. package/src/utils/handleError.js +16 -0
  37. package/src/utils/io.js +106 -106
  38. package/src/utils/log.js +44 -44
  39. package/src/utils/mpCiUtils.js +73 -74
  40. package/src/utils/npmUtils.js +166 -148
  41. package/src/utils/tkitUtils.js +158 -84
  42. package/src/utils/widgets.js +167 -173
@@ -1,200 +1,221 @@
1
- const fs = require('fs');
2
- const { DEFAULT_MODULE_DIR, MODULE_CONFIG_FILENAME, MODULE_CONFIG_INVALID_KEY } = require('../config/constant');
3
- const { fail } = require('./log');
4
- const { resolve, isObject, isArray } = require('./widgets');
5
-
6
- /**
7
- * 获取每个模块下面的信息,并且收集,后续更新到appJson里面
8
- * @param { object } file 操作目录下面所有的文件
9
- * @param { string } appName 小程序的名称
10
- */
11
- function setModuleConfig(file, appName, moduleDir) {
12
- const content = file.contents ? JSON.parse(file.contents.toString()) : JSON.parse(file);
13
-
14
- if (isObject(content)) {
15
- content.root = content.root.indexOf(moduleDir) > -1 ? content.root : `${moduleDir}/${content.root}`;
16
- }
17
-
18
- if (isArray(content)) {
19
- let i = content.length - 1;
20
- while (i >= 0) {
21
- let current = content[i];
22
- current.root = current.root.indexOf(moduleDir) > -1 ? current.root : `${moduleDir}/${current.root}`;
23
-
24
- if (appName && current.mpConfig && current.mpConfig[appName]) {
25
- current = { ...current, ...current.mpConfig[appName] };
26
- }
27
-
28
- delete current.mpConfig;
29
- delete current.isSubpackages;
30
-
31
- content[i] = current;
32
- i--; // eslint-disable-line
33
- }
34
- }
35
- return content;
36
- }
37
-
38
- /**
39
- * 递归获取本地所有模块的配置信息
40
- */
41
- function getLocalModuleConfig(modules = [], appName, moduleDir, moduleConfigFilename) {
42
- const modulesConfig = {};
43
-
44
- modules.forEach(({ path }) => {
45
- const moduleConfigPath = resolve(path, moduleConfigFilename);
46
- if (fs.existsSync(moduleConfigPath)) {
47
- const content = fs.readFileSync(moduleConfigPath, 'utf-8');
48
- modulesConfig[moduleConfigPath] = setModuleConfig(content, appName, moduleDir);
49
- }
50
- });
51
- return modulesConfig;
52
- }
53
-
54
- /**
55
- * 更新appJson里面的首页配置
56
- * @param { object } appJson appJson信息
57
- * @param { array } mainPackages 小程序主包信息
58
- * @returns { object } appJson小程序主页配置信息
59
- */
60
- function updateMainPackages(appJson, mainPackages = []) {
61
- let foundMainPackages = appJson.subpackages.filter(subpackage => mainPackages.includes(subpackage.name));
62
- if (foundMainPackages.length === 0) {
63
- // 没找到主包
64
- foundMainPackages = [appJson.subpackages[0]];
65
- }
66
- // 拼装 app.pages
67
- foundMainPackages.forEach((subpackage) => {
68
- if (!subpackage.pages || !subpackage.pages.length) {
69
- fail(`主包 ${subpackage} 不能没有 pages`);
70
- process.exit(-1);
71
- }
72
- subpackage.pages.forEach((page) => {
73
- appJson.pages.push(`${subpackage.root}/${page}`);
74
- });
75
- if (subpackage.plugins) {
76
- Object.assign(appJson.plugins, subpackage.plugins);
77
- }
78
- });
79
-
80
- // 去掉 subpackages 中的主包配置
81
- const foundMainPackageNames = foundMainPackages.map(item => item.name);
82
- // eslint-disable-next-line
83
- appJson.subpackages = appJson.subpackages.filter(subpackage => !foundMainPackageNames.includes(subpackage.name));
84
-
85
- return appJson;
86
- }
87
- /**
88
- * 获取app.json内容
89
- * @param {string} sourceAppJsonPath
90
- * @returns
91
- */
92
- const getAppJsonContent = (sourceAppJsonPath) => {
93
- if (!fs.existsSync(sourceAppJsonPath)) {
94
- fail(`当前路径 ${sourceAppJsonPath} 没找到app.json`);
95
- process.exit(1);
96
- }
97
- const appJson = JSON.parse(fs.readFileSync(sourceAppJsonPath), 'utf-8');
98
- // 加入默认值
99
- appJson.subpackages = [];
100
- appJson.pages = [];
101
- // appJson.plugins = {};
102
- delete appJson.entranceDeclare;
103
- return appJson;
104
- };
105
- /**
106
- * 更新app.json中的subpackages
107
- * @param {Object} appJson
108
- * @param {Object} modulesConfig
109
- */
110
- const updateSubpackages = (appJson, modulesConfig) => {
111
- // eslint-disable-next-line
112
- for (const name in modulesConfig) {
113
- const moduleInfo = isObject(modulesConfig[name]) ? [modulesConfig[name]] : modulesConfig[name];
114
- // eslint-disable-next-line
115
- appJson.subpackages = appJson.subpackages.concat(moduleInfo);
116
- }
117
- };
118
-
119
- /**
120
- * 处理合并subpackages后的appjson, 整理重复不合法的地方
121
- * @param {Object} appJson appjson
122
- */
123
- const processAppJson = (appJson) => {
124
- const { subpackages } = appJson;
125
- const pluginsMap = {};
126
- Object.keys(appJson.plugins || {}).forEach(key => pluginsMap[key] = ['app.json']);
127
- const subps = subpackages.map((subp) => {
128
- const invalidKeys = [];
129
- Object.keys(subp).forEach((key) => {
130
- if (key === 'plugins') {
131
- Object.keys(subp.plugins).forEach((pk) => {
132
- pluginsMap[pk] ? pluginsMap[pk].push(`分包${subp.name}`) : pluginsMap[pk] = [`分包${subp.name}`];
133
- });
134
- return;
135
- }
136
- if (MODULE_CONFIG_INVALID_KEY.indexOf(key) > -1) {
137
- // 如果分包配置中有不支持的key,则错误提醒
138
- invalidKeys.push(key);
139
- return;
140
- }
141
- if (['requiredBackgroundModes', 'embeddedAppIdList'].indexOf(key) > -1) {
142
- // 提到appjson最上层处理
143
- const preVal = appJson[key];
144
- // eslint-disable-next-line
145
- preVal ? appJson[key] = Array.from(new Set(preVal
146
- // eslint-disable-next-line
147
- .slice(0).concat(subp[key]))) : appJson[key] = subp[key].slice(0);
148
- return;
149
- }
150
- });
151
- if (invalidKeys.length) {
152
- fail(`不支持分包${subp?.name}配置${invalidKeys.join(',')}\n`);
153
- }
154
- // eslint-disable-next-line
155
- invalidKeys.concat(['requiredBackgroundModes', 'embeddedAppIdList']).forEach(k => delete subp[k]);
156
- return subp;
157
- });
158
- // 如果plugins重复,则错误提示
159
- const pluginsErrMsg = Object.keys(pluginsMap).map((pk) => {
160
- if (pluginsMap[pk].length > 1) {
161
- return `${pluginsMap[pk].join(',')}重复配置plugin(${pk});`;
162
- }
163
- return '';
164
- })
165
- .reduce((pre, cur) => pre + cur, '');
166
- if (pluginsErrMsg) {
167
- fail(pluginsErrMsg);
168
- }
169
- // eslint-disable-next-line
170
- appJson.subpackages = subps;
171
- };
172
-
173
-
174
- /**
175
- * 动态生成编译后的app.json
176
- * @param {object} tmsConfig
177
- * @param {array} modules
178
- * @returns
179
- */
180
- function buildOutputAppJson(tmsConfig, modules) {
181
- // 获取当前 modules 下的所有子模块的配置内容
182
- const modulesConfig = getLocalModuleConfig(modules, tmsConfig.appName, DEFAULT_MODULE_DIR, MODULE_CONFIG_FILENAME);
183
- // 获取app.json的配置
184
- const appJson = getAppJsonContent(resolve('./app.json'));
185
- // 更新app.json中的subpackages
186
- updateSubpackages(appJson, modulesConfig);
187
- // 处理appJson中重复||冲突的地方
188
- processAppJson(appJson);
189
- // 更新主包,需在subpackages处理完成后执行, pages/
190
- updateMainPackages(appJson, tmsConfig.mainPackages, DEFAULT_MODULE_DIR);
191
-
192
- fs.writeFileSync(resolve(`${tmsConfig.outputDir}/app.json`), JSON.stringify(appJson, null, 2), 'utf8');
193
-
194
- return appJson;
195
- }
196
-
197
- module.exports = {
198
- setModuleConfig,
199
- buildOutputAppJson,
200
- };
1
+ const fs = require('fs');
2
+ const { MODULE_CONFIG_FILENAME, MODULE_CONFIG_INVALID_KEY } = require('../config/constant');
3
+ const { fail } = require('./log');
4
+ const { resolve, isObject, isArray } = require('./widgets');
5
+ const { handleError } = require('./handleError');
6
+
7
+ /**
8
+ * 获取每个模块下面的信息,并且收集,后续更新到appJson里面
9
+ * @param { object } file 操作目录下面所有的文件
10
+ * @param { string } appName 小程序的名称
11
+ */
12
+
13
+ function setModuleConfig(file, appName) {
14
+ const content = file.contents ? JSON.parse(file.contents.toString()) : JSON.parse(file);
15
+
16
+
17
+ if (isArray(content)) {
18
+ let i = content.length - 1;
19
+ while (i >= 0) {
20
+ let current = content[i];
21
+
22
+ if (appName && current.mpConfig && current.mpConfig[appName]) {
23
+ current = { ...current, ...current.mpConfig[appName] };
24
+ }
25
+
26
+ delete current.mpConfig;
27
+ delete current.isSubpackages;
28
+
29
+ content[i] = current;
30
+ i--; // eslint-disable-line
31
+ }
32
+ }
33
+ return content;
34
+ }
35
+
36
+ /**
37
+ * 递归获取本地所有模块的配置信息
38
+ */
39
+ function getLocalModuleConfig(modules = [], appName, moduleConfigFilename) {
40
+ const modulesConfig = {};
41
+
42
+ modules.forEach(({ path }) => {
43
+ const moduleConfigPath = resolve(path, moduleConfigFilename);
44
+ if (fs.existsSync(moduleConfigPath)) {
45
+ const content = fs.readFileSync(moduleConfigPath, 'utf-8');
46
+ modulesConfig[moduleConfigPath] = setModuleConfig(content, appName);
47
+ }
48
+ });
49
+
50
+ return modulesConfig;
51
+ }
52
+
53
+ /**
54
+ * 更新appJson里面的首页配置
55
+ * @param { object } appJson appJson信息
56
+ * @param { array } mainPackages 小程序主包信息
57
+ * @returns { object } appJson小程序主页配置信息
58
+ */
59
+ function updateMainPackages(appJson, mainPackages = []) {
60
+ let foundMainPackages = appJson.subpackages.filter(subpackage => mainPackages.includes(subpackage.name));
61
+ if (foundMainPackages.length === 0) {
62
+ // 没找到主包
63
+ foundMainPackages = [appJson.subpackages[0]];
64
+ }
65
+ // 拼装 app.pages
66
+ foundMainPackages.forEach((subpackage) => {
67
+ if (!subpackage.pages || !subpackage.pages.length) {
68
+ fail(`主包 ${subpackage} 不能没有 pages`);
69
+ process.exit(-1);
70
+ }
71
+ subpackage.pages.forEach((page) => {
72
+ appJson.pages.push(`${subpackage.root}/${page}`);
73
+ });
74
+ if (subpackage.plugins) {
75
+ Object.assign(appJson.plugins, subpackage.plugins);
76
+ }
77
+ });
78
+
79
+ // 去掉 subpackages 中的主包配置
80
+ const foundMainPackageNames = foundMainPackages.map(item => item.name);
81
+ // eslint-disable-next-line
82
+ appJson.subpackages = appJson.subpackages.filter(subpackage => !foundMainPackageNames.includes(subpackage.name));
83
+
84
+ return appJson;
85
+ }
86
+ /**
87
+ * 获取app.json内容
88
+ * @param {string} sourceAppJsonPath
89
+ * @returns
90
+ */
91
+ const getAppJsonContent = (sourceAppJsonPath) => {
92
+ if (!fs.existsSync(sourceAppJsonPath)) {
93
+ fail(`当前路径 ${sourceAppJsonPath} 没找到app.json`);
94
+ process.exit(1);
95
+ }
96
+ const appJson = JSON.parse(fs.readFileSync(sourceAppJsonPath), 'utf-8');
97
+ // 加入默认值
98
+ appJson.subpackages = [];
99
+ appJson.pages = [];
100
+ // appJson.plugins = {};
101
+ delete appJson.entranceDeclare;
102
+ return appJson;
103
+ };
104
+ /**
105
+ * 更新app.json中的subpackages
106
+ * @param {Object} appJson
107
+ * @param {Object} modulesConfig
108
+ */
109
+ const updateSubpackages = (appJson, modulesConfig) => {
110
+ // eslint-disable-next-line
111
+ for (const name in modulesConfig) {
112
+ const moduleInfo = isObject(modulesConfig[name]) ? [modulesConfig[name]] : modulesConfig[name];
113
+ // 过滤 pages 为空的情况
114
+ const validModules = getValidModules(moduleInfo);
115
+ // eslint-disable-next-line
116
+ appJson.subpackages = appJson.subpackages.concat(validModules);
117
+ }
118
+ };
119
+ /**
120
+ * 过滤页面为空的分包
121
+ * @param {Array} moduleCfg 模块配置内容
122
+ * @returns pages不为空的分包
123
+ */
124
+ const getValidModules = (moduleCfg) => {
125
+ // 过滤 pages 为空的情况
126
+ const validModules = moduleCfg.filter(item => item.pages.length > 0);
127
+ return validModules;
128
+ };
129
+
130
+ /**
131
+ * 处理合并subpackages后的appjson, 整理重复不合法的地方
132
+ * @param {Object} appJson appjson
133
+ */
134
+ const processAppJson = (appJson) => {
135
+ const { subpackages } = appJson;
136
+ const pluginsMap = {};
137
+ Object.keys(appJson.plugins || {}).forEach(key => pluginsMap[key] = ['app.json']);
138
+ const subps = subpackages.map((subp) => {
139
+ const invalidKeys = [];
140
+ Object.keys(subp).forEach((key) => {
141
+ if (key === 'dependencies') {
142
+ // eslint-disable-next-line
143
+ delete subp.dependencies;
144
+ return;
145
+ }
146
+ if (key === 'plugins') {
147
+ Object.keys(subp.plugins).forEach((pk) => {
148
+ pluginsMap[pk] ? pluginsMap[pk].push(`分包${subp.name}`) : pluginsMap[pk] = [`分包${subp.name}`];
149
+ });
150
+ return;
151
+ }
152
+ if (MODULE_CONFIG_INVALID_KEY.indexOf(key) > -1) {
153
+ // 如果分包配置中有不支持的key,则错误提醒
154
+ invalidKeys.push(key);
155
+ return;
156
+ }
157
+ if (['requiredBackgroundModes', 'embeddedAppIdList'].indexOf(key) > -1) {
158
+ // 提到appjson最上层处理
159
+ const preVal = appJson[key];
160
+ // eslint-disable-next-line
161
+ preVal ? appJson[key] = Array.from(new Set(preVal
162
+ // eslint-disable-next-line
163
+ .slice(0).concat(subp[key]))) : appJson[key] = subp[key].slice(0);
164
+ return;
165
+ }
166
+ });
167
+ if (invalidKeys.length) {
168
+ fail(`不支持分包${subp?.name}配置${invalidKeys.join(',')}\n`);
169
+ }
170
+ // eslint-disable-next-line
171
+ invalidKeys.concat(['requiredBackgroundModes', 'embeddedAppIdList']).forEach(k => delete subp[k]);
172
+ return subp;
173
+ });
174
+ // 如果plugins重复,则错误提示
175
+ const pluginsErrMsg = Object.keys(pluginsMap).map((pk) => {
176
+ if (pluginsMap[pk].length > 1) {
177
+ return `${pluginsMap[pk].join(',')}重复配置plugin(${pk});`;
178
+ }
179
+ return '';
180
+ })
181
+ .reduce((pre, cur) => pre + cur, '');
182
+ if (pluginsErrMsg) {
183
+ fail(`plugins配置出现错误:${pluginsErrMsg}`);
184
+ }
185
+ // eslint-disable-next-line
186
+ appJson.subpackages = subps;
187
+ };
188
+
189
+
190
+ /**
191
+ * 动态生成编译后的app.json
192
+ * @param {object} tmsConfig
193
+ * @param {array} modules
194
+ * @returns
195
+ */
196
+ function buildOutputAppJson(tmsConfig, modules, isDev) {
197
+ try {
198
+ // 获取当前 modules 下的所有子模块的配置内容
199
+ const modulesConfig = getLocalModuleConfig(modules, tmsConfig.appName, MODULE_CONFIG_FILENAME);
200
+ // 获取app.json的配置
201
+ const appJson = getAppJsonContent(resolve('./app.json'));
202
+ // 更新app.json中的subpackages
203
+ updateSubpackages(appJson, modulesConfig);
204
+ // 处理appJson中重复||冲突的地方
205
+ processAppJson(appJson);
206
+ // 更新主包,需在subpackages处理完成后执行, pages/
207
+ updateMainPackages(appJson, tmsConfig.mainPackages);
208
+
209
+ fs.writeFileSync(resolve(`${tmsConfig.outputDir}/app.json`), JSON.stringify(appJson, null, 2), 'utf8');
210
+
211
+ return appJson;
212
+ } catch (e) {
213
+ handleError(`生成app.json出现错误: ${e}`, isDev);
214
+ }
215
+ }
216
+
217
+ module.exports = {
218
+ setModuleConfig,
219
+ getValidModules,
220
+ buildOutputAppJson,
221
+ };
@@ -1,77 +1,77 @@
1
- const fs = require('fs');
2
- const semver = require('semver');
3
- const { resolve } = require('./widgets');
4
- const path = require('path');
5
- const { fail } = require('./log');
6
- const shelljs = require('shelljs');
7
-
8
- const getLatestVersion = (npmName) => {
9
- const data = shelljs.exec(`npm view ${npmName} version`);
10
- return data.stdout || '0.0.0';
11
- };
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
22
- const packageJsonName = 'package.json'; // 查找文件名
23
- // 1.1根目录的package.json
24
- const packageArr = [
25
- {
26
- srcPackageDir: path.join(cwd, packageJsonName),
27
- destNpmDir: resolve(outputDir, 'node_modules'),
28
- },
29
- ];
30
- // 1.2模块的package.json
31
- modules.forEach((item) => {
32
- const srcPackageDir = path.join(cwd, item.path, 'package.json');
33
- if (fs.existsSync(srcPackageDir)) {
34
- packageArr.push({
35
- srcPackageDir,
36
- destNpmDir: resolve(outputDir, item.root, 'node_modules'),
37
- });
38
- }
39
- });
40
-
41
- // 步骤2. 比较package.json的依赖与node_modules依赖的版本号
42
- for (const item of packageArr) {
43
- const packageJson = fs.readFileSync(item.srcPackageDir, 'utf-8');
44
- let dependencies;
45
- try {
46
- dependencies = JSON.parse(packageJson).dependencies;
47
- } catch (e) {
48
- fail(`解析${packageJson}报错,请检查是否是正确的json配置项`);
49
- process.exit(1);
50
- }
51
- const dependenciesKeys = Object.keys(dependencies);
52
- for (const key of dependenciesKeys) {
53
- const depPath = path.join(item.destNpmDir, key);
54
- if (!fs.existsSync(depPath)) {
55
- return true;
56
- }
57
- const depPackagePath = path.join(depPath, 'package.json');
58
- if (fs.existsSync(depPackagePath)) {
59
- const packageData = require(depPackagePath);
60
- if (dependencies[key] === 'latest') {
61
- dependencies[key] = getLatestVersion(key);
62
- }
63
- if (packageData.version === 'latest') {
64
- packageData.version = getLatestVersion(key);
65
- }
66
- if (semver.lt(packageData.version, semver.minVersion(dependencies[key]).version)) {
67
- return true;
68
- }
69
- }
70
- }
71
- }
72
- return false;
73
- };
74
-
75
- module.exports = {
76
- checkDependencies,
77
- };
1
+ const fs = require('fs');
2
+ const semver = require('semver');
3
+ const { resolve } = require('./widgets');
4
+ const path = require('path');
5
+ const shelljs = require('shelljs');
6
+ const { handleError } = require('./handleError');
7
+
8
+ const getLatestVersion = (npmName) => {
9
+ const data = shelljs.exec(`npm view ${npmName} version`);
10
+ return data.stdout || '0.0.0';
11
+ };
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, isDev) => {
21
+ // 步骤1. 收集package.json
22
+ const packageJsonName = 'package.json'; // 查找文件名
23
+ // 1.1根目录的package.json
24
+ const packageArr = [
25
+ {
26
+ srcPackageDir: path.join(cwd, packageJsonName),
27
+ destNpmDir: resolve(outputDir, 'node_modules'),
28
+ },
29
+ ];
30
+ // 1.2模块的package.json
31
+ modules.forEach((item) => {
32
+ const srcPackageDir = path.join(cwd, item.path, 'package.json');
33
+ if (fs.existsSync(srcPackageDir)) {
34
+ packageArr.push({
35
+ srcPackageDir,
36
+ destNpmDir: resolve(outputDir, item.root, 'node_modules'),
37
+ });
38
+ }
39
+ });
40
+
41
+ // 步骤2. 比较package.json的依赖与node_modules依赖的版本号
42
+ 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配置项`, isDev);
50
+ }
51
+ const dependenciesKeys = Object.keys(dependencies);
52
+ for (const key of dependenciesKeys) {
53
+ const depPath = path.join(item.destNpmDir, key);
54
+ if (!fs.existsSync(depPath)) {
55
+ return true;
56
+ }
57
+ const depPackagePath = path.join(depPath, 'package.json');
58
+ if (fs.existsSync(depPackagePath)) {
59
+ const packageData = require(depPackagePath);
60
+ if (dependencies[key] === 'latest') {
61
+ dependencies[key] = getLatestVersion(key);
62
+ }
63
+ if (packageData.version === 'latest') {
64
+ packageData.version = getLatestVersion(key);
65
+ }
66
+ if (semver.lt(packageData.version, semver.minVersion(dependencies[key]).version)) {
67
+ return true;
68
+ }
69
+ }
70
+ }
71
+ }
72
+ return false;
73
+ };
74
+
75
+ module.exports = {
76
+ checkDependencies,
77
+ };
@@ -1,35 +1,35 @@
1
- const fs = require('fs');
2
- const cp = require('child_process');
3
-
4
- const getDevtoolCliPath = async () => {
5
- let cliPath;
6
- if (process.platform === 'darwin') {
7
- cliPath = '/Applications/wechatwebdevtools.app/Contents/MacOS/cli';
8
- } else if (process.platform === 'win32') {
9
- cliPath = 'C:/Program Files (x86)/Tencent/微信web开发者工具/cli.bat';
10
- } else {
11
- // 其余平台暂不支持
12
- throw new Error('unsupported platform');
13
- }
14
- const exists = await new Promise(resolve => fs.exists(cliPath, resolve));
15
- if (!exists) {
16
- // 找不到开发工具
17
- throw new Error(`未找到微信小程序开发者工具,请确认是否安装,如未安装请前往
18
- https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 下载`);
19
- }
20
- return cliPath;
21
- };
22
-
23
- const openDevtool = async (path) => {
24
- const cliPath = await getDevtoolCliPath();
25
- const child = cp.spawn(cliPath, ['open', '--project', path], {
26
- detached: true,
27
- stdio: 'inherit',
28
- });
29
- child.unref();
30
- };
31
-
32
- module.exports = {
33
- getDevtoolCliPath,
34
- openDevtool,
35
- };
1
+ const fs = require('fs');
2
+ const cp = require('child_process');
3
+
4
+ const getDevtoolCliPath = async () => {
5
+ let cliPath;
6
+ if (process.platform === 'darwin') {
7
+ cliPath = '/Applications/wechatwebdevtools.app/Contents/MacOS/cli';
8
+ } else if (process.platform === 'win32') {
9
+ cliPath = 'C:/Program Files (x86)/Tencent/微信web开发者工具/cli.bat';
10
+ } else {
11
+ // 其余平台暂不支持
12
+ throw new Error('unsupported platform');
13
+ }
14
+ const exists = await new Promise(resolve => fs.exists(cliPath, resolve));
15
+ if (!exists) {
16
+ // 找不到开发工具
17
+ throw new Error(`未找到微信小程序开发者工具,请确认是否安装,如未安装请前往
18
+ https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 下载`);
19
+ }
20
+ return cliPath;
21
+ };
22
+
23
+ const openDevtool = async (path) => {
24
+ const cliPath = await getDevtoolCliPath();
25
+ const child = cp.spawn(cliPath, ['open', '--project', path], {
26
+ detached: true,
27
+ stdio: 'inherit',
28
+ });
29
+ child.unref();
30
+ };
31
+
32
+ module.exports = {
33
+ getDevtoolCliPath,
34
+ openDevtool,
35
+ };