@tmsfe/tmskit 0.0.19 → 0.0.22

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.19",
3
+ "version": "0.0.22",
4
4
  "description": "tmskit",
5
5
  "main": "dist/index.cjs",
6
6
  "bin": {
@@ -30,50 +30,49 @@
30
30
  "devDependencies": {
31
31
  "@rollup/plugin-babel": "^5.0.2",
32
32
  "@rollup/plugin-commonjs": "^19.0.0",
33
- "@rollup/plugin-dynamic-import-vars": "^1.1.1",
34
33
  "@rollup/plugin-json": "^4.0.3",
35
- "rollup": "^2.6.1",
36
- "rollup-plugin-node-resolve": "^5.2.0",
37
- "rollup-plugin-replace": "^2.2.0",
38
- "rollup-plugin-terser": "^6.1.0",
39
- "rollup-plugin-typescript2": "0.27.0"
34
+ "rollup": "^2.6.1"
40
35
  },
41
36
  "dependencies": {
42
37
  "ansi-colors": "1.1.0",
43
38
  "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
39
  "async": "^3.2.2",
52
40
  "chalk": "^4.1.0",
53
41
  "chokidar": "^3.5.3",
54
42
  "commander": "^8.3.0",
55
43
  "copy-webpack-plugin": "^9.1.0",
56
44
  "ejs": "^3.1.5",
45
+ "fancy-log": "1.3.2",
57
46
  "fs-extra": "^10.0.1",
58
47
  "glob-ignore": "^1.0.2",
48
+ "glob-parent": "^3.0.1",
59
49
  "gulp": "^4.0.2",
50
+ "gulp-if": "^3.0.0",
60
51
  "gulp-watch": "^5.0.1",
61
52
  "htmlparser2": "^7.2.0",
62
53
  "inquirer": "^7.3.3",
63
54
  "leven": "3.1.0",
64
55
  "lodash": "^4.17.21",
65
56
  "metalsmith": "^2.3.0",
57
+ "minimatch": "^5.1.0",
66
58
  "miniprogram-ci": "1.4.13",
67
59
  "moment": "^2.29.2",
68
60
  "object-assign": "^4.0.1",
69
61
  "ora": "^5.1.0",
62
+ "path-is-absolute": "^1.0.1",
70
63
  "plugin-error": "^1.0.0",
71
64
  "postcss": "^8.4.6",
72
65
  "precinct": "^8.3.1",
66
+ "readable-stream": "^2.2.2",
73
67
  "replace-ext": "^2.0.0",
68
+ "request": "^2.88.2",
74
69
  "shelljs": "^0.8.4",
70
+ "slash": "^1.0.0",
75
71
  "strip-comments": "^2.0.1",
76
72
  "through2": "^4.0.2",
73
+ "unzipper": "^0.10.11",
74
+ "vinyl": "^2.1.0",
75
+ "vinyl-file": "^2.0.0",
77
76
  "vinyl-sourcemaps-apply": "^0.2.0"
78
77
  },
79
78
  "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
  };
@@ -1,16 +1,41 @@
1
1
  const { src, dest } = require('gulp');
2
- // const px2rpx = require('gulp-px-to-rpx');
3
- // const cache = require('gulp-cache');
4
- // const image = require('gulp-image');
5
- // const replaceEnv = require('./plugins/replaceEnv');
6
- const { mpCommonDep } = require('./plugins/mpCommonDep');
7
- const { mpJsonDep } = require('./plugins/mpJsonDep');
8
- const { mpWxmlDep } = require('./plugins/mpWxmlDep');
9
- // const postcss = require('gulp-postcss');
10
- // const base64 = require('./plugins/postcss-font-base64');
2
+ const path = require('path');
3
+ const minimatch = require('minimatch');
4
+ const gulpif = require('gulp-if');
5
+ const mpProjectJson = require('./plugins/mpProjectJson');
6
+ const { resolve } = require('../utils/widgets');
7
+ const through = require('through2');
11
8
  const { fail } = require('../utils/log');
12
9
 
