file-lane 2.0.2 → 2.0.3-beta.2

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
@@ -1,9 +1,28 @@
1
1
  import IFileLaneConfig from './interface/IFileLaneConfig';
2
+ import IFileLaneEvents from './interface/IFileLaneEvents';
2
3
  /**
3
4
  * FileLane
4
5
  *
5
6
  * 文件车道,用于将文件进行对应转换,支持1对1、1对N、N对1
6
7
  *
8
+ * 打包过程中,有以下事件
9
+ * 1. 打包成功:onBuildSuccess
10
+ * 2. 打包失败: onBuildError
11
+ * 3. 产生日志: onLog
12
+ *
13
+ * @example
14
+ * ```
15
+ * new FileLane<IJavascriptCompileOption>(
16
+ * fileLaneConfig,
17
+ * projectPath,
18
+ * compilerOption,
19
+ * {
20
+ * onBuildSuccess: (data) => {},
21
+ * onBuildError: (data) => {},
22
+ * onLog: (logs) => {}
23
+ * }).start()
24
+ * ```
25
+ *
7
26
  * @description
8
27
  */
9
28
  declare class FileLane<O = any> {
@@ -12,19 +31,20 @@ declare class FileLane<O = any> {
12
31
  private readonly events?;
13
32
  private context;
14
33
  private watcher?;
34
+ /**
35
+ * 每次编译的数据对象
36
+ */
15
37
  private compilation?;
16
38
  private changeFileList;
39
+ private triggerCount;
17
40
  /**
18
41
  * 实例化FileLane
19
42
  * @param config fileLane 配置
20
43
  * @param projectPath 项目路径
21
44
  * @param compilerOption 编译参数,不同语言的项目具有的参数不同,即使同一项目开发者也会设置不同的参数
22
45
  */
23
- constructor(config: IFileLaneConfig<O>, projectPath?: string, compilerOption?: O | undefined, events?: {
24
- onBuildSuccess?: (data: {
25
- costTime: number;
26
- }) => void;
27
- } | undefined);
46
+ constructor(config: IFileLaneConfig<O>, projectPath?: string, compilerOption?: O | undefined, events?: IFileLaneEvents | undefined);
47
+ private addProcessListener;
28
48
  /**
29
49
  * 运行
30
50
  * @param params
@@ -39,10 +59,9 @@ declare class FileLane<O = any> {
39
59
  watch?: boolean;
40
60
  }): Promise<void>;
41
61
  private validateConfig;
42
- private processExitHandler;
62
+ private processBeforeExitHandler;
43
63
  private sigintHandler;
44
- stop(): void;
45
- dispose(): void;
64
+ dispose(): Promise<void>;
46
65
  /**
47
66
  *
48
67
  * @param fileList 原始文件列表
@@ -51,6 +70,15 @@ declare class FileLane<O = any> {
51
70
  private initCompilation;
52
71
  private buildFile;
53
72
  private runLoaders;
73
+ /**
74
+ * 处理日志
75
+ * 1. 触发onLog事件
76
+ * 2. 如果有错误日志,则抛出错误
77
+ * @param logs
78
+ * @param onError
79
+ * @returns
80
+ */
81
+ private handlerLogs;
54
82
  private writeFiles;
55
83
  private runLoader;
56
84
  /**
package/lib/FileLane.js CHANGED
@@ -22,11 +22,30 @@ const CompilationEvent_1 = __importDefault(require("./event/CompilationEvent"));
22
22
  const FileEvent_1 = __importDefault(require("./event/FileEvent"));
23
23
  const FileLaneUtil_1 = __importDefault(require("./utils/FileLaneUtil"));
24
24
  const IChangedFile_1 = require("./interface/IChangedFile");
25
+ const FileLaneTriggerType_1 = __importDefault(require("./enum/FileLaneTriggerType"));
25
26
  /**
26
27
  * FileLane
27
28
  *
28
29
  * 文件车道,用于将文件进行对应转换,支持1对1、1对N、N对1
29
30
  *
31
+ * 打包过程中,有以下事件
32
+ * 1. 打包成功:onBuildSuccess
33
+ * 2. 打包失败: onBuildError
34
+ * 3. 产生日志: onLog
35
+ *
36
+ * @example
37
+ * ```
38
+ * new FileLane<IJavascriptCompileOption>(
39
+ * fileLaneConfig,
40
+ * projectPath,
41
+ * compilerOption,
42
+ * {
43
+ * onBuildSuccess: (data) => {},
44
+ * onBuildError: (data) => {},
45
+ * onLog: (logs) => {}
46
+ * }).start()
47
+ * ```
48
+ *
30
49
  * @description
31
50
  */
32
51
  class FileLane {
@@ -41,11 +60,19 @@ class FileLane {
41
60
  this.compilerOption = compilerOption;
42
61
  this.events = events;
43
62
  this.changeFileList = [];
44
- this.processExitHandler = () => {
63
+ this.triggerCount = 0;
64
+ this.processBeforeExitHandler = () => {
45
65
  this.dispose();
46
66
  };
67
+ this.sigintHandler = () => __awaiter(this, void 0, void 0, function* () {
68
+ yield this.dispose();
69
+ process.exit();
70
+ });
47
71
  this.context = FileLaneUtil_1.default.createContext(config.output, projectPath);
48
- process.on('exit', this.processExitHandler);
72
+ this.addProcessListener();
73
+ }
74
+ addProcessListener() {
75
+ process.on('beforeExit', this.processBeforeExitHandler);
49
76
  process.on('SIGINT', this.sigintHandler);
50
77
  }
51
78
  /**
@@ -65,10 +92,10 @@ class FileLane {
65
92
  if (!fileList || !fileList.length) {
66
93
  return;
67
94
  }
68
- yield this.build(fileList);
69
95
  if (params === null || params === void 0 ? void 0 : params.watch) {
70
96
  this.watch();
71
97
  }
98
+ yield this.build(fileList, FileLaneTriggerType_1.default.START);
72
99
  });
73
100
  }
74
101
  validateConfig() {
@@ -118,28 +145,26 @@ class FileLane {
118
145
  }
119
146
  return result;
120
147
  }
121
- sigintHandler() {
122
- process.exit();
123
- }
124
- stop() {
125
- process.exit();
126
- }
127
148
  dispose() {
128
- this.complyAfterWork();
129
- if (this.watcher) {
130
- this.watcher.close();
131
- }
132
- this.cleanOutput();
149
+ return __awaiter(this, void 0, void 0, function* () {
150
+ if (this.watcher) {
151
+ this.watcher.removeAllListeners();
152
+ yield this.watcher.close();
153
+ }
154
+ yield this.complyAfterWork();
155
+ yield this.cleanOutput();
156
+ });
133
157
  }
134
158
  /**
135
159
  *
136
160
  * @param fileList 原始文件列表
137
161
  */
138
- build(fileList) {
162
+ build(fileList, trigger) {
139
163
  return __awaiter(this, void 0, void 0, function* () {
140
- const { onBuildSuccess } = this.events || {};
164
+ var _a;
165
+ const { onBuildSuccess, onBuildError } = this.events || {};
141
166
  const t1 = Date.now();
142
- this.initCompilation();
167
+ this.initCompilation(trigger);
143
168
  try {
144
169
  yield this.complyBeforeCompile();
145
170
  this.triggerPlugins(new CompilationEvent_1.default(CompilationEvent_1.default.PROJECT_START));
@@ -153,22 +178,29 @@ class FileLane {
153
178
  this.triggerPlugins(new CompilationEvent_1.default(CompilationEvent_1.default.FLLOW_WORK_START));
154
179
  yield this.complyAfterCompile();
155
180
  this.triggerPlugins(new CompilationEvent_1.default(CompilationEvent_1.default.FLLOW_WORK_END));
156
- onBuildSuccess === null || onBuildSuccess === void 0 ? void 0 : onBuildSuccess({ costTime: Date.now() - t1 });
157
- shared_utils_1.ColorConsole.success({
158
- word: 'Success: build finish!',
159
- style: shared_utils_1.ColorConsole.getStyle(shared_utils_1.Loglevel.SUCCESS)
160
- });
181
+ if (onBuildSuccess) {
182
+ onBuildSuccess === null || onBuildSuccess === void 0 ? void 0 : onBuildSuccess({
183
+ costTime: Date.now() - t1,
184
+ info: (_a = this.compilation) === null || _a === void 0 ? void 0 : _a.info
185
+ });
186
+ }
161
187
  }
162
188
  catch (error) {
163
- shared_utils_1.ColorConsole.throw(`ERROR: `, { word: 'build error' }, `, ${error || 'unknown error'}`);
164
- // 结束任务
165
- process.exit();
189
+ if (onBuildError) {
190
+ onBuildError === null || onBuildError === void 0 ? void 0 : onBuildError(error);
191
+ }
192
+ else {
193
+ shared_utils_1.ColorConsole.throw(`ERROR: `, { word: 'build error' }, `, ${error || 'unknown error'}`);
194
+ }
166
195
  }
167
196
  });
168
197
  }
169
- initCompilation() {
198
+ initCompilation(trigger) {
170
199
  const { plugins } = this.config;
171
200
  const compilation = new FileLaneCompilation_1.default();
201
+ compilation.trigger = trigger;
202
+ compilation.triggerCount = ++this.triggerCount;
203
+ compilation.info.trigger = trigger;
172
204
  plugins === null || plugins === void 0 ? void 0 : plugins.forEach((item) => {
173
205
  item.compilation = compilation;
174
206
  item.apply();
@@ -205,11 +237,44 @@ class FileLane {
205
237
  }
206
238
  }
207
239
  });
208
- result = yield this.runLoader(result, item);
240
+ const loader = new item();
241
+ result = yield this.runLoader(result, loader);
242
+ if (loader.logs) {
243
+ this.handlerLogs(loader.logs, () => {
244
+ throw new Error(`loader error: ${item.name}`);
245
+ });
246
+ }
209
247
  }
210
248
  return result;
211
249
  });
212
250
  }
251
+ /**
252
+ * 处理日志
253
+ * 1. 触发onLog事件
254
+ * 2. 如果有错误日志,则抛出错误
255
+ * @param logs
256
+ * @param onError
257
+ * @returns
258
+ */
259
+ handlerLogs(logs, onError) {
260
+ var _a;
261
+ if (!(logs === null || logs === void 0 ? void 0 : logs.length)) {
262
+ return;
263
+ }
264
+ const onLog = (_a = this.events) === null || _a === void 0 ? void 0 : _a.onLog;
265
+ if (onLog) {
266
+ onLog(logs);
267
+ }
268
+ else {
269
+ logs.forEach((item) => {
270
+ shared_utils_1.ColorConsole.logger(item);
271
+ });
272
+ }
273
+ const hasError = logs.find((item) => item.level && [shared_utils_1.Loglevel.ERROR, shared_utils_1.Loglevel.THROW].includes(item.level));
274
+ if (hasError) {
275
+ onError();
276
+ }
277
+ }
213
278
  writeFiles(fileList) {
214
279
  return __awaiter(this, void 0, void 0, function* () {
215
280
  if (!fileList || !fileList.length) {
@@ -227,9 +292,8 @@ class FileLane {
227
292
  }));
228
293
  });
229
294
  }
230
- runLoader(fileList, loaderType) {
295
+ runLoader(fileList, loader) {
231
296
  return __awaiter(this, void 0, void 0, function* () {
232
- const loader = new loaderType();
233
297
  loader.context = this.context;
234
298
  loader.compilerOption = this.compilerOption;
235
299
  return loader.parser(fileList);
@@ -325,7 +389,6 @@ class FileLane {
325
389
  }
326
390
  catch (error) {
327
391
  shared_utils_1.ColorConsole.throw(`FollowWork: ${item.workerDescribe} error, ${error}`);
328
- process.exit();
329
392
  }
330
393
  }
331
394
  }
@@ -340,7 +403,7 @@ class FileLane {
340
403
  : this.collectFile();
341
404
  // 开始编译前,记录列表置空
342
405
  this.changeFileList = [];
343
- yield this.build(fileList);
406
+ yield this.build(fileList, FileLaneTriggerType_1.default.UPDATE);
344
407
  });
345
408
  this.listenFileChange(onChange);
346
409
  });
@@ -422,7 +485,7 @@ class FileLane {
422
485
  */
423
486
  cleanOutput() {
424
487
  return __awaiter(this, void 0, void 0, function* () {
425
- shared_utils_1.FileUtil.del(FileLaneUtil_1.default.getOutputPath(this.context));
488
+ return shared_utils_1.FileUtil.del(FileLaneUtil_1.default.getOutputPath(this.context));
426
489
  });
427
490
  }
428
491
  /**
@@ -1,3 +1,4 @@
1
+ import FileLaneTriggerType from './enum/FileLaneTriggerType';
1
2
  import AsyncEventDispatcher from './event/asyncEvent/AsyncEventDispatcher';
2
3
  import IFileParam from './interface/IFileParam';
3
4
  /**
@@ -5,6 +6,18 @@ import IFileParam from './interface/IFileParam';
5
6
  */
6
7
  declare class FileLaneCompilation extends AsyncEventDispatcher {
7
8
  buildFileList: IFileParam<any>[];
8
- diffJson: string[];
9
+ /**
10
+ * 触发方式
11
+ *
12
+ */
13
+ trigger: FileLaneTriggerType;
14
+ /**
15
+ * 触发次数
16
+ */
17
+ triggerCount: number;
18
+ info: {
19
+ trigger: FileLaneTriggerType;
20
+ [key: string]: any;
21
+ };
9
22
  }
10
23
  export default FileLaneCompilation;
@@ -3,6 +3,7 @@ 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
+ const FileLaneTriggerType_1 = __importDefault(require("./enum/FileLaneTriggerType"));
6
7
  const AsyncEventDispatcher_1 = __importDefault(require("./event/asyncEvent/AsyncEventDispatcher"));
7
8
  /**
8
9
  * FileLaneCompilation
@@ -11,7 +12,18 @@ class FileLaneCompilation extends AsyncEventDispatcher_1.default {
11
12
  constructor() {
12
13
  super(...arguments);
13
14
  this.buildFileList = [];
14
- this.diffJson = [];
15
+ /**
16
+ * 触发方式
17
+ *
18
+ */
19
+ this.trigger = FileLaneTriggerType_1.default.START;
20
+ /**
21
+ * 触发次数
22
+ */
23
+ this.triggerCount = 0;
24
+ this.info = {
25
+ trigger: FileLaneTriggerType_1.default.START
26
+ };
15
27
  }
16
28
  }
