@tmsfe/tmskit 0.0.15-beta.5 → 0.0.17

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.15-beta.5",
3
+ "version": "0.0.17",
4
4
  "description": "tmskit",
5
5
  "main": "dist/index.cjs",
6
6
  "bin": {
@@ -18,8 +18,7 @@
18
18
  "build": "rollup -c --environment TARGET:tmskit",
19
19
  "pub:patch": "sh build/publish.sh patch",
20
20
  "pub:minor": "sh build/publish.sh minor",
21
- "pub:major": "sh build/publish.sh major",
22
- "postinstall": "node build/postinstall.js"
21
+ "pub:major": "sh build/publish.sh major"
23
22
  },
24
23
  "author": "tms·web",
25
24
  "license": "ISC",
@@ -40,6 +39,15 @@
40
39
  "rollup-plugin-typescript2": "0.27.0"
41
40
  },
42
41
  "dependencies": {
42
+ "ansi-colors": "1.1.0",
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",
43
51
  "async": "^3.2.2",
44
52
  "chalk": "^4.1.0",
45
53
  "chokidar": "^3.5.3",
@@ -56,7 +64,7 @@
56
64
  "lodash": "^4.17.21",
57
65
  "metalsmith": "^2.3.0",
58
66
  "miniprogram-ci": "1.4.13",
59
- "moment": "^2.29.1",
67
+ "moment": "^2.29.2",
60
68
  "object-assign": "^4.0.1",
61
69
  "ora": "^5.1.0",
62
70
  "plugin-error": "^1.0.0",
@@ -1,6 +1,7 @@
1
1
  const path = require('path');
2
2
  const fs = require('fs');
3
3
  const ora = require('ora');
4
+ const chalk = require('chalk');
4
5
  const { parallel, series } = require('gulp');
5
6
  const { resolve, mergeMap } = require('../utils/widgets');
6
7
  const { buildOutputAppJson } = require('../core/buildAppJson');
@@ -9,7 +10,7 @@ const compile = require('./compile');
9
10
  const watch = require('./watch');
10
11
  const { info } = require('../utils/log');
11
12
 
12
- const watchEvents = ['add', 'change', 'unlink', 'unlinkDir'];
13
+ const watchEvents = ['add', 'change', 'unlink', 'addDir', 'unlinkDir'];
13
14
  function excludeGlob(glob) {
14
15
  const otherArr = new Set();
15
16
  otherArr.add('!**/*.{ttf,otf,woff,eot}');
@@ -37,17 +38,6 @@ function adaptPath(pathDir) {
37
38
  }
38
39
  module.exports = async (tmsConfig, newModules, isDev = true) => {
39
40
  const compileTasksMap = new Map();
40
- // 监听app.json
41
- if (isDev) {
42
- watch(
43
- [resolve('app.json')],
44
- { ignoreInitial: false, events: watchEvents },
45
- () => buildOutputAppJson(tmsConfig, newModules, isDev),
46
- { from: resolve(), to: resolve(tmsConfig.outputDir) },
47
- );
48
- } else {
49
- buildOutputAppJson(tmsConfig, newModules, isDev);
50
- }
51
41
 
52
42
  // 监听根目录的文件
53
43
  mergeMap(compileTasksMap, compile(tmsConfig, {
@@ -146,17 +136,27 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
146
136
  sTime = new Date().getTime();
147
137
  cb();
148
138
  }
149
- function end(cb) {
139
+ async function end(cb) {
150
140
  if (isDev) {
141
+ // 监听app.json
142
+ watch(
143
+ [resolve('app.json')],
144
+ { ignoreInitial: false, events: watchEvents },
145
+ () => buildOutputAppJson(tmsConfig, newModules, isDev),
146
+ { from: resolve(), to: resolve(tmsConfig.outputDir) },
147
+ );
148
+ // 监听其他文件
151
149
  compileTasksMap.forEach(({ taskFn, module }, globValue) => {
152
150
  watch(globValue, { ignoreInitial: true, events: watchEvents }, taskFn, module);
153
151
  });
152
+ } else {
153
+ buildOutputAppJson(tmsConfig, newModules, isDev);
154
154
  }
155
155
  eTime = new Date().getTime() - sTime;
156
156
  if (typeof tmsConfig?.hooks?.afterCompile === 'function') {
157
- tmsConfig?.hooks?.afterCompile({ isDev, tmsConfig, modules: newModules });
157
+ await tmsConfig?.hooks?.afterCompile({ isDev, tmsConfig, modules: newModules });
158
158
  }
159
- spinner.succeed(`首次编译完成, 耗时${eTime / 1000}s, 微信开发者工具打开项目即可预览。`);
159
+ spinner.succeed(chalk.green(`首次编译完成, 耗时${eTime / 1000}s, 微信开发者工具打开项目即可预览。`));
160
160
  spinner.stop();
161
161
  cb();
162
162
  }
@@ -165,5 +165,6 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
165
165
  compileTasksMap.forEach(({ taskFn }) => {
166
166
  compileTasks.push(taskFn);
167
167
  });
168
+ // 一次性完成编译任务(编译完成后再添加watch任务-封装到end函数里面)
168
169
  series(start, parallel(...compileTasks), end)();
169
170
  };
@@ -0,0 +1,172 @@
1
+
2
+ /* eslint-disable */
3
+ // 该文件源于npm包 gulp-watch 但内部有bug, 故源码进行单独修改
4
+ const assign = require('object-assign');
5
+ const path = require('path');
6
+ const PluginError = require('plugin-error');
7
+ const fancyLog = require('fancy-log');
8
+ const colors = require('ansi-colors');
9
+ const chokidar = require('chokidar');
10
+ const { Duplex } = require('readable-stream');
11
+ const vinyl = require('vinyl-file');
12
+ const File = require('vinyl');
13
+ const anymatch = require('anymatch');
14
+ const pathIsAbsolute = require('path-is-absolute');
15
+ const globParent = require('glob-parent');
16
+ const slash = require('slash');
17
+
18
+ function normalizeGlobs(globs) {
19
+ if (!globs) {
20
+ throw new PluginError('gulp-watch', 'glob argument required');
21
+ }
22
+
23
+ if (typeof globs === 'string') {
24
+ globs = [globs];
25
+ }
26
+
27
+ if (!Array.isArray(globs)) {
28
+ throw new PluginError('gulp-watch', `glob should be String or Array, not ${typeof globs}`);
29
+ }
30
+
31
+ return globs;
32
+ }
33
+
34
+ function watch(globs, opts, cb) {
35
+ const originalGlobs = globs;
36
+ globs = normalizeGlobs(globs);
37
+
38
+ if (typeof opts === 'function') {
39
+ cb = opts;
40
+ opts = {};
41
+ }
42
+
43
+ opts = assign({}, watch._defaultOptions, opts);
44
+ cb = cb || function () {};
45
+
46
+ function resolveFilepath(filepath) {
47
+ if (pathIsAbsolute(filepath)) {
48
+ return path.normalize(filepath);
49
+ }
50
+ return path.resolve(opts.cwd || process.cwd(), filepath);
51
+ }
52
+
53
+ function resolveGlob(glob) {
54
+ let mod = '';
55
+
56
+ if (glob[0] === '!') {
57
+ mod = glob[0];
58
+ glob = glob.slice(1);
59
+ }
60
+
61
+ return mod + slash(resolveFilepath(glob));
62
+ }
63
+ globs = globs.map(resolveGlob);
64
+
65
+ const baseForced = Boolean(opts.base);
66
+ const outputStream = new Duplex({ objectMode: true, allowHalfOpen: true });
67
+
68
+ outputStream._write = function _write(file, enc, done) {
69
+ cb(file);
70
+ this.push(file);
71
+ done();
72
+ };
73
+
74
+ outputStream._read = function _read() { };
75
+
76
+ const watcher = chokidar.watch(globs, opts);
77
+
78
+ opts.events.forEach((ev) => {
79
+ watcher.on(ev, processEvent.bind(undefined, ev));
80
+ });
81
+
82
+ ['add', 'change', 'unlink', 'addDir', 'unlinkDir', 'error', 'ready', 'raw']
83
+ .forEach((ev) => {
84
+ watcher.on(ev, outputStream.emit.bind(outputStream, ev));
85
+ });
86
+
87
+ outputStream.add = function add(newGlobs) {
88
+ newGlobs = normalizeGlobs(newGlobs)
89
+ .map(resolveGlob);
90
+ watcher.add(newGlobs);
91
+ globs.push.apply(globs, newGlobs);
92
+ };
93
+ outputStream.unwatch = watcher.unwatch.bind(watcher);
94
+ outputStream.close = function () {
95
+ watcher.close();
96
+ this.emit('end');
97
+ };
98
+
99
+ function processEvent(event, filepath) {
100
+ filepath = resolveFilepath(filepath);
101
+ const fileOpts = assign({}, opts);
102
+
103
+ let glob;
104
+ let currentFilepath = filepath;
105
+ while (!(glob = globs[anymatch(globs, currentFilepath, true)]) && currentFilepath !== (currentFilepath = path.dirname(currentFilepath))) {} // eslint-disable-line no-empty-blocks/no-empty-blocks
106
+
107
+ if (!glob) {
108
+ console.error('[gulp-watch]没有匹配到glob')
109
+ return;
110
+ }
111
+
112
+ if (!baseForced) {
113
+ fileOpts.base = path.normalize(globParent(glob));
114
+ }
115
+
116
+ // Do not stat deleted files
117
+ if (event === 'unlink' || event === 'unlinkDir' || event === 'addDir') {
118
+ fileOpts.path = filepath;
119
+
120
+ write(event, null, new File(fileOpts));
121
+ return;
122
+ }
123
+
124
+ // Workaround for early read
125
+ setTimeout(() => {
126
+ vinyl.read(filepath, fileOpts).then((file) => {
127
+ write(event, null, file);
128
+ });
129
+ }, opts.readDelay);
130
+ }
131
+
132
+ function write(event, err, file) {
133
+ if (err) {
134
+ outputStream.emit('error', err);
135
+ return;
136
+ }
137
+
138
+ if (opts.verbose) {
139
+ log(event, file);
140
+ }
141
+
142
+ file.event = event;
143
+ outputStream.push(file);
144
+ cb(file);
145
+ }
146
+
147
+ function log(event, file) {
148
+ event = event[event.length - 1] === 'e' ? `${event}d` : `${event}ed`;
149
+
150
+ const msg = [colors.magenta(file.relative), 'was', event];
151
+
152
+ if (opts.name) {
153
+ msg.unshift(`${colors.cyan(opts.name)} saw`);
154
+ }
155
+
156
+ fancyLog.info.apply(null, msg);
157
+ }
158
+
159
+ return outputStream;
160
+ }
161
+
162
+ // This is not part of the public API as that would lead to global state (singleton) pollution,
163
+ // and allow unexpected interference between unrelated modules that make use of gulp-watch.
164
+ // This can be useful for unit tests and root application configuration, though.
165
+ // Avoid modifying gulp-watch's default options inside a library/reusable package, please.
166
+ watch._defaultOptions = {
167
+ events: ['add', 'change', 'unlink'],
168
+ ignoreInitial: true,
169
+ readDelay: 10,
170
+ };
171
+
172
+ module.exports = watch;
@@ -49,7 +49,7 @@ function mpJsonDep(
49
49
  }
50
50
 
51
51
  // eslint-disable-next-line
52
- const reg = new RegExp(`^(\./\.\.\/)+.*\/${includeName}\/(.*)`);
52
+ const reg = new RegExp(`^(\./|\.\.\/)+.*\/${includeName}\/(.*)`);
53
53
  const regRes = componentPath.match(reg) || [];
54
54
  if (regRes[2]) {
55
55
  const depDestPath = resolve(tmsConfig.outputDir, module.to, includeName, regRes[2]);
@@ -1,9 +1,10 @@
1
- const watch = require('gulp-watch');
1
+ const watch = require('./plugins/gulp-watch');
2
2
  const path = require('path');
3
3
  const shellJs = require('shelljs');
4
4
  const { info, warn } = require('../utils/log');
5
5
  const { resolve } = require('../utils/widgets');
6
6
  const { global } = require('../utils/global');
7
+ const { findAllFilesOfDir } = require('../utils/io');
7
8
 
8
9
  const TIP_MAP = {
9
10
  'package.json': '若依赖有变动,请重新执行tmskit run dev',
@@ -21,6 +22,12 @@ const logTip = (fileName, tipMap) => {
21
22
  info(`${fileName}有更新`);
22
23
  };
23
24
 
25
+ const getTargetFile = (sourceFile, module, outputDir) => {
26
+ const sourceFileRelativeModule = path.relative(resolve(module.from), sourceFile);
27
+ const targetFile = resolve(outputDir, module.to, sourceFileRelativeModule);
28
+ return targetFile;
29
+ };
30
+
24
31
  module.exports = function (globValue, watchOptions, callback, module) {
25
32
  watch(globValue, {
26
33
  // readDelay: 100,
@@ -31,9 +38,21 @@ module.exports = function (globValue, watchOptions, callback, module) {
31
38
  const sourceFileName = sourceFileDirArr.slice(sourceFileDirArr.length - 2).join('/');
32
39
 
33
40
  const tmsConfig = global.getData('tmsConfig');
34
- const sourceFileRelativeModule = path.relative(resolve(module.from), sourceFile);
35
- const targetFile = resolve(tmsConfig.outputDir, module.to, sourceFileRelativeModule);
36
41
 
42
+ if (vinyl.event === 'addDir') {
43
+ info(`更新${sourceFileName}目录`);
44
+ const files = findAllFilesOfDir(sourceFile);
45
+ for (const file of files) {
46
+ const fileDirArr = file.replace(/\\/g, '/').split('/');
47
+ const fileName = fileDirArr.slice(fileDirArr.length - 2).join('/');
48
+ const targetFile = getTargetFile(file, module, tmsConfig.outputDir);
49
+ logTip(fileName, TIP_MAP);
50
+ callback([file], path.dirname(targetFile));
51
+ }
52
+ return;
53
+ }
54
+
55
+ const targetFile = getTargetFile(sourceFile, module, tmsConfig.outputDir);
37
56
  if (vinyl.event === 'unlink' || vinyl.event === 'unlinkDir') {
38
57
  info(`删除${sourceFileName}`);
39
58
  shellJs.rm('-rf', targetFile);
@@ -1,12 +1,14 @@
1
1
  /**
2
2
  * 生成编译后的app.json
3
3
  */
4
+ /* eslint-disable no-param-reassign */
4
5
  const fs = require('fs');
5
6
  const { MODULE_CONFIG_FILENAME, MODULE_CONFIG_INVALID_KEY } = require('../config/constant');
6
- const { getModuleConfig, getValidModules } = require('./tmsMpconfig');
7
+ const { getModuleConfig } = require('./tmsMpconfig');
7
8
  const { fail } = require('../utils/log');
8
9
  const { resolve, isObject } = require('../utils/widgets');
9
10
  const { handleError } = require('./handleError');
11
+ const { global } = require('../utils/global');
10
12
 
11
13
  /**
12
14
  * 更新appJson里面的主包配置
@@ -36,7 +38,6 @@ function updateMainPackages(appJson, mainPackages = []) {
36
38
 
37
39
  // 去掉 subpackages 中的主包配置
38
40
  const foundMainPackageNames = foundMainPackages.map(item => item.name);
39
- // eslint-disable-next-line
40
41
  appJson.subpackages = appJson.subpackages.filter(subpackage => !foundMainPackageNames.includes(subpackage.name));
41
42
 
42
43
  return appJson;
@@ -62,17 +63,15 @@ const getAppJsonContent = (sourceAppJsonPath) => {
62
63
  /**
63
64
  * 更新app.json中的subpackages
64
65
  * @param {Object} appJson
65
- * @param {Object} modulesConfig
66
+ * @param {Object} modulesConfigs
66
67
  */
67
- const updateSubpackages = (appJson, modulesConfig) => {
68
+ const updateSubpackages = (appJson, modulesConfigs) => {
68
69
  // eslint-disable-next-line
69
- for (const name in modulesConfig) {
70
- const moduleInfo = isObject(modulesConfig[name]) ? [modulesConfig[name]] : modulesConfig[name];
71
- // 过滤 pages 为空的情况
72
- const validModules = getValidModules(moduleInfo);
73
- // eslint-disable-next-line
74
- appJson.subpackages = appJson.subpackages.concat(validModules);
70
+ for (const modulePath in modulesConfigs) {
71
+ const moduleInfo = isObject(modulesConfigs[modulePath]) ? [modulesConfigs[modulePath]] : modulesConfigs[modulePath];
72
+ appJson.subpackages = appJson.subpackages.concat(moduleInfo);
75
73
  }
74
+ appJson.subpackages.sort((item1, item2) => item1.name.localeCompare(item2.name));
76
75
  };
77
76
 
78
77
  /**
@@ -105,9 +104,7 @@ const fixAppJson = (appJson) => {
105
104
  if (['requiredBackgroundModes', 'embeddedAppIdList'].indexOf(key) > -1) {
106
105
  // 提到appjson最上层处理
107
106
  const preVal = appJson[key];
108
- // eslint-disable-next-line
109
107
  preVal ? appJson[key] = Array.from(new Set(preVal
110
- // eslint-disable-next-line
111
108
  .slice(0).concat(subp[key]))) : appJson[key] = subp[key].slice(0);
112
109
  return;
113
110
  }
@@ -115,7 +112,6 @@ const fixAppJson = (appJson) => {
115
112
  if (invalidKeys.length) {
116
113
  fail(`不支持分包${subp?.name}配置${invalidKeys.join(',')}\n`);
117
114
  }
118
- // eslint-disable-next-line
119
115
  invalidKeys.concat(['requiredBackgroundModes', 'embeddedAppIdList']).forEach(k => delete subp[k]);
120
116
  return subp;
121
117
  });
@@ -130,7 +126,6 @@ const fixAppJson = (appJson) => {
130
126
  if (pluginsErrMsg) {
131
127
  fail(`plugins配置出现错误:${pluginsErrMsg}`);
132
128
  }
133
- // eslint-disable-next-line
134
129
  appJson.subpackages = subps;
135
130
  };
136
131
 
@@ -143,11 +138,11 @@ const fixAppJson = (appJson) => {
143
138
  function buildOutputAppJson(tmsConfig, modules) {
144
139
  try {
145
140
  // 获取当前 modules 下的所有子模块的配置内容
146
- const modulesConfig = getModuleConfig(modules, tmsConfig.appName, MODULE_CONFIG_FILENAME);
141
+ const modulesConfigs = getModuleConfig(modules, tmsConfig.appName, MODULE_CONFIG_FILENAME);
147
142
  // 获取app.json的配置
148
143
  const appJson = getAppJsonContent(resolve('./app.json'));
149
144
  // 更新app.json中的subpackages
150
- updateSubpackages(appJson, modulesConfig);
145
+ updateSubpackages(appJson, modulesConfigs);
151
146
  // 处理appJson中重复||冲突的地方
152
147
  fixAppJson(appJson);
153
148
  // 更新主包,需在subpackages处理完成后执行, pages/
@@ -155,7 +150,9 @@ function buildOutputAppJson(tmsConfig, modules) {
155
150
 
156
151
  fs.writeFileSync(resolve(`${tmsConfig.outputDir}/app.json`), JSON.stringify(appJson, null, 2), 'utf8');
157
152
  if (typeof tmsConfig?.hooks?.updateAppJson === 'function') {
158
- tmsConfig?.hooks?.updateAppJson(appJson);
153
+ tmsConfig?.hooks?.updateAppJson({
154
+ tmsConfig, module, appJson, isDev: global.getData('isDev'),
155
+ });
159
156
  }
160
157
 
161
158
  return appJson;
@@ -93,40 +93,38 @@ const checkModules = function (tmsConfig, modules, isQuit = false) {
93
93
  return targetModules;
94
94
  };
95
95
 
96
- /**
97
- * 过滤页面为空的分包
98
- * @param {Array} moduleCfg 模块配置内容
99
- * @returns pages不为空的分包
100
- */
101
- const getValidModules = (moduleCfg) => {
102
- // 过滤 pages 为空的情况
103
- const validModules = moduleCfg.filter(item => item.pages.length > 0);
104
- return validModules;
105
- };
106
-
107
96
  /**
108
97
  * 适配处理module.config.json的字段
109
98
  * @param { object } fileContent module.config.json的内容
110
99
  * @param { string } appName 小程序的名称
111
100
  */
112
101
  function adaptMpCgContent(fileContent, appName) {
113
- const content = fileContent.contents ? JSON.parse(fileContent.contents.toString()) : JSON.parse(fileContent);
102
+ const handleContent = function (appName, current) {
103
+ let res = current;
104
+ if (appName && current.mpConfig && current.mpConfig[appName]) {
105
+ res = { ...current, ...current.mpConfig[appName] };
106
+ }
107
+
108
+ delete res.mpConfig;
109
+ delete res.isSubpackages;
110
+ return res;
111
+ };
112
+
113
+ let content = JSON.parse(fileContent);
114
114
 
115
115
  if (isArray(content)) {
116
116
  let i = content.length - 1;
117
117
  while (i >= 0) {
118
- let current = content[i];
119
-
120
- if (appName && current.mpConfig && current.mpConfig[appName]) {
121
- current = { ...current, ...current.mpConfig[appName] };
122
- }
123
-
124
- delete current.mpConfig;
125
- delete current.isSubpackages;
126
-
127
- content[i] = current;
118
+ content[i] = handleContent(appName, content[i]);
128
119
  i--; // eslint-disable-line
129
120
  }
121
+ } else {
122
+ if (appName && content.mpConfig && content.mpConfig[appName]) {
123
+ content = { ...content, ...content.mpConfig[appName] };
124
+ delete content.mpConfig;
125
+ delete content.isSubpackages;
126
+ }
127
+ content = handleContent(appName, content);
130
128
  }
131
129
  return content;
132
130
  }
@@ -168,7 +166,7 @@ const tmsModulesMergeLocalModuleCfg = (modules, appName) => {
168
166
  let moduleConfigContent = fs.readFileSync(moduleConfigPath, 'utf-8');
169
167
  moduleConfigContent = adaptMpCgContent(moduleConfigContent, appName);
170
168
  const moduleContentArr = isObject(moduleConfigContent) ? [moduleConfigContent] : moduleConfigContent;
171
- getValidModules(moduleContentArr).forEach(({ name }, moduleContentArrIndex) => {
169
+ moduleContentArr.forEach(({ name }, moduleContentArrIndex) => {
172
170
  if (name === moduleName) {
173
171
  findModule = true;
174
172
  newModules.push({
@@ -178,7 +176,7 @@ const tmsModulesMergeLocalModuleCfg = (modules, appName) => {
178
176
  }
179
177
  });
180
178
  if (!findModule) {
181
- fail(`启动模块${moduleName}在${moduleConfigPath}没有找到或pages为空,请检查配置`);
179
+ fail(`启动模块${moduleName}在${moduleConfigPath}没有找到,请检查配置`);
182
180
  process.exit(1);
183
181
  }
184
182
  } catch (e) {
@@ -232,7 +230,6 @@ module.exports = {
232
230
  readTmsConfig,
233
231
  readTmsPrivateCf,
234
232
  getModuleConfig,
235
- getValidModules,
236
233
  checkModules,
237
234
  tmsModulesMergeLocalModuleCfg,
238
235
  subModulesMergeDepModules,
package/src/entry.js CHANGED
@@ -34,6 +34,17 @@ module.exports = [
34
34
  require('./scripts/run/index')('dev', cmd);
35
35
  },
36
36
  },
37
+ {
38
+ command: 'cloud',
39
+ description: '云函数开发',
40
+ options: [
41
+ ['-m, --module [moduleName]', '模块名称'],
42
+ ['-e, --env [env]', '环境变量'],
43
+ ],
44
+ action: (cmd) => {
45
+ require('./scripts/run/index')('cloud', cmd);
46
+ },
47
+ },
37
48
  {
38
49
  command: 'build',
39
50
  description: 'prod 打包编译',
@@ -9,6 +9,9 @@ async function build(tmsConfig, targetModules, env) {
9
9
 
10
10
  const { targetModules: newModules } = await init(tmsConfig, targetModules);
11
11
 
12
+ if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
13
+ await tmsConfig?.hooks?.beforeCompile({ isDev: false, tmsConfig, modules: newModules });
14
+ };
12
15
  compileBuild(tmsConfig, newModules, env);
13
16
  }
14
17
 
@@ -0,0 +1,12 @@
1
+ const { symLink } = require('../../../core/symbolicLink');
2
+ const { handleError } = require('../../../core/handleError');
3
+ const { succeed } = require('../../../utils/log');
4
+
5
+ module.exports = async (tmsConfig) => {
6
+ try {
7
+ await symLink(tmsConfig);
8
+ succeed('云函数创建软链成功');
9
+ } catch (e) {
10
+ handleError(`创建软链错误: ${e}`);
11
+ }
12
+ };
@@ -7,6 +7,7 @@ const { info } = require('../../../utils/log');
7
7
  const { global } = require('../../../utils/global');
8
8
  const { CACHE_DIR } = require('../../../config/constant');
9
9
 
10
+
10
11
  // 用户编译分包时,需要将dist中其他分包(主包不能删除)的内容删除,否则其他分包的内容混入到主包(导致主包的体积超2M)
11
12
  function delOtherModule(tmsConfig, targetModules) {
12
13
  const modules = tmsModulesMergeLocalModuleCfg(tmsConfig.modules, tmsConfig.appName);
@@ -25,7 +26,7 @@ async function dev(tmsConfig, targetModules, env) {
25
26
  let newModules = targetModules;
26
27
  const { noCache } = global.getData('cmd');
27
28
  if (noCache) {
28
- shelljs.rm('-rf', resolve('dist'));
29
+ shelljs.rm('-rf', resolve(tmsConfig.outputDir));
29
30
  shelljs.rm('-rf', CACHE_DIR);
30
31
  }
31
32
 
@@ -35,7 +36,7 @@ async function dev(tmsConfig, targetModules, env) {
35
36
 
36
37
  info('当前dev启动的有效模块', newModules.map(item => item.name).sort());
37
38
  if (typeof tmsConfig?.hooks?.beforeCompile === 'function') {
38
- tmsConfig?.hooks?.beforeCompile({ isDev: true, tmsConfig, modules: newModules });
39
+ await tmsConfig?.hooks?.beforeCompile({ isDev: true, tmsConfig, modules: newModules });
39
40
  };
40
41
  delOtherModule(tmsConfig, newModules);
41
42
  compileDev(tmsConfig, newModules, env);
@@ -2,6 +2,7 @@ const init = require('./init/index');
2
2
  const dev = require('./dev/index');
3
3
  const build = require('./build/index');
4
4
  const install = require('./install/index');
5
+ const cloud = require('./cloud/index');
5
6
  const { global } = require('../../utils/global');
6
7
  const {
7
8
  readTmsConfig,
@@ -87,6 +88,9 @@ async function run(commandName, cmd) {
87
88
  global.setData('isDev', true);
88
89
  dev(tmsConfig, newModules, env);
89
90
  return;
91
+ case 'cloud':
92
+ cloud(tmsConfig, env);
93
+ return;
90
94
  case 'install':
91
95
  install(tmsConfig, newModules, env);
92
96
  return;
@@ -17,7 +17,7 @@ const { checkDependencies } = require('../../../core/checkDependencies');
17
17
  * @param { array } defaultFiles 默认需要拷贝的配置项
18
18
  * @returns
19
19
  */
20
- const cpFilesToOutput = function (tmsConfig, targetModules, defaultFiles) {
20
+ const cpFilesToOutput = function (tmsConfig, defaultFiles) {
21
21
  const outputDir = resolve(tmsConfig.outputDir);
22
22
  io.ensureDirExist(outputDir);
23
23
  defaultFiles.forEach((item) => {
@@ -25,18 +25,6 @@ const cpFilesToOutput = function (tmsConfig, targetModules, defaultFiles) {
25
25
  shelljs.cp('-rf', resolve(item), resolve(tmsConfig.outputDir, item));
26
26
  }
27
27
  });
28
-
29
- // 拷贝模块的package.json到编译输出目录
30
- targetModules.forEach((item) => {
31
- const outputModuleDir = resolve(`${tmsConfig.outputDir}/${item.root}`);
32
- if (!fs.existsSync(resolve(item.path))) {
33
- fail(`${item.path}模块代码路径不存在, 请检查tms.config.js的${item.name}模块的path`);
34
- process.exit(1);
35
- }
36
- io.ensureDirExist(outputModuleDir);
37
- const modulePackagePath = resolve(item.path, 'package.json');
38
- if (fs.existsSync(modulePackagePath)) shelljs.cp('-Rf', modulePackagePath, outputModuleDir);
39
- });
40
28
  };
41
29
 
42
30
  /**
@@ -78,7 +66,7 @@ async function task(tmsConfig, targetModules) {
78
66
  cpFilesToOutput,
79
67
  '开始拷贝文件到编译输出目录',
80
68
  '拷贝文件到编译输出目录完成',
81
- )(tmsConfig, newModules, DEFAULT_COPY_CONFIG);
69
+ )(tmsConfig, DEFAULT_COPY_CONFIG);
82
70
 
83
71
  // install
84
72
  if (checkDependencies(newModules, resolve('./'), tmsConfig.outputDir)) {
@@ -1,10 +1,26 @@
1
+ const shelljs = require('shelljs');
2
+ const fs = require('fs');
3
+ const io = require('../../../utils/io');
1
4
  const { createTask, resolve } = require('../../../utils/widgets');
2
5
  const { buildMpNpm } = require('../../../core/mpCi');
3
6
  const { CACHE_DIR } = require('../../../config/constant');
4
7
  const { mpNpmInstallAll } = require('../../../core/npm');
5
8
  const { global } = require('../../../utils/global');
9
+ const { fail } = require('../../../utils/log');
6
10
 
7
11
  async function install(tmsConfig, modules) {
12
+ // 拷贝模块的package.json到编译输出目录
13
+ modules.forEach((item) => {
14
+ const outputModuleDir = resolve(`${tmsConfig.outputDir}/${item.root}`);
15
+ if (!fs.existsSync(resolve(item.path))) {
16
+ fail(`${item.path}模块代码路径不存在, 请检查tms.config.js的${item.name}模块的path`);
17
+ process.exit(1);
18
+ }
19
+ io.ensureDirExist(outputModuleDir);
20
+ const modulePackagePath = resolve(item.path, 'package.json');
21
+ if (fs.existsSync(modulePackagePath)) shelljs.cp('-Rf', modulePackagePath, outputModuleDir);
22
+ });
23
+
8
24
  // 小程序npm install
9
25
  await createTask(
10
26
  mpNpmInstallAll,