file-lane 2.0.2-beta.10 → 2.0.2-beta.12

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,15 @@ 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
156
  console.log('FileLane error', error);
156
- ColorConsole_1.default.throw(`ERROR: build error, ${error || 'unknown error'}`);
157
+ shared_utils_1.ColorConsole.throw(`ERROR: `, { word: 'build error' }, `, ${error || 'unknown error'}`);
157
158
  // 结束任务
158
159
  process.exit();
159
160
  }
@@ -236,8 +237,8 @@ class FileLane {
236
237
  let loaders = [];
237
238
  for (let rule of rules) {
238
239
  // 1. 检查filePath是否符合规则test
239
- if (FileUtil_1.default.match(parse, rule.test) &&
240
- 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)) {
241
242
  // 2. 符合规则就添加到返回列表中
242
243
  loaders.push(...rule.loader);
243
244
  }
@@ -259,13 +260,7 @@ class FileLane {
259
260
  const { preWorks } = this.config;
260
261
  if (preWorks) {
261
262
  for (let item of preWorks) {
262
- try {
263
- yield item(this.context, fileList, this.config, this.compilerOption);
264
- }
265
- catch (error) {
266
- // prework的item error
267
- ColorConsole_1.default.throw((error === null || error === void 0 ? void 0 : error.toString()) || 'unkonw error');
268
- }
263
+ yield item(this.context, fileList, this.config, this.compilerOption);
269
264
  }
270
265
  }
271
266
  });
@@ -279,12 +274,12 @@ class FileLane {
279
274
  if (followWorks) {
280
275
  for (let item of followWorks) {
281
276
  try {
282
- ColorConsole_1.default.info(`FollowWork: ${item.workerDescribe} start`);
277
+ shared_utils_1.ColorConsole.info(`FollowWork: ${item.workerDescribe} start`);
283
278
  yield item.worker(this.context, this.config, this.compilerOption);
284
- ColorConsole_1.default.info(`FollowWork: ${item.workerDescribe} end`);
279
+ shared_utils_1.ColorConsole.info(`FollowWork: ${item.workerDescribe} end`);
285
280
  }
286
281
  catch (error) {
287
- ColorConsole_1.default.throw(`FollowWork: ${item.workerDescribe} error, ${error}`);
282
+ shared_utils_1.ColorConsole.throw(`FollowWork: ${item.workerDescribe} error, ${error}`);
288
283
  process.exit();
289
284
  }
290
285
  }
@@ -295,16 +290,8 @@ class FileLane {
295
290
  return __awaiter(this, void 0, void 0, function* () {
296
291
  // 监听文件变化,并触发 build
297
292
  this.listenFileChange(() => __awaiter(this, void 0, void 0, function* () {
298
- var _a, _b, _c;
299
293
  const fileList = this.collectFile();
300
- if (!this.ws && ((_a = this.compilerOption) === null || _a === void 0 ? void 0 : _a.hasOwnProperty('server'))) {
301
- const port = (_b = this.compilerOption.server) === null || _b === void 0 ? void 0 : _b.port;
302
- this.ws = new ws_1.default(`ws://localhost:${port}`);
303
- }
304
294
  yield this.build(fileList);
305
- (_c = this.ws) === null || _c === void 0 ? void 0 : _c.send(JSON.stringify({
306
- type: 'restart'
307
- }));
308
295
  }));
309
296
  });
310
297
  }
@@ -318,8 +305,8 @@ class FileLane {
318
305
  let files = [];
319
306
  if (entryFileList) {
320
307
  entryFileList.forEach((filePath) => {
321
- if (FileUtil_1.default.include(filePath, this.config.include, this.config.exclude)) {
322
- 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}`);
323
310
  files.push(filePath);
324
311
  }
325
312
  });
@@ -328,7 +315,7 @@ class FileLane {
328
315
  // 1.取出projectPath
329
316
  const projectPath = this.context.projectPath;
330
317
  // 2. 循环文件夹,取出所有匹配的文件路径
331
- 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);
332
319
  }
333
320
  return files;
334
321
  }
@@ -338,7 +325,7 @@ class FileLane {
338
325
  */
339
326
  listenFileChange(onChange) {
340
327
  const watcher = chokidar_1.default.watch(this.context.projectPath, {
341
- ignored: this.config.watchIgnores
328
+ ignored: this.getIgnoreConfig()
342
329
  });
343
330
  const throttledOnChange = lodash_1.default.throttle(onChange, 1000, {
344
331
  leading: true,
@@ -347,13 +334,13 @@ class FileLane {
347
334
  const handler = (filePath) => {
348
335
  const { exclude, include } = this.config;
349
336
  // 判断是否为删除,执行onChange回调
350
- if (FileUtil_1.default.include(filePath, include, exclude)) {
337
+ if (shared_utils_1.FileUtil.include(filePath, include, exclude)) {
351
338
  throttledOnChange([filePath]);
352
339
  }
353
340
  };
354
341
  watcher
355
342
  .on('ready', () => {
356
- 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.`);
357
344
  watcher
358
345
  .on('add', (path) => {
359
346
  // 监听文件添加事件
@@ -371,6 +358,7 @@ class FileLane {
371
358
  .on('error', (error) => {
372
359
  // 监听错误
373
360
  FileLaneUtil_1.default.checkError(error.message);
361
+ watcher.close();
374
362
  });
375
363
  if (this.watcher) {
376
364
  this.watcher.close();
@@ -385,7 +373,31 @@ class FileLane {
385
373
  * 清除输出文件夹
386
374
  */
387
375
  cleanOutput() {
388
- 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;
389
401
  }
390
402
  }
391
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,4 +1,4 @@
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';
@@ -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.10",
3
+ "version": "2.0.2-beta.12",
4
4
  "description": "File conversion tool, can be one-to-one, one to N, N to one",
5
5
  "keywords": [
6
6
  "file",
@@ -20,15 +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.10",
24
- "archiver": "^6.0.1",
23
+ "@aiot-toolkit/shared-utils": "2.0.2-beta.12",
25
24
  "chokidar": "^3.5.3",
25
+ "del": "^4.1.0",
26
+ "fs-extra": "^11.2.0",
26
27
  "lodash": "^4.17.21"
27
28
  },
28
29
  "devDependencies": {
29
30
  "@types/archiver": "^5.3.2",
30
- "@types/fs-extra": "^11.0.4",
31
- "fs-extra": "^11.2.0"
31
+ "@types/fs-extra": "^11.0.4"
32
32
  },
33
- "gitHead": "00d23c1ad1306a6c72bb007685de9fd4a6ee61af"
33
+ "gitHead": "3d2f9f8f48bb88492013d867f6eff27cb33365a0"
34
34
  }