17
29
  exports.default = FileLaneCompilation;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * FileLaneTriggerType
3
+ */
4
+ declare enum FileLaneTriggerType {
5
+ START = 1,
6
+ UPDATE = 2
7
+ }
8
+ export default FileLaneTriggerType;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * FileLaneTriggerType
5
+ */
6
+ var FileLaneTriggerType;
7
+ (function (FileLaneTriggerType) {
8
+ FileLaneTriggerType[FileLaneTriggerType["START"] = 1] = "START";
9
+ FileLaneTriggerType[FileLaneTriggerType["UPDATE"] = 2] = "UPDATE";
10
+ })(FileLaneTriggerType || (FileLaneTriggerType = {}));
11
+ exports.default = FileLaneTriggerType;
@@ -103,6 +103,7 @@ export default interface IFileLaneConfig<O = any> {
103
103
  * 配置watch时忽略的文件或者文件夹
104
104
  */
105
105
  watchIgnores?: MatchType;
106
+ projectPath: string;
106
107
  }
107
108
  type FollowWoker<O> = {
108
109
  worker: FollowWork<O>;
@@ -0,0 +1,52 @@
1
+ import { ILog } from '@aiot-toolkit/shared-utils/lib/interface/ILog';
2
+ import FileLaneTriggerType from '../enum/FileLaneTriggerType';
3
+ /**
4
+ * IFileLaneEvents
5
+ */
6
+ export default interface IFileLaneEvents {
7
+ /**
8
+ * 转换成功
9
+ * @param data
10
+ * @returns
11
+ */
12
+ onBuildSuccess?: (data: IFileLaneSuccessData) => void;
13
+ /**
14
+ * 转换失败
15
+ * @default 错误消息打印到控制台
16
+ * @param error
17
+ * @returns
18
+ */
19
+ onBuildError?: (error: unknown) => void;
20
+ /**
21
+ * 日志触发的方法,打包过程中可能多次触发
22
+ * @default 日志打印到控制台
23
+ * @param logs
24
+ * @returns
25
+ */
26
+ onLog?: (logs: ILog[]) => void;
27
+ }
28
+ export interface IFileLaneSuccessData {
29
+ /**
30
+ * 耗时(毫秒)
31
+ */
32
+ costTime: number;
33
+ info?: {
34
+ /**
35
+ * 触发类型
36
+ */
37
+ trigger: FileLaneTriggerType;
38
+ /**
39
+ * rpk 文件绝对路径
40
+ */
41
+ rpk?: string;
42
+ /**
43
+ * 差异文件列表
44
+ */
45
+ diffList?: string[];
46
+ /**
47
+ * 差异文件压缩包
48
+ */
49
+ diffFile?: string;
50
+ [key: string]: any;
51
+ };
52
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,4 @@
1
+ import { ILog } from '@aiot-toolkit/shared-utils/lib/interface/ILog';
1
2
  import IFileLaneContext from './IFileLaneContext';
2
3
  import IFileParam from './IFileParam';
3
4
  /**
@@ -7,4 +8,10 @@ export default interface ILoader {
7
8
  context?: IFileLaneContext;
8
9
  compilerOption?: any;
9
10
  parser(files: IFileParam[]): Promise<IFileParam[]> | IFileParam[];
11
+ /**
12
+ * 日志列表
13
+ *
14
+ * 其中有 `THROW` `ERROR` 时,会中断打包,并触发 `onBuildError` 事件
15
+ */
16
+ logs?: ILog[];
10
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "file-lane",
3
- "version": "2.0.2",
3
+ "version": "2.0.3-beta.2",
4
4
  "description": "File conversion tool, can be one-to-one, one to N, N to one",
5
5
  "keywords": [
6
6
  "file",
@@ -20,7 +20,7 @@
20
20
  "test": "node ./__tests__/file-lane.test.js"
21
21
  },
22
22
  "dependencies": {
23
- "@aiot-toolkit/shared-utils": "2.0.2",
23
+ "@aiot-toolkit/shared-utils": "2.0.3-beta.2",
24
24
  "chokidar": "^3.6.0",
25
25
  "fs-extra": "^11.2.0",
26
26
  "lodash": "^4.17.21"
@@ -28,5 +28,5 @@
28
28
  "devDependencies": {
29
29
  "@types/fs-extra": "^11.0.4"
30
30
  },
31
- "gitHead": "8ca4bf2ed7bcf11911e5c514696951bdba4fc12d"
31
+ "gitHead": "7776973f6a81d7f13eb330be8df5801d9143c83c"
32
32
  }