@tmsfe/tmskit 0.0.15-beta.4 → 0.0.16

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/main.js CHANGED
File without changes
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@tmsfe/tmskit",
3
- "version": "0.0.15-beta.4",
3
+ "version": "0.0.16",
4
4
  "description": "tmskit",
5
5
  "main": "dist/index.cjs",
6
6
  "bin": {
7
7
  "tmskit": "main.js"
8
8
  },
9
9
  "files": [
10
+ "build",
10
11
  "src",
11
12
  "dist",
12
13
  "main.js",
@@ -17,8 +18,7 @@
17
18
  "build": "rollup -c --environment TARGET:tmskit",
18
19
  "pub:patch": "sh build/publish.sh patch",
19
20
  "pub:minor": "sh build/publish.sh minor",
20
- "pub:major": "sh build/publish.sh major",
21
- "postinstall": "cd ./node_modules/gulp-watch && npm install chokidar@3.5.3"
21
+ "pub:major": "sh build/publish.sh major"
22
22
  },
23
23
  "author": "tms·web",
24
24
  "license": "ISC",
@@ -39,8 +39,18 @@
39
39
  "rollup-plugin-typescript2": "0.27.0"
40
40
  },
41
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",
42
51
  "async": "^3.2.2",
43
52
  "chalk": "^4.1.0",
53
+ "chokidar": "^3.5.3",
44
54
  "commander": "^8.3.0",
45
55
  "copy-webpack-plugin": "^9.1.0",
46
56
  "ejs": "^3.1.5",
@@ -57,7 +67,6 @@
57
67
  "moment": "^2.29.1",
58
68
  "object-assign": "^4.0.1",
59
69
  "ora": "^5.1.0",
60
- "patch-package": "^6.4.7",
61
70
  "plugin-error": "^1.0.0",
62
71
  "postcss": "^8.4.6",
63
72
  "precinct": "^8.3.1",
@@ -9,7 +9,7 @@ const compile = require('./compile');
9
9
  const watch = require('./watch');
10
10
  const { info } = require('../utils/log');
11
11
 
12
- const watchEvents = ['add', 'change', 'unlink', 'unlinkDir'];
12
+ const watchEvents = ['add', 'change', 'unlink', 'addDir', 'unlinkDir'];
13
13
  function excludeGlob(glob) {
14
14
  const otherArr = new Set();
15
15
  otherArr.add('!**/*.{ttf,otf,woff,eot}');
@@ -146,7 +146,7 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
146
146
  sTime = new Date().getTime();
147
147
  cb();
148
148
  }
149
- function end(cb) {
149
+ async function end(cb) {
150
150
  if (isDev) {
151
151
  compileTasksMap.forEach(({ taskFn, module }, globValue) => {
152
152
  watch(globValue, { ignoreInitial: true, events: watchEvents }, taskFn, module);
@@ -154,7 +154,7 @@ module.exports = async (tmsConfig, newModules, isDev = true) => {
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
159
  spinner.succeed(`首次编译完成, 耗时${eTime / 1000}s, 微信开发者工具打开项目即可预览。`);
160
160
  spinner.stop();
@@ -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;
@@ -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);
@@ -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);
@@ -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,
package/src/utils/io.js CHANGED
@@ -97,6 +97,25 @@ const fileInDir = (dir, file) => {
97
97
  };
98
98
 
99
99
 
100
+ function findAllFilesOfDir(dir) {
101
+ const list = [];
102
+ function listFile(dir) {
103
+ const arr = fs.readdirSync(dir);
104
+ arr.forEach((item) => {
105
+ const fullPath = path.join(dir, item);
106
+ const stats = fs.statSync(fullPath);
107
+ if (stats.isDirectory()) {
108
+ listFile(fullPath);
109
+ } else {
110
+ list.push(fullPath);
111
+ }
112
+ });
113
+ return list;
114
+ }
115
+ listFile(dir);
116
+ return list;
117
+ }
118
+
100
119
  module.exports = {
101
120
  isDirEmpty,
102
121
  copyFile,
@@ -105,4 +124,5 @@ module.exports = {
105
124
  ext,
106
125
  fileInDir,
107
126
  isFile,
127
+ findAllFilesOfDir,
108
128
  };
package/CHANGELOG.md DELETED
@@ -1,32 +0,0 @@
1
- ##
2
- 1. [0.0.5] 支持创建新的小程序
3
- 2. [0.0.6] 支持编译功能
4
- 3. [0.0.7]
5
- * 更新切换开发环境需要重启构建工具,调整为改代码直接生效
6
- * 修复切换第三方代码库分支后,构建工具没有重新拉代码的问题
7
- * 修复thirdparty代码没有正确构建问题
8
- * 新增对一个仓库下多个分包的支持
9
- * 优化代码下载流程,多仓库并行下载,第三方代码下载时间60s->15s
10
- * 本地private.config.js配置,支持指定屏蔽某些模块,屏蔽所有远程模块的设置
11
- 4. [0.0.10]
12
- * 优化npm install并行改成串行,60个模块开发第一次启动由100s优化至40s, 第二次启动2s;
13
- * 添加global全局缓存变量,避免变量层层传递的问题
14
- * 增加--latest参数,重新初始化操作(拉去第三方模块、npm下载、生成appJson)
15
- * 修复获取用户指定模块的bug
16
- 5. [0.0.12] 云函数支持创建软连, 支持引入项目外的云函数能力
17
-
18
- 6. [0.0.13]
19
- * 首次编译完成,提示用户编译完成.
20
- * 日志添加时间戳
21
- * 编译代码有更新时,增加提示日志
22
- * 删除源码文件或文件夹时,支持监听文件变动,删除编译文件
23
- * fix window中tar命令的兼容性问题
24
- 7. [0.0.15]
25
- * 删除原函数软连功能
26
- * fix 修复dist node_module.tar.gz没有删除bug
27
- 8. [-]
28
- * fix: 日志打印多空行问题
29
- * add: 增加--noCache的参数,支持不使用缓存运行dev
30
- * add: package.json、module.config.json更新后,提示重新启动dev
31
- * fix: gulp-watch更新某个文件时,编译多个文件的问题
32
- * 策略:拉取第三方代码,dev流程都会执行