forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandalone-worker.ts
More file actions
77 lines (62 loc) · 1.8 KB
/
standalone-worker.ts
File metadata and controls
77 lines (62 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* eslint-disable no-console */
import type { ProjectSchema } from '@alilc/lowcode-types';
import CodeGen from './standalone';
declare const self: any;
self.onmessage = (event: any) => {
const msg = event.data || {};
if (msg.type === 'run') {
run(msg);
}
};
self.postMessage({ type: 'ready' });
async function run(msg: { solution: string; schema: ProjectSchema; flattenResult?: boolean }) {
try {
print('begin run...');
self.postMessage({ type: 'run:begin' });
const { solution } = msg;
if (!solution) {
throw new Error('solution is required');
}
const createAppBuilder = CodeGen.solutions[solution as 'icejs' | 'rax'];
if (typeof createAppBuilder !== 'function') {
throw new Error(`solution '${solution}' is invalid`);
}
const appBuilder = createAppBuilder();
print('generating from schema: %o', msg.schema);
const result = await appBuilder.generateProject(msg.schema);
print('generated result: %o', result);
const finalResult = msg.flattenResult
? CodeGen.utils.resultHelper.flattenResult(result)
: result;
if (msg.flattenResult) {
print('flatten result: %o', finalResult);
}
self.postMessage({
type: 'run:end',
result: finalResult,
});
} catch (e) {
printErr(`${e}`);
self.postMessage({
type: 'run:error',
errorMsg: `${(e as Error | null)?.message || e}`,
errorDetail: `${e}`,
});
}
}
function print(text: string, ...args: any[]) {
console.debug(`[code-generator/worker]: ${text}`, ...args);
self.postMessage({
type: 'stdout',
data: text,
args,
});
}
function printErr(text: string, ...args: any[]) {
console.debug(`[code-generator/worker]: %c${text}`, 'color:red', ...args);
self.postMessage({
type: 'stderr',
data: text,
args,
});
}