-
Notifications
You must be signed in to change notification settings - Fork 148
/
compile.ts
73 lines (69 loc) · 2.46 KB
/
compile.ts
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
import { ICompileIPCResult } from "@dataform/api/vm/compile";
import { validate } from "@dataform/core/utils";
import { dataform } from "@dataform/protos";
import { ChildProcess, fork } from "child_process";
import * as fs from "fs";
import * as path from "path";
export async function compile(
compileConfig: dataform.ICompileConfig
): Promise<dataform.CompiledGraph> {
// Resolve the path in case it hasn't been resolved already.
path.resolve(compileConfig.projectDir);
const returnedPath = await CompileChildProcess.forkProcess().compile(compileConfig);
const contents = fs.readFileSync(returnedPath);
let compiledGraph = dataform.CompiledGraph.decode(contents);
fs.unlinkSync(returnedPath);
// Merge graph errors into the compiled graph.
compiledGraph = dataform.CompiledGraph.create({
...compiledGraph,
graphErrors: validate(compiledGraph)
});
return compiledGraph;
}
class CompileChildProcess {
public static forkProcess() {
// Run the bin_loader script if inside bazel, otherwise don't.
const forkScript = process.env.BAZEL_TARGET ? "../vm/compile_bin_loader" : "../vm/compile";
return new CompileChildProcess(fork(require.resolve(forkScript)));
}
private readonly childProcess: ChildProcess;
constructor(childProcess: ChildProcess) {
this.childProcess = childProcess;
}
public async compile(compileConfig: dataform.ICompileConfig) {
const compileInChildProcess = new Promise<string>(async (resolve, reject) => {
this.childProcess.on("message", (result: ICompileIPCResult) => {
if (result.err) {
//console.log("And my error is: " + result.err);
reject(new Error(result.err));
} else {
// We receive back a path where the compiled graph was written in proto format.
resolve(result.path);
}
});
/*console.log(
"[compile.ts 58] + compileConfig= " +
compileConfig.projectDir +
", " +
compileConfig.schemaSuffixOverride
);*/
this.childProcess.send(compileConfig);
});
let timer;
const timeout = new Promise(
(resolve, reject) =>
(timer = setTimeout(() => reject(new Error("Compilation timed out")), 5000))
);
try {
await Promise.race([timeout, compileInChildProcess]);
return await compileInChildProcess;
} finally {
if (!this.childProcess.killed) {
this.childProcess.kill();
}
if (timer) {
clearTimeout(timer);
}
}
}
}