13
- // const since = task => file => (lastRun(task) > file.stat.ctime ? lastRun(task) : 0);
10
+ const getTargetFile = (sourceFile, module, outputDir) => {
11
+ const sourceFileRelativeModule = path.relative(resolve(module.from), sourceFile);
12
+ const targetFile = resolve(outputDir, module.to, sourceFileRelativeModule);
13
+ return targetFile;
14
+ };
15
+
16
+ const addPlugins = function (tmsConfig, srcPipe, pluginParams) {
17
+ const { plugins = [] } = tmsConfig;
18
+ const newPlugins = [{
19
+ glob: '**/project.config.json',
20
+ action: mpProjectJson(),
21
+ }, ...plugins];
22
+ return newPlugins.reduce((srcPipe, pluginItem) => srcPipe.pipe(gulpif(
23
+ file => minimatch(file.path, pluginItem.glob),
24
+ through.obj(function (file, enc, cb) {
25
+ const sourceFile = file.history[0];
26
+ pluginItem.action({
27
+ tmsConfig,
28
+ sourceFile,
29
+ file,
30
+ targetFile: getTargetFile(sourceFile, pluginParams.module, tmsConfig.outputDir),
31
+ isDev: pluginParams.isDev,
32
+ cb,
33
+ });
34
+ this.push(file);
35
+ }),
36
+ )), srcPipe);
37
+ };
38
+
14
39
  module.exports = function (
15
40
  tmsConfig,
16
41
  {
@@ -22,150 +47,23 @@ module.exports = function (
22
47
  },
23
48
  ) {
24
49
  const compileTasksMap = new Map();
25
- Object.keys(glob).forEach((globKey) => {
26
- const globValue = glob[globKey];
27
-
28
- switch (globKey) {
29
- case 'js':
30
- compileTasksMap.set(
31
- globValue,
32
- {
33
- module,
34
- taskFn: (sourceFile, targetPath) => {
35
- const newGlobValue = Array.isArray(sourceFile) ? sourceFile : globValue;
36
- const newDestPath = targetPath ? targetPath : destPath;
37
- const srcPipe = src(newGlobValue, { ...srcOption });
38
- return srcPipe
39
- // .pipe(replaceEnv(/process\.env(\.(\w*))?/g, tmsConfig.envData))
40
- .pipe(mpCommonDep(tmsConfig, module, ['.js', '.ts', '.wxs', '.json'], isDev))
41
- .on('error', (err) => {
42
- fail(`mpCommonDep编译报错${err}`);
43
- })
44
- .pipe(dest(newDestPath, { overwrite: true }))
45
- .on('error', (err) => {
46
- fail(`js编译报错${err}`);
47
- });
48
- },
49
- },
50
- );
51
- break;
52
- case 'wxss':
53
- compileTasksMap.set(
54
- globValue,
55
- {
56
- module,
57
- taskFn: (sourceFile, targetPath) => {
58
- const newGlobValue = Array.isArray(sourceFile) ? sourceFile : globValue;
59
- const newDestPath = targetPath ? targetPath : destPath;
60
- const srcPipe = src(newGlobValue, { ...srcOption });
61
- return srcPipe
62
- .pipe(mpCommonDep(tmsConfig, module, ['.wxss', '.less'], isDev))
63
- .on('error', (err) => {
64
- fail(`mpCommonDep编译报错${err}`);
65
- })
66
- // .pipe(postcss([base64()]))
67
- // .on('error', (err) => {
68
- // fail(`postcss编译报错${err}`);
69
- // })
70
- // .pipe(px2rpx({
71
- // designWidth: 375, // 设计稿宽度,默认为750
72
- // precision: 2, // 小数最大精度,默认为6
73
- // }))
74
- .pipe(dest(newDestPath))
75
- .on('error', (err) => {
76
- fail(`wxss编译报错${err}`);
77
- });
78
- },
79
- },
80
- );
81
- break;
82
- case 'json':
83
- compileTasksMap.set(
84
- globValue,
85
- {
86
- module,
87
- taskFn: (sourceFile, targetPath) => {
88
- const newGlobValue = Array.isArray(sourceFile) ? sourceFile : globValue;
89
- const newDestPath = targetPath ? targetPath : destPath;
90
- const srcPipe = src(newGlobValue, { ...srcOption });
91
- return srcPipe
92
- .pipe(mpJsonDep(tmsConfig, module, ['.json'], ['.wxml', '.json', '.js', '.ts', '.wxss', '.less'], isDev))
93
- .on('error', (err) => {
94
- fail(`mpJsonDep编译报错${err}`);
95
- })
96
- .pipe(dest(newDestPath))
97
- .on('error', (err) => {
98
- fail(`json编译报错${err}`);
99
- });
100
- },
101
- },
102
- );
103
- break;
104
- case 'wxml':
105
- compileTasksMap.set(
106
- globValue,
107
- {
108
- module,
109
- taskFn: (sourceFile, targetPath) => {
110
- const newGlobValue = Array.isArray(sourceFile) ? sourceFile : globValue;
111
- const newDestPath = targetPath ? targetPath : destPath;
112
- const srcPipe = src(newGlobValue, { ...srcOption });
113
- return srcPipe
114
- .pipe(mpWxmlDep(tmsConfig, module, isDev))
115
- .on('error', (err) => {
116
- fail(`mpWxmlDep编译报错${err}`);
117
- })
118
- .pipe(dest(newDestPath))
119
- .on('error', (err) => {
120
- fail(`wxml编译报错${err}`);
121
- });
122
- },
123
- },
124
- );
125
- break;
126
- case 'image':
127
- compileTasksMap.set(
128
- globValue,
129
- {
130
- module,
131
- taskFn: (sourceFile, targetPath) => {
132
- const newGlobValue = Array.isArray(sourceFile) ? sourceFile : globValue;
133
- const newDestPath = targetPath ? targetPath : destPath;
134
- const srcPipe = src(newGlobValue, { ...srcOption });
135
- return srcPipe
136
- // .pipe(cache(image()))
137
- // .on('error', (err) => {
138
- // fail(`image编译报错${err}`);
139
- // })
140
- .pipe(dest(newDestPath))
141
- .on('error', (err) => {
142
- fail(`image编译报错${err}`);
143
- });
144
- },
145
- },
146
- );
147
- break;
148
- case 'other':
149
- compileTasksMap.set(
150
- globValue,
151
- {
152
- module,
153
- taskFn: (sourceFile, targetPath) => {
154
- const newGlobValue = Array.isArray(sourceFile) ? sourceFile : globValue;
155
- const newDestPath = targetPath ? targetPath : destPath;
156
- const srcPipe = src(newGlobValue, { ...srcOption });
157
- return srcPipe
158
- .pipe(dest(newDestPath))
159
- .on('error', (err) => {
160
- fail(`编译报错${err}`);
161
- });
162
- },
163
- },
164
- );
165
- break;
166
- default:
167
- break;
168
- }
169
- });
50
+ compileTasksMap.set(
51
+ glob,
52
+ {
53
+ module,
54
+ taskFn: (sourceFile, targetPath) => {
55
+ const newGlobValue = Array.isArray(sourceFile) ? sourceFile : glob;
56
+ const newDestPath = targetPath ? targetPath : destPath;
57
+ let srcPipe = src(newGlobValue, { ...srcOption });
58
+ const pluginParams = { module, isDev };
59
+ srcPipe = addPlugins(tmsConfig, srcPipe, pluginParams);
60
+ return srcPipe
61
+ .pipe(dest(newDestPath))
62
+ .on('error', (err) => {
63
+ fail(`编译报错${err}`);
64
+ });
65
+ },
66
+ },
67
+ );
170
68
  return compileTasksMap;
171
69
  };
@@ -3,124 +3,84 @@ 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');
10
10
  const watch = require('./watch');
11
11
  const { info } = require('../utils/log');
12
+ const report = require('../core/report');
12
13
 
13
14
  const watchEvents = ['add', 'change', 'unlink', 'addDir', 'unlinkDir'];
14
- function excludeGlob(glob) {
15
- const otherArr = new Set();
16
- otherArr.add('!**/*.{ttf,otf,woff,eot}');
17
- Object.keys(glob).forEach((globKey) => {
18
- if (typeof glob[globKey] === 'string') {
19
- const data = glob[globKey].startsWith('!') ? glob[globKey] : `!${glob[globKey]}`;
20
- otherArr.add(data);
21
- } if (Array.isArray(glob[globKey])) {
22
- glob[globKey].forEach((value) => {
23
- if (typeof value === 'string') {
24
- const data = value.startsWith('!') ? value : `!${value}`;
25
- otherArr.add(data);
26
- }
27
- });
28
- }
29
- });
30
- return Array.from(otherArr);
31
- }
32
15
 
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) => {
16
+ module.exports = async (tmsConfig, modules, isDev = true) => {
40
17
  const compileTasksMap = new Map();
41
18
 
42
19
  // 监听根目录的文件
43
20
  mergeMap(compileTasksMap, compile(tmsConfig, {
44
- glob: {
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
- },
21
+ glob: DEFAULT_COPY_CONFIG.map(item => resolve(item)),
49
22
  module: { from: resolve(), to: resolve(tmsConfig.outputDir) },
50
23
  destPath: resolve(tmsConfig.outputDir),
51
24
  srcOption: { allowEmpty: true },
52
25
  isDev,
53
26
  }));
54
-
55
27
  // 监听模块的文件
56
- for (let module of newModules) {
28
+ for (let moduleItem of modules) {
57
29
  // 处理默认参数
58
- module = {
30
+ moduleItem = {
59
31
  ...{ exclude: [] },
60
- ...module,
32
+ ...moduleItem,
61
33
  };
62
- const modulePath = adaptPath(module.path);
34
+ const srcModulePath = getAbsolutePath(moduleItem.path);
35
+ const buildModulePath = resolve(tmsConfig.outputDir, moduleItem.modulePath);
63
36
 
64
37
  if (isDev) {
65
38
  // 监听模块配置文件
66
39
  watch(
67
- [`${modulePath}/**/module.config.json`],
40
+ [`${srcModulePath}/**/module.config.json`],
68
41
  { events: watchEvents },
69
- () => buildOutputAppJson(tmsConfig, newModules, isDev),
70
- { from: modulePath, to: modulePath },
42
+ () => buildOutputAppJson(tmsConfig, modules, isDev),
43
+ { from: srcModulePath, to: buildModulePath },
71
44
  );
72
45
  }
73
46
 
74
- const excludes = module.exclude.map(key => `!${resolve(key)}`);
75
- 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
- };
47
+ const excludes = moduleItem.exclude.map((ePath) => {
48
+ const newPath = getAbsolutePath(ePath, srcModulePath);
49
+ const ext = path.extname(ePath).slice(1);
50
+ if (ext) {
51
+ return `!${resolve(srcModulePath, newPath)}`;
52
+ }
53
+ return `!${resolve(srcModulePath, newPath)}/**/*`;
54
+ });
82
55
 
83
56
  mergeMap(compileTasksMap, compile(tmsConfig, {
84
- glob: {
85
- ...glob,
86
- other: [`${modulePath}/**/*`, ...excludeGlob(glob)],
87
- },
88
- destPath: resolve(tmsConfig.outputDir, module.root),
89
- module: { from: module.path, to: module.root },
57
+ glob: [`${srcModulePath}/**/*`, `!${srcModulePath}/**/module.config.json`, ...excludes],
58
+ destPath: buildModulePath,
59
+ module: { from: srcModulePath, to: buildModulePath },
90
60
  srcOption: { allowEmpty: true },
91
61
  isDev,
92
62
  }));
93
63
  }
94
64
 
95
- // 静态资源目录-拷贝
65
+ // static静态资源目录
96
66
  if (tmsConfig?.static && tmsConfig?.static.length > 0) {
97
67
  for (const item of tmsConfig.static) {
98
- item.from = adaptPath(item.from);
99
- item.to = adaptPath(item.to);
68
+ item.from = getAbsolutePath(item.from);
69
+ item.to = getAbsolutePath(item.to);
100
70
 
101
71
  let glob = {};
102
72
  const ext = path.extname(item.from).slice(1);
103
73
  if (ext) {
104
- glob[ext] = [item.from];
74
+ glob = [item.from];
105
75
  } else {
106
- glob = {
107
- js: [`${item.from}/**/*.{js,ts,wxs}`],
108
- json: [`${item.from}/**/*.json`],
109
- wxss: [`${item.from}/**/*.{less,wxss}`],
110
- wxml: [`${item.from}/**/*.wxml`],
111
- image: [`${item.from}/**/*.{png,jpg,jpeg,gif,svg}`],
112
- };
113
- glob.other = [`${item.from}/**/*`, ...excludeGlob(glob)];
76
+ glob = [`${item.from}/**/*`];
114
77
  }
115
78
 
116
79
  const from = fs.lstatSync(item.from).isFile() ? path.dirname(item.from) : item.from;
117
80
  mergeMap(compileTasksMap, compile(tmsConfig, {
118
81
  glob,
119
82
  destPath: item.to,
120
- module: {
121
- from,
122
- to: item.to,
123
- },
83
+ module: { from, to: item.to },
124
84
  srcOption: { allowEmpty: true },
125
85
  isDev,
126
86
  }));
@@ -142,7 +102,7 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
142
102
  watch(
143
103
  [resolve('app.json')],
144
104
  { ignoreInitial: false, events: watchEvents },
145
- () => buildOutputAppJson(tmsConfig, newModules, isDev),
105
+ () => buildOutputAppJson(tmsConfig, modules, isDev),
146
106
  { from: resolve(), to: resolve(tmsConfig.outputDir) },
147
107
  );
148
108
  // 监听其他文件
@@ -150,15 +110,16 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
150
110
  watch(globValue, { ignoreInitial: true, events: watchEvents }, taskFn, module);
151
111
  });
152
112
  } else {
153
- buildOutputAppJson(tmsConfig, newModules, isDev);
113
+ buildOutputAppJson(tmsConfig, modules, isDev);
154
114
  }
155
115
  eTime = new Date().getTime() - sTime;
156
116
  if (typeof tmsConfig?.hooks?.afterCompile === 'function') {
157
117
  await tmsConfig?.hooks?.afterCompile({
158
118
  isDev,
159
119
  tmsConfig: filterField(tmsConfig, ['gitAccount']),
160
- modules: newModules,
120
+ modules,
161
121
  });
122
+ report('hooks:afterCompile');
162
123
  }
163
124
  spinner.succeed(chalk.green(`首次编译完成, 耗时${eTime / 1000}s, 微信开发者工具打开项目即可预览。`));
164
125
  spinner.stop();
@@ -0,0 +1,13 @@
1
+ /* eslint-disable no-param-reassign */
2
+ function mpProjectJson() {
3
+ return function ({ file, cb }) {
4
+ if (file.isBuffer()) {
5
+ let contents = String(file.contents);
6
+ contents = contents.replace(/"miniprogramRoot"[^,]+,/g, '');
7
+ file.contents = Buffer.from(contents);
8
+ }
9
+ cb();
10
+ };
11
+ }
12
+
13
+ module.exports = mpProjectJson;
@@ -1,25 +1,45 @@
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 { resolve } = require('../utils/widgets');
5
+ const { series } = require('gulp');
6
+ const { info, warn, fail } = require('../utils/log');
7
+ const { resolve, filterField } = require('../utils/widgets');
6
8
  const { global } = require('../utils/global');
7
- const { findAllFilesOfDir } = require('../utils/io');
9
+ const { checkPackageVersion } = require('../core/checkDependencies');
10
+ const report = require('../core/report');
8
11
 
9
12
  const TIP_MAP = {
10
- 'package.json': '若依赖有变动,请重新执行tmskit run dev',
11
- 'module.config.json': '若dependencies字段有变动,需要重新执行tmskit run dev才会生效',
13
+ 'package.json': {
14
+ tip: '依赖版本有变动,请重新执行tmskit run dev',
15
+ check: (sourceFile, targetFile) => {
16
+ const targetArr = targetFile.split('/');
17
+ targetArr[targetArr.length - 1] = 'node_modules';
18
+ const packageArr = [{
19
+ srcPackageDir: sourceFile,
20
+ destNpmDir: targetArr.join('/'),
21
+ }];
22
+ return checkPackageVersion(packageArr);
23
+ },
24
+ },
25
+ 'module.config.json': {
26
+ tip: '若dependencies字段有变动,请重新执行tmskit run dev才会生效',
27
+ check: () => true,
28
+ },
12
29
  };
13
30
 
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;
31
+ const logTip = (fileName, sourceFile, targetFile, tipMap) => {
32
+ try {
33
+ for (const item in tipMap) {
34
+ if (fileName.indexOf(item) > -1 && tipMap[item].check(sourceFile, targetFile)) {
35
+ warn(`${fileName}有更新, ${tipMap[item].tip}`);
36
+ return;
37
+ }
20
38
  }
39
+ info(`${fileName}有更新`);
40
+ } catch (e) {
41
+ fail(`${sourceFile}文件更新提示出现错误: ${e.message}`);
21
42
  }
22
- info(`${fileName}有更新`);
23
43
  };
24
44
 
25
45
  const getTargetFile = (sourceFile, module, outputDir) => {
@@ -39,27 +59,58 @@ module.exports = function (globValue, watchOptions, callback, module) {
39
59
 
40
60
  const tmsConfig = global.getData('tmsConfig');
41
61
 
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
62
  const targetFile = getTargetFile(sourceFile, module, tmsConfig.outputDir);
56
63
  if (vinyl.event === 'unlink' || vinyl.event === 'unlinkDir') {
57
64
  info(`删除${sourceFileName}`);
58
65
  shellJs.rm('-rf', targetFile);
66
+ updateFileHook(tmsConfig, sourceFile, targetFile, vinyl.event);
59
67
  return;
60
68
  }
61
-
62
- logTip(sourceFileName, TIP_MAP);
63
- return callback([sourceFile], path.dirname(targetFile));
69
+ runCallback({
70
+ tmsConfig,
71
+ sourceFile,
72
+ targetFile,
73
+ callback,
74
+ type: vinyl.event,
75
+ });
76
+ logTip(sourceFileName, sourceFile, targetFile, TIP_MAP);
64
77
  });
65
78
  };
79
+
80
+ // 开始执行watch 文件的 callback函数
81
+ async function runCallback({
82
+ tmsConfig,
83
+ sourceFile,
84
+ targetFile,
85
+ callback,
86
+ type,
87
+ }) {
88
+ // callbacl.name === taskFn 代表进入gulp编译流程
89
+ // series为gulp编译串行执行,callback执行完毕后,执行updateFileHooks钩子
90
+ if (callback.name === 'taskFn') {
91
+ series(
92
+ callback.bind(null, [sourceFile], path.dirname(targetFile)),
93
+ (cb) => {
94
+ cb();
95
+ updateFileHook(tmsConfig, sourceFile, targetFile, type);
96
+ },
97
+ )();
98
+ } else {
99
+ // 普通的callback函数
100
+ await callback([sourceFile], path.dirname(targetFile));
101
+ await updateFileHook(tmsConfig, sourceFile, targetFile, type);
102
+ }
103
+ }
104
+
105
+ // 执行源码文件更新的钩子
106
+ async function updateFileHook(tmsConfig, sourceFile, targetFile, type) {
107
+ if (typeof tmsConfig?.hooks?.updateFile === 'function') {
108
+ await tmsConfig.hooks.updateFile({
109
+ tmsConfig: filterField(tmsConfig, ['gitAccount']),
110
+ sourceFile,
111
+ targetFile,
112
+ type,
113
+ });
114
+ report('hooks:updateFile');
115
+ }
116
+ }