file-lane 2.0.2-beta.1 → 2.0.2-beta.11

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/lib/FileLane.d.ts CHANGED
@@ -9,17 +9,19 @@ import IFileLaneConfig from './interface/IFileLaneConfig';
9
9
  declare class FileLane<O = any> {
10
10
  readonly config: IFileLaneConfig<O>;
11
11
  readonly compilerOption?: O | undefined;
12
+ private readonly events?;
12
13
  private context;
13
14
  private watcher?;
14
15
  private compilation?;
15
- private ws;
16
16
  /**
17
17
  * 实例化FileLane
18
18
  * @param config fileLane 配置
19
19
  * @param projectPath 项目路径
20
20
  * @param compilerOption 编译参数,不同语言的项目具有的参数不同,即使同一项目开发者也会设置不同的参数
21
21
  */
22
- constructor(config: IFileLaneConfig<O>, projectPath?: string, compilerOption?: O | undefined);
22
+ constructor(config: IFileLaneConfig<O>, projectPath?: string, compilerOption?: O | undefined, events?: {
23
+ onBuildSuccess?: (() => void) | undefined;
24
+ } | undefined);
23
25
  /**
24
26
  * 运行
25
27
  * @param params
@@ -79,5 +81,11 @@ declare class FileLane<O = any> {
79
81
  * 清除输出文件夹
80
82
  */
81
83
  private cleanOutput;
84
+ /**
85
+ * 1. 配置的忽略文件夹
86
+ * 2. 默认应该忽略的文件及文件夹
87
+ * @returns
88
+ */
89
+ private getIgnoreConfig;
82
90
  }
83
91
  export default FileLane;
package/lib/FileLane.js CHANGED
@@ -13,13 +13,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const shared_utils_1 = require("@aiot-toolkit/shared-utils");
16
- const ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
17
- const FileUtil_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/utils/FileUtil"));
18
16
  const chokidar_1 = __importDefault(require("chokidar"));
17
+ const del_1 = __importDefault(require("del"));
19
18
  const fs_extra_1 = __importDefault(require("fs-extra"));
20
19
  const lodash_1 = __importDefault(require("lodash"));
21
20
  const path_1 = __importDefault(require("path"));
22
- const ws_1 = __importDefault(require("ws"));
23
21
  const FileLaneCompilation_1 = __importDefault(require("./FileLaneCompilation"));
24
22
  const CompilationEvent_1 = __importDefault(require("./event/CompilationEvent"));
25
23
  const FileEvent_1 = __importDefault(require("./event/FileEvent"));
