forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrun.ts
More file actions
68 lines (58 loc) · 2.02 KB
/
crun.ts
File metadata and controls
68 lines (58 loc) · 2.02 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
import { ensureDir } from "fs-extra"
import { spawn } from "node:child_process"
import { writeFileSync } from "node:fs"
import { resolve } from "node:path"
import { createInterface } from "node:readline"
import { readDebugInfo } from "./build"
import { BINDIR, error, log } from "./command"
import { readCompiled } from "./run"
import { BuildOptions } from "./sideprotocol"
import { printDmesg } from "./vmworker"
export interface CRunOptions {
net?: boolean
serial?: string
lazyGc?: boolean
settings?: boolean
testSelfExit?: boolean
}
export async function crunScript(
fn: string,
options: CRunOptions & BuildOptions
) {
options.verify = false
if (!options.flag) options.flag = {}
const compfn = BINDIR + "/crun.devs"
const args = [compfn]
if (options.serial) args.unshift(options.serial)
else if (options.net) args.unshift("8082", "-w")
else if (!options.testSelfExit) options.flag.testHarness = true
const prog = await readCompiled(fn, options)
await ensureDir(BINDIR)
writeFileSync(compfn, prog.binary)
if (!options.lazyGc) args.unshift("-X")
if (!options.settings) args.unshift("-n")
const executable = resolve(__dirname, "../../runtime/built/jdcli")
log(`run: ${executable} ${args.join(" ")}`)
const child = spawn(executable, args, {
stdio: ["inherit", "pipe", "pipe"],
})
let recentLines: string[] = []
const dbg = readDebugInfo()
createInterface({ input: child.stdout }).on("line", line => {
if (!printDmesg(dbg, "C", line)) {
recentLines.push(line)
if (recentLines.length > 100) recentLines = recentLines.slice(50)
}
})
createInterface({ input: child.stderr }).on("line", line => {
printDmesg(dbg, "C", "! " + line)
})
child.on("exit", (code, err) => {
if (!code && err) code = 2
if (code) {
log(recentLines.join("\n"))
error(`exit code: ${code} ${err ?? ""}`)
}
setTimeout(() => process.exit(code), 200)
})
}