forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ts
More file actions
127 lines (115 loc) · 3.46 KB
/
run.ts
File metadata and controls
127 lines (115 loc) · 3.46 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { DebugInfo, checkMagic } from "@devicescript/compiler"
import { readFileSync } from "node:fs"
import { compileFile, devsFactory, devsStartWithNetwork } from "./build"
import { error } from "./command"
import { BuildOptions } from "./sideprotocol"
export interface RunOptions {
test?: boolean
wait?: boolean
tcp?: boolean
testTimeout?: string
testSelfExit?: boolean
}
export async function readCompiled(
fn: string,
options: BuildOptions = {}
): Promise<{
binary: Uint8Array
dbg?: DebugInfo
settings?: Record<string, Uint8Array>
}> {
const buf = readFileSync(fn)
if (checkMagic(buf)) return { binary: buf }
if (
checkMagic(
Buffer.from(
buf.subarray(0, 16).toString("binary").replace(/\s*/g, ""),
"hex"
)
)
)
return {
binary: Buffer.from(
buf.toString("binary").replace(/\s*/g, ""),
"hex"
),
}
if (buf[0] == 0x7b) {
const dbg = JSON.parse(buf.toString("utf-8")) as DebugInfo
return { dbg, binary: Buffer.from(dbg.binary.hex, "hex") }
}
const res = await compileFile(fn, options)
if (!res.success) process.exit(1)
return { binary: res.binary, dbg: res.dbg, settings: res.settings }
}
export async function runTest(
fn: string,
options: RunOptions & BuildOptions = {}
) {
if (!options.flag) options.flag = {}
if (!options.testSelfExit) options.flag.testHarness = true
const prog = await readCompiled(fn, options)
const inst = await devsFactory()
const devid = "12abdd2289421234"
return new Promise<void>((resolve, reject) => {
inst.sendPacket = () => {}
inst.panicHandler = panic_code => {
inst.devsStop()
if (panic_code) {
console.log("test failed")
resolve = null
reject(new Error("test failed"))
} else {
console.log("test OK")
const f = resolve
resolve = null
if (f) f()
}
}
inst.devsStop()
inst.devsGcStress(true)
inst.devsSetDeviceId(devid)
inst.devsStart()
inst.devsDeploy(prog.binary)
inst.error = (...data) => {
error(...data)
process.exit(1)
}
setTimeout(() => {
if (resolve) {
inst.devsStop()
console.log("timeout")
reject(new Error("timeout"))
}
}, parseInt(options.testTimeout) || 5000)
})
}
function err(msg: string) {
error("Error: " + msg)
process.exit(1)
}
export async function runScript(
fn: string,
options: RunOptions & BuildOptions
) {
if (options.test && options.wait)
err("can't use --test and --wait together")
if (options.test && !fn) err("--test require file name")
if (!options.wait && !fn) err("need either --wait or file name")
if (options.test) {
try {
await runTest(fn, options)
process.exit(0)
} catch (e) {
err(e?.message)
}
}
const inst = await devsStartWithNetwork(options)
if (fn) {
const prog = await readCompiled(fn, options)
const r = inst.devsDeploy(prog.binary)
if (r) throw new Error("deploy error: " + r)
console.log(`self-deployed ${fn}`)
}
if (options.wait) console.log(`waiting for external deploy`)
}