@@ -38,9 +36,10 @@ class FileLane {
38
36
  * @param projectPath 项目路径
39
37
  * @param compilerOption 编译参数,不同语言的项目具有的参数不同,即使同一项目开发者也会设置不同的参数
40
38
  */
41
- constructor(config, projectPath, compilerOption) {
39
+ constructor(config, projectPath, compilerOption, events) {
42
40
  this.config = config;
43
41
  this.compilerOption = compilerOption;
42
+ this.events = events;
44
43
  this.processExitHandler = () => {
45
44
  this.dispose();
46
45
  };
@@ -56,10 +55,10 @@ class FileLane {
56
55
  return __awaiter(this, void 0, void 0, function* () {
57
56
  const errorList = this.validateConfig();
58
57
  if (errorList && errorList.length) {
59
- ColorConsole_1.default.error(`### file-lane ### ${errorList.map((item, index) => `${index + 1}. ${item}`).join('\r\n')}`);
58
+ shared_utils_1.ColorConsole.error(`### file-lane ### ${errorList.map((item, index) => `${index + 1}. ${item}`).join('\r\n')}`);
60
59
  return;
61
60
  }
62
- this.cleanOutput();
61
+ yield this.cleanOutput();
63
62
  const fileList = this.collectFile();
64
63
  if (!fileList || !fileList.length) {
65
64
  return;
@@ -132,6 +131,7 @@ class FileLane {
132
131
  */
133
132
  build(fileList) {
134
133
  return __awaiter(this, void 0, void 0, function* () {
134
+ const { onBuildSuccess } = this.events || {};
135
135
  this.initCompilation();
136
136
  try {
137
137
  yield this.runPreWorks(fileList);
@@ -146,14 +146,17 @@ class FileLane {
146
146
  this.triggerPlugins(new CompilationEvent_1.default(CompilationEvent_1.default.FLLOW_WORK_START));
147
147
  yield this.runFollowWorks();
148
148
  this.triggerPlugins(new CompilationEvent_1.default(CompilationEvent_1.default.FLLOW_WORK_END));
149
- ColorConsole_1.default.success({
149
+ shared_utils_1.ColorConsole.success({
150
150
  word: 'Success: build finish!',
151
- style: ColorConsole_1.default.getStyle(shared_utils_1.LOG_LEVEL.Success)
151
+ style: shared_utils_1.ColorConsole.getStyle(shared_utils_1.Loglevel.SUCCESS)
152
152
  });
153
+ onBuildSuccess === null || onBuildSuccess === void 0 ? void 0 : onBuildSuccess();
153
154
  }
154
155
  catch (error) {
155
- console.log('error', error);
156
- ColorConsole_1.default.throw(`ERROR: build error, ${error || 'unknown error'}`);
156
+ console.log('FileLane error', error);
157
+ shared_utils_1.ColorConsole.throw(`ERROR: `, { word: 'build error' }, `, ${error || 'unknown error'}`);
158
+ // 结束任务
159
+ process.exit();
157
160
  }
158
161
  });
159
162
  }
@@ -234,8 +237,8 @@ class FileLane {
234
237
  let loaders = [];
235
238
  for (let rule of rules) {
236
239
  // 1. 检查filePath是否符合规则test
237
- if (FileUtil_1.default.match(parse, rule.test) &&
238
- FileUtil_1.default.include(filePath, rule.include, rule.exclude)) {
240
+ if (shared_utils_1.FileUtil.match(parse, rule.test) &&
241
+ shared_utils_1.FileUtil.include(filePath, rule.include, rule.exclude)) {
239
242
  // 2. 符合规则就添加到返回列表中
240
243
  loaders.push(...rule.loader);
241
244
  }
@@ -257,13 +260,7 @@ class FileLane {
257
260
  const { preWorks } = this.config;
258
261
  if (preWorks) {
259
262
  for (let item of preWorks) {
260
- try {
261
- yield item(this.context, fileList, this.config, this.compilerOption);
262
- }
263
- catch (error) {
264
- // prework的item error
265
- ColorConsole_1.default.throw((error === null || error === void 0 ? void 0 : error.toString()) || 'unkonw error');
266
- }
263
+ yield item(this.context, fileList, this.config, this.compilerOption);
267
264
  }
268
265
  }
269
266
  });
@@ -276,7 +273,15 @@ class FileLane {
276
273
  const { followWorks } = this.config;
277
274
  if (followWorks) {
278
275
  for (let item of followWorks) {
279
- yield item(this.context, this.config, this.compilerOption);
276
+ try {
277
+ shared_utils_1.ColorConsole.info(`FollowWork: ${item.workerDescribe} start`);
278
+ yield item.worker(this.context, this.config, this.compilerOption);
279
+ shared_utils_1.ColorConsole.info(`FollowWork: ${item.workerDescribe} end`);
280
+ }
281
+ catch (error) {
282
+ shared_utils_1.ColorConsole.throw(`FollowWork: ${item.workerDescribe} error, ${error}`);
283
+ process.exit();
284
+ }
280
285
  }
281
286
  }
282
287
  });
@@ -285,16 +290,8 @@ class FileLane {
285
290
  return __awaiter(this, void 0, void 0, function* () {
286
291
  // 监听文件变化,并触发 build
287
292
  this.listenFileChange(() => __awaiter(this, void 0, void 0, function* () {
288
- var _a, _b, _c;
289
293
  const fileList = this.collectFile();
290
- if (!this.ws && ((_a = this.compilerOption) === null || _a === void 0 ? void 0 : _a.hasOwnProperty('server'))) {
291
- const port = (_b = this.compilerOption.server) === null || _b === void 0 ? void 0 : _b.port;
292
- this.ws = new ws_1.default(`ws://localhost:${port}`);
293
- }
294
294
  yield this.build(fileList);
295
- (_c = this.ws) === null || _c === void 0 ? void 0 : _c.send(JSON.stringify({
296
- type: 'restart'
297
- }));
298
295
  }));
299
296
  });
300
297
  }
@@ -308,8 +305,8 @@ class FileLane {
308
305
  let files = [];
309
306
  if (entryFileList) {
310
307
  entryFileList.forEach((filePath) => {
311
- if (FileUtil_1.default.include(filePath, this.config.include, this.config.exclude)) {
312
- ColorConsole_1.default.log(`### file-lane ### file change: ${filePath}`);
308
+ if (shared_utils_1.FileUtil.include(filePath, this.config.include, this.config.exclude)) {
309
+ shared_utils_1.ColorConsole.log(`### file-lane ### file change: ${filePath}`);
313
310
  files.push(filePath);
314
311
  }
315
312
  });
@@ -318,7 +315,7 @@ class FileLane {
318
315
  // 1.取出projectPath
319
316
  const projectPath = this.context.projectPath;
320
317
  // 2. 循环文件夹,取出所有匹配的文件路径
321
- files = FileUtil_1.default.readAlldirSync(projectPath, this.config.include, this.config.exclude);
318
+ files = shared_utils_1.FileUtil.readAlldirSync(projectPath, this.config.include, this.config.exclude);
322
319
  }
323
320
  return files;
324
321
  }
@@ -328,7 +325,7 @@ class FileLane {
328
325
  */
329
326
  listenFileChange(onChange) {
330
327
  const watcher = chokidar_1.default.watch(this.context.projectPath, {
331
- ignored: this.config.watchIgnores
328
+ ignored: this.getIgnoreConfig()
332
329
  });
333
330
  const throttledOnChange = lodash_1.default.throttle(onChange, 1000, {
334
331
  leading: true,
@@ -337,13 +334,13 @@ class FileLane {
337
334
  const handler = (filePath) => {
338
335
  const { exclude, include } = this.config;
339
336
  // 判断是否为删除,执行onChange回调
340
- if (FileUtil_1.default.include(filePath, include, exclude)) {
337
+ if (shared_utils_1.FileUtil.include(filePath, include, exclude)) {
341
338
  throttledOnChange([filePath]);
342
339
  }
343
340
  };
344
341
  watcher
345
342
  .on('ready', () => {
346
- ColorConsole_1.default.log(`### file-lane ### Initial scan complete. Ready for Watch.`);
343
+ shared_utils_1.ColorConsole.log(`### file-lane ### Initial scan complete. Ready for Watch.`);
347
344
  watcher
348
345
  .on('add', (path) => {
349
346
  // 监听文件添加事件
@@ -361,6 +358,7 @@ class FileLane {
361
358
  .on('error', (error) => {
362
359
  // 监听错误
363
360
  FileLaneUtil_1.default.checkError(error.message);
361
+ watcher.close();
364
362
  });
365
363
  if (this.watcher) {
366
364
  this.watcher.close();
@@ -375,7 +373,31 @@ class FileLane {
375
373
  * 清除输出文件夹
376
374
  */
377
375
  cleanOutput() {
378
- fs_extra_1.default.removeSync(this.outputPath);
376
+ return __awaiter(this, void 0, void 0, function* () {
377
+ yield (0, del_1.default)(this.outputPath, { force: true });
378
+ });
379
+ }
380
+ /**
381
+ * 1. 配置的忽略文件夹
382
+ * 2. 默认应该忽略的文件及文件夹
383
+ * @returns
384
+ */
385
+ getIgnoreConfig() {
386
+ let ignoreList = [];
387
+ // 1.
388
+ if (this.config.watchIgnores) {
389
+ if (Array.isArray(this.config.watchIgnores)) {
390
+ ignoreList.push(...this.config.watchIgnores);
391
+ }
392
+ else {
393
+ ignoreList.push(this.config.watchIgnores);
394
+ }
395
+ }
396
+ // 2. 忽略.开头的隐藏文件夹
397
+ ignoreList.push(/(^|[\/\\])\../);
398
+ // 3. 忽略output目录
399
+ ignoreList.push(path_1.default.join(this.context.projectPath, this.config.output));
400
+ return ignoreList;
379
401
  }
380
402
  }
381
403
  exports.default = FileLane;
package/lib/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import FileLane from './FileLane';
2
- import IFileLaneConfig from './interface/IFileLaneConfig';
2
+ import IFileLaneConfig, { FollowWork, PreWork } from './interface/IFileLaneConfig';
3
3
  import IFileLaneContext from './interface/IFileLaneContext';
4
4
  import IFileParam from './interface/IFileParam';
5
5
  import ILoader from './interface/ILoader';
6
6
  import IPlugin from './interface/IPlugin';
7
- export { FileLane, IFileLaneConfig, IFileLaneContext, IFileParam, ILoader, IPlugin };
7
+ import FileLaneUtil from './utils/FileLaneUtil';
8
+ export { FileLane, IFileLaneConfig, IFileLaneContext, IFileParam, ILoader, IPlugin, FileLaneUtil, PreWork, FollowWork };
9
+ export default FileLane;
package/lib/index.js CHANGED
@@ -3,6 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.FileLane = void 0;
6
+ exports.FileLaneUtil = exports.FileLane = void 0;
7
7
  const FileLane_1 = __importDefault(require("./FileLane"));
8
8
  exports.FileLane = FileLane_1.default;
9
+ const FileLaneUtil_1 = __importDefault(require("./utils/FileLaneUtil"));
10
+ exports.FileLaneUtil = FileLaneUtil_1.default;
11
+ exports.default = FileLane_1.default;
@@ -1,10 +1,22 @@
1
- import { MatchType } from '@aiot-toolkit/shared-utils/lib/utils/FileUtil';
1
+ import { MatchType } from '@aiot-toolkit/shared-utils';
2
2
  import IFileLaneContext from './IFileLaneContext';
3
3
  import ILoader from './ILoader';
4
4
  import IPlugin from './IPlugin';
5
5
  export type ILoaderClass = (new (...args: any[]) => ILoader) & {
6
6
  raw?: boolean;
7
7
  };
8
+ export interface IRule {
9
+ /**
10
+ * 配置文件
11
+ */
12
+ test: MatchType;
13
+ /**
14
+ * 文件的 Loader,从前向后依次执行,前一个 loader 结果做为后一个 loader 的入参
15
+ */
16
+ loader: ILoaderClass[];
17
+ exclude?: MatchType;
18
+ include?: MatchType;
19
+ }
8
20
  /**
9
21
  * IFileLaneConfig
10
22
  */
@@ -45,15 +57,7 @@ export default interface IFileLaneConfig<O = any> {
45
57
  /**
46
58
  * 转换规则
47
59
  */
48
- rules: {
49
- /**
50
- * 配置文件
51
- */
52
- test: MatchType;
53
- loader: ILoaderClass[];
54
- exclude?: MatchType;
55
- include?: MatchType;
56
- }[];
60
+ rules: IRule[];
57
61
  };
58
62
  /**
59
63
  * 插件
@@ -78,11 +82,16 @@ export default interface IFileLaneConfig<O = any> {
78
82
  * ux--[webpack, zip]
79
83
  * uxInspect--[webpack, zipPhone, zipWatch, zipTv]
80
84
  */
81
- followWorks?: FollowWork<O>[];
85
+ followWorks?: FollowWoker<O>[];
82
86
  /**
83
87
  * 配置watch时忽略的文件或者文件夹
84
88
  */
85
89
  watchIgnores?: MatchType;
86
90
  }
91
+ type FollowWoker<O> = {
92
+ worker: FollowWork<O>;
93
+ workerDescribe?: string;
94
+ };
87
95
  export type FollowWork<O = any> = (context: IFileLaneContext, config?: IFileLaneConfig, compilerOption?: O) => Promise<any>;
88
96
  export type PreWork<O = any> = (context: IFileLaneContext, fileList: string[], config: IFileLaneConfig, compilerOption?: O) => Promise<any>;
97
+ export {};
@@ -11,13 +11,6 @@ declare class FileLaneUtil {
11
11
  * @returns
12
12
  */
13
13
  static pathToFileParam(path: string, readContent?: boolean): IFileParam;
14
- /**
15
- * 将buildPath文件夹的内容压缩成zip包,放置于targetPath路径下
16
- * @param buildPath
17
- * @param targetPath
18
- * @param zipRootDirNames 可自定义压缩包内最外层目录的名称
19
- */
20
- static zipProject(buildPath: string[], targetFile: string, zipRootDirNames?: string[]): Promise<void>;
21
14
  static createContext(output: string, projectPath?: string): IFileLaneContext;
22
15
  static checkError(message: string): void;
23
16
  }
@@ -1,22 +1,10 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
14
5
  Object.defineProperty(exports, "__esModule", { value: true });
15
6
  const shared_utils_1 = require("@aiot-toolkit/shared-utils");
16
- const ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
17
- const archiver_1 = __importDefault(require("archiver"));
18
7
  const fs_extra_1 = __importDefault(require("fs-extra"));
19
- const path_1 = __importDefault(require("path"));
20
8
  /**
21
9
  * FileLaneUtil
22
10
  */
@@ -40,49 +28,6 @@ class FileLaneUtil {
40
28
  content
41
29
  };
42
30
  }
43
- /**
44
- * 将buildPath文件夹的内容压缩成zip包,放置于targetPath路径下
45
- * @param buildPath
46
- * @param targetPath
47
- * @param zipRootDirNames 可自定义压缩包内最外层目录的名称
48
- */
49
- static zipProject(buildPath, targetFile, zipRootDirNames) {
50
- return __awaiter(this, void 0, void 0, function* () {
51
- return new Promise((resolve, reject) => {
52
- const output = fs_extra_1.default.createWriteStream(targetFile);
53
- const archive = archiver_1.default.create('zip', {
54
- zlib: { level: 9 }
55
- });
56
- // 监听错误
57
- archive.on('error', (err) => {
58
- ColorConsole_1.default.throw(`${err.message}`);
59
- reject();
60
- });
61
- // 监听写入流打开的事件
62
- output.on('open', () => {
63
- ColorConsole_1.default.info(`Write stream is open`);
64
- });
65
- output.on('close', () => {
66
- ColorConsole_1.default.info(`Write stream is closed`);
67
- resolve();
68
- });
69
- // 将压缩文件导入到输出流中
70
- archive.pipe(output);
71
- // 指定压缩目录
72
- buildPath.forEach((folder, index) => {
73
- const folderName = path_1.default.basename(folder); // 获取文件夹名
74
- if (zipRootDirNames && zipRootDirNames[index]) {
75
- archive.directory(folder, zipRootDirNames[index]);
76
- }
77
- else {
78
- archive.directory(folder, folderName);
79
- }
80
- });
81
- // 完成压缩,关闭输出流
82
- archive.finalize();
83
- });
84
- });
85
- }
86
31
  static createContext(output, projectPath) {
87
32
  const cwd = process.cwd();
88
33
  return {
@@ -93,14 +38,16 @@ class FileLaneUtil {
93
38
  }
94
39
  static checkError(message) {
95
40
  if (message.includes('System limit for number of file watchers reached')) {
96
- ColorConsole_1.default.error('There are too many files to watch, ', {
41
+ shared_utils_1.ColorConsole.error('There are too many files to watch, ', {
97
42
  word: 'please configure the content to be ignored.',
98
43
  style: {
99
- bgColor: ColorConsole_1.default.getStyle(shared_utils_1.LOG_LEVEL.Error).color || '#FF0000'
44
+ bgColor: shared_utils_1.ColorConsole.getStyle(shared_utils_1.Loglevel.ERROR).color || '#FF0000'
100
45
  }
101
46
  }, '(For example, node_modules)');
102
47
  }
103
- ColorConsole_1.default.throw(`### file-lane ### watch error ${message}`);
48
+ else {
49
+ shared_utils_1.ColorConsole.throw(`### file-lane ### watch error ${message}`);
50
+ }
104
51
  }
105
52
  }
106
53
  exports.default = FileLaneUtil;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "file-lane",
3
- "version": "2.0.2-beta.1",
3
+ "version": "2.0.2-beta.11",
4
4
  "description": "File conversion tool, can be one-to-one, one to N, N to one",
5
5
  "keywords": [
6
6
  "file",
@@ -20,13 +20,15 @@
20
20
  "test": "node ./__tests__/file-lane.test.js"
21
21
  },
22
22
  "dependencies": {
23
- "@aiot-toolkit/shared-utils": "2.0.2-beta.1",
24
- "archiver": "^6.0.1",
23
+ "@aiot-toolkit/shared-utils": "2.0.2-beta.11",
25
24
  "chokidar": "^3.5.3",
25
+ "del": "^4.1.0",
26
26
  "lodash": "^4.17.21"
27
27
  },
28
28
  "devDependencies": {
29
- "@types/archiver": "^5.3.2"
29
+ "@types/archiver": "^5.3.2",
30
+ "@types/fs-extra": "^11.0.4",
31
+ "fs-extra": "^11.2.0"
30
32
  },
31
- "gitHead": "9244af36e617295ea4ff11a2f4f07f0e4f9f5f59"
33
+ "gitHead": "07c38dd12ba0d932c6dc1fd16fe25aaf15ab9fbb"
32
34
  }