file-lane 2.0.2 → 2.0.3-beta.1
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 +35 -7
- package/lib/FileLane.js +90 -19
- package/lib/FileLaneCompilation.d.ts +14 -1
- package/lib/FileLaneCompilation.js +13 -1
- package/lib/enum/FileLaneTriggerType.d.ts +8 -0
- package/lib/enum/FileLaneTriggerType.js +11 -0
- package/lib/interface/IFileLaneConfig.d.ts +1 -0
- package/lib/interface/IFileLaneEvents.d.ts +52 -0
- package/lib/interface/IFileLaneEvents.js +2 -0
- package/lib/interface/ILoader.d.ts +7 -0
- package/package.json +3 -3
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,19 @@ 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);
|
|
28
47
|
/**
|
|
29
48
|
* 运行
|
|
30
49
|
* @param params
|
|
@@ -41,8 +60,8 @@ declare class FileLane<O = any> {
|
|
|
41
60
|
private validateConfig;
|
|
42
61
|
private processExitHandler;
|
|
43
62
|
private sigintHandler;
|
|
44
|
-
stop(): void
|
|
45
|
-
dispose(): void
|
|
63
|
+
stop(): Promise<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,6 +60,7 @@ class FileLane {
|
|
|
41
60
|
this.compilerOption = compilerOption;
|
|
42
61
|
this.events = events;
|
|
43
62
|
this.changeFileList = [];
|
|
63
|
+
this.triggerCount = 0;
|
|
44
64
|
this.processExitHandler = () => {
|
|
45
65
|
this.dispose();
|
|
46
66
|
};
|
|
@@ -65,10 +85,10 @@ class FileLane {
|
|
|
65
85
|
if (!fileList || !fileList.length) {
|
|
66
86
|
return;
|
|
67
87
|
}
|
|
68
|
-
yield this.build(fileList);
|
|
69
88
|
if (params === null || params === void 0 ? void 0 : params.watch) {
|
|
70
89
|
this.watch();
|
|
71
90
|
}
|
|
91
|
+
yield this.build(fileList, FileLaneTriggerType_1.default.START);
|
|
72
92
|
});
|
|
73
93
|
}
|
|
74
94
|
validateConfig() {
|
|
@@ -122,24 +142,30 @@ class FileLane {
|
|
|
122
142
|
process.exit();
|
|
123
143
|
}
|
|
124
144
|
stop() {
|
|
125
|
-
|
|
145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
+
if (this.watcher) {
|
|
147
|
+
this.watcher.removeAllListeners();
|
|
148
|
+
yield this.watcher.close();
|
|
149
|
+
}
|
|
150
|
+
});
|
|
126
151
|
}
|
|
127
152
|
dispose() {
|
|
128
|
-
this
|
|
129
|
-
|
|
130
|
-
this.
|
|
131
|
-
|
|
132
|
-
|
|
153
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
154
|
+
this.complyAfterWork();
|
|
155
|
+
yield this.stop();
|
|
156
|
+
this.cleanOutput();
|
|
157
|
+
});
|
|
133
158
|
}
|
|
134
159
|
/**
|
|
135
160
|
*
|
|
136
161
|
* @param fileList 原始文件列表
|
|
137
162
|
*/
|
|
138
|
-
build(fileList) {
|
|
163
|
+
build(fileList, trigger) {
|
|
139
164
|
return __awaiter(this, void 0, void 0, function* () {
|
|
140
|
-
|
|
165
|
+
var _a;
|
|
166
|
+
const { onBuildSuccess, onBuildError } = this.events || {};
|
|
141
167
|
const t1 = Date.now();
|
|
142
|
-
this.initCompilation();
|
|
168
|
+
this.initCompilation(trigger);
|
|
143
169
|
try {
|
|
144
170
|
yield this.complyBeforeCompile();
|
|
145
171
|
this.triggerPlugins(new CompilationEvent_1.default(CompilationEvent_1.default.PROJECT_START));
|
|
@@ -153,22 +179,35 @@ class FileLane {
|
|
|
153
179
|
this.triggerPlugins(new CompilationEvent_1.default(CompilationEvent_1.default.FLLOW_WORK_START));
|
|
154
180
|
yield this.complyAfterCompile();
|
|
155
181
|
this.triggerPlugins(new CompilationEvent_1.default(CompilationEvent_1.default.FLLOW_WORK_END));
|
|
156
|
-
onBuildSuccess === null || onBuildSuccess === void 0 ? void 0 : 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
|
+
});
|
|
157
186
|
shared_utils_1.ColorConsole.success({
|
|
158
187
|
word: 'Success: build finish!',
|
|
159
188
|
style: shared_utils_1.ColorConsole.getStyle(shared_utils_1.Loglevel.SUCCESS)
|
|
160
189
|
});
|
|
161
190
|
}
|
|
162
191
|
catch (error) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
192
|
+
if (onBuildError) {
|
|
193
|
+
onBuildError === null || onBuildError === void 0 ? void 0 : onBuildError(error);
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
shared_utils_1.ColorConsole.throw(`ERROR: `, { word: 'build error' }, `, ${error || 'unknown error'}`);
|
|
197
|
+
}
|
|
198
|
+
// 非监听模式,则结束任务
|
|
199
|
+
if (!this.watcher) {
|
|
200
|
+
process.exit();
|
|
201
|
+
}
|
|
166
202
|
}
|
|
167
203
|
});
|
|
168
204
|
}
|
|
169
|
-
initCompilation() {
|
|
205
|
+
initCompilation(trigger) {
|
|
170
206
|
const { plugins } = this.config;
|
|
171
207
|
const compilation = new FileLaneCompilation_1.default();
|
|
208
|
+
compilation.trigger = trigger;
|
|
209
|
+
compilation.triggerCount = ++this.triggerCount;
|
|
210
|
+
compilation.info.trigger = trigger;
|
|
172
211
|
plugins === null || plugins === void 0 ? void 0 : plugins.forEach((item) => {
|
|
173
212
|
item.compilation = compilation;
|
|
174
213
|
item.apply();
|
|
@@ -205,11 +244,44 @@ class FileLane {
|
|
|
205
244
|
}
|
|
206
245
|
}
|
|
207
246
|
});
|
|
208
|
-
|
|
247
|
+
const loader = new item();
|
|
248
|
+
result = yield this.runLoader(result, loader);
|
|
249
|
+
if (loader.logs) {
|
|
250
|
+
this.handlerLogs(loader.logs, () => {
|
|
251
|
+
throw new Error(`loader error: ${item.name}`);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
209
254
|
}
|
|
210
255
|
return result;
|
|
211
256
|
});
|
|
212
257
|
}
|
|
258
|
+
/**
|
|
259
|
+
* 处理日志
|
|
260
|
+
* 1. 触发onLog事件
|
|
261
|
+
* 2. 如果有错误日志,则抛出错误
|
|
262
|
+
* @param logs
|
|
263
|
+
* @param onError
|
|
264
|
+
* @returns
|
|
265
|
+
*/
|
|
266
|
+
handlerLogs(logs, onError) {
|
|
267
|
+
var _a;
|
|
268
|
+
if (!(logs === null || logs === void 0 ? void 0 : logs.length)) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
const onLog = (_a = this.events) === null || _a === void 0 ? void 0 : _a.onLog;
|
|
272
|
+
if (onLog) {
|
|
273
|
+
onLog(logs);
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
logs.forEach((item) => {
|
|
277
|
+
shared_utils_1.ColorConsole.logger(item);
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
const hasError = logs.find((item) => item.level && [shared_utils_1.Loglevel.ERROR, shared_utils_1.Loglevel.THROW].includes(item.level));
|
|
281
|
+
if (hasError) {
|
|
282
|
+
onError();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
213
285
|
writeFiles(fileList) {
|
|
214
286
|
return __awaiter(this, void 0, void 0, function* () {
|
|
215
287
|
if (!fileList || !fileList.length) {
|
|
@@ -227,9 +299,8 @@ class FileLane {
|
|
|
227
299
|
}));
|
|
228
300
|
});
|
|
229
301
|
}
|
|
230
|
-
runLoader(fileList,
|
|
302
|
+
runLoader(fileList, loader) {
|
|
231
303
|
return __awaiter(this, void 0, void 0, function* () {
|
|
232
|
-
const loader = new loaderType();
|
|
233
304
|
loader.context = this.context;
|
|
234
305
|
loader.compilerOption = this.compilerOption;
|
|
235
306
|
return loader.parser(fileList);
|
|
@@ -340,7 +411,7 @@ class FileLane {
|
|
|
340
411
|
: this.collectFile();
|
|
341
412
|
// 开始编译前,记录列表置空
|
|
342
413
|
this.changeFileList = [];
|
|
343
|
-
yield this.build(fileList);
|
|
414
|
+
yield this.build(fileList, FileLaneTriggerType_1.default.UPDATE);
|
|
344
415
|
});
|
|
345
416
|
this.listenFileChange(onChange);
|
|
346
417
|
});
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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,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;
|
|
@@ -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
|
+
}
|
|
@@ -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.
|
|
3
|
+
"version": "2.0.3-beta.1",
|
|
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.
|
|
23
|
+
"@aiot-toolkit/shared-utils": "2.0.3-beta.1",
|
|
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": "
|
|
31
|
+
"gitHead": "77b1b0aab9b5c227b5cc355b585a7c93761cc9a3"
|
|
32
32
|
}
|