forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
287 lines (261 loc) · 10 KB
/
cli.ts
File metadata and controls
287 lines (261 loc) · 10 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { program, CommandOptions } from "commander"
import { annotate } from "./annotate"
import { build } from "./build"
import { crunScript } from "./crun"
import { ctool } from "./ctool"
import { deployScript } from "./deploy"
import { devtools } from "./devtools"
import { disasm } from "./disasm"
import { init } from "./init"
import { logParse } from "./logparse"
import { runScript } from "./run"
import { compileFlagHelp } from "@devicescript/compiler"
import { startVm } from "./vm"
import { cliVersion } from "./version"
import { dcfg } from "./dcfg"
import { setConsoleColors, setVerbose } from "./command"
import { binPatch } from "./binpatch"
import {
boardNames,
flashAuto,
flashESP32,
flashRP2040,
setupFlashBoards,
} from "./flash"
import { addBoard } from "./addboard"
export async function mainCli() {
Error.stackTraceLimit = 30
function buildCommand(nameAndArgs: string, opts?: CommandOptions) {
return program
.command(nameAndArgs, opts)
.option("-s, --stats", "show additional size information")
.option("-o, --out-dir", "output directory, default is 'built'")
.option("--no-verify", "don't verify resulting bytecode")
.option(
"-F, --flag <compiler-flag>",
"set compiler flag",
(val, prev: Record<string, boolean>) => {
if (!compileFlagHelp[val])
throw new Error(`invalid compiler flag: '${val}'`)
prev[val] = true
return prev
},
{}
)
}
program
.name("DeviceScript")
.description(
"build and run DeviceScript program https://aka.ms/devicescript"
)
.version(cliVersion())
.option("-v, --verbose", "more logging")
.option("--no-colors", "disable color output")
buildCommand("build", { isDefault: true })
.description("build a DeviceScript file")
.arguments("[file.ts]")
.action(build)
program
.command("flags")
.description("show description of compiler flags")
.action(() => {
function pad(s: string, n: number) {
while (s.length < n) s += " "
return s
}
for (const k of Object.keys(compileFlagHelp)) {
console.log(` -F ${pad(k, 20)} ${compileFlagHelp[k]}`)
}
})
program
.command("init")
.description("creates or configures a devicescript project")
.option("-f, --force", "force overwrite existing files")
.option("--spaces <number>", "number of spaces when generating JSON")
.option(
"--install",
"Run npm install or yarn install after creating files"
)
.argument("[dir]", "path to create the project", "./")
.action(init)
program
.command("devtools")
.description("launches a local development tools server")
.option("--internet", "allow connections from non-localhost")
.option(
"--localhost",
"use localhost:8000 instead of the internet dashboard"
)
.option("-l, --logging", "print out device log messages as they come")
.option("-t, --trace <string>", "save all packets to named file")
.option("-u, --usb", "listen to Jacdac over USB (requires usb)")
.option(
"-s, --serial",
"listen to Jacdac over SERIAL (requires serialport)"
)
.option(
"-i, --spi",
"listen to Jacdac over SPI (requires rpio, experimental)"
)
.option(
"--vscode",
"update behavior to match executing within Visual Studio Code"
)
.option("--diagnostics", "enable Jacdac-ts diagnostics")
.arguments("[file.ts]")
.action(devtools)
program
.command("ctool", { hidden: true })
.description("access to internal compilation tools")
.option("--empty", "generate empty program embed")
.option("-t, --test", "run compiler tests")
.option("--fetch-boards <boards.json>", "re-create boards.json file")
.option("--local-boards <repos-path>", "use local, not remote info.json files")
.action(ctool)
program
.command("logparse", { hidden: true })
.description("parse binary log file from SD card")
.option(
"-g, --generation <number>",
"give details for a specific generation; otherwise generations are just listed"
)
.option("-s, --stats", "only print stats, not content")
.arguments("<log_xxx.jdl>")
.action(logParse)
buildCommand("run")
.description("run a script")
.option(
"--tcp",
"use tcp jacdac proxy on 127.0.0.1:8082 (otherwise ws://127.0.0.1:8081)"
)
.option("-t, --test", "run in test mode (no sockets, no restarts)")
.option(
"-T, --test-timeout <milliseconds>",
"set timeout for --test mode (default: 2000ms)"
)
.option("-w, --wait", "wait for external deploy")
.arguments("[file.ts|file.devs]")
.action(runScript)
// this talks direct jacdac packets, it doesn't work with current devtools (since they do not forward jacdac packets)
// hide it until we have a better story
program
.command("deploy", { hidden: true })
.description("deploy a script over jacdac proxy")
.option(
"--tcp",
"use tcp jacdac proxy on 127.0.0.1:8082 (otherwise ws://127.0.0.1:8081)"
)
.arguments("<file.ts|file.devs>")
.action(deployScript)
program
.command("vm")
.description("start DeviceScript VM interpreter process")
.option(
"--tcp",
"use tcp jacdac proxy on 127.0.0.1:8082 (otherwise ws://127.0.0.1:8081)"
)
.option("--gc-stress", "stress-test the GC")
.option("--device-id <string>", "set device ID")
.option("--devtools", "set when spawned from devtools")
.action(startVm)
buildCommand("crun", { hidden: true })
.description("run a script using native runner")
.option("-n, --net", "connect to 127.0.0.1:8082 for Jacdac proxy")
.option(
"--lazy-gc",
"only run GC when full (otherwise run on every allocation for stress-test)"
)
.option(
"--settings",
"load/save settings from files (otherwise in memory only)"
)
.option(
"-s, --serial <serial-port>",
"connect to serial port, not 127.0.0.1:8082"
)
.arguments("<file.ts|file.devs>")
.action(crunScript)
program
.command("disasm")
.description("disassemble .devs binary")
.option("-d, --detailed", "include all details")
.arguments("[file.ts|file-dbg.json|file.devs]")
.action(disasm)
program
.command("annotate")
.description("annotate stack frames in stdin")
.action(annotate)
program
.command("dcfg", { hidden: true })
.description("compile/decompile DCFG files")
.option(
"-u, --update <file.c>",
"update given C file with compiled output"
)
.option("-o, --output <file.bin>", "specify output file name")
.arguments("<file.json|file.bin>")
.action(dcfg)
const flash = program.command("flash")
function addFlashCmd(arch: string) {
const r = arch ? flash.command(arch) : flash
r.description(
arch
? `flash with ${arch.toUpperCase()}-specific parameters`
: `flash DeviceScript runtime (interpreter/VM)`
)
r.option("-b, --board <board-id>", "specify board to flash")
r.option("--once", "do not wait for the board to be connected")
r.addHelpText("after", () => {
setupFlashBoards()
return `\nAvailable boards:\n` + boardNames(arch)
})
return r
}
addFlashCmd("").action(flashAuto)
addFlashCmd("esp32")
.option("--all-serial", "do not filter serial ports by vendor")
.option(
"--baud <rate>",
"specify speed of serial port (default: 1500000)"
)
.option("--port <path>", "specify port")
.option("--esptool <path>", "explicitly specify path to esptool.py")
.action(flashESP32)
addFlashCmd("rp2040").action(flashRP2040)
program
.command("addboard")
.description("fork a board configuration for a new board")
.option("-B, --base <board-id>", "ID of a board to fork (required)")
.option("-n, --name <board-name>", "new board name (required)")
.option(
"-b, --board <board-id>",
"new board ID (auto-generated from name)"
)
.option("--force", "overwrite JSON config file")
.action(addBoard)
program
.command("binpatch", { hidden: true })
.description("patch an interpreter binary with board configuration")
.option("--uf2 <file.uf2>", "interpreter binary in UF2 format")
.option("--bin <file.bin>", "interpreter binary in BIN format")
.option(
"--esp <file.bin>",
"interpreter binary as a combined ESP32 image (with bootloader and partition table)"
)
.option(
"-o, --outdir <folder>",
"specify output directory, default to 'dist'"
)
.option(
"--generic",
"copy the uf2/bin file and corresponding ELF file as 'generic' variant"
)
.option("--slug <string>", "repo slug (eg. microsoft/devicescript)")
.option("--elf <file.elf>", "specify ELF file name")
.arguments("<file.board.json...>")
.action(binPatch)
program.on("option:verbose", () => setVerbose(true))
program.on("option:no-colors", () => setConsoleColors(false))
program.parse(process.argv)
}
if (require.main === module) mainCli()