-
Notifications
You must be signed in to change notification settings - Fork 122
/
build.js
executable file
·318 lines (292 loc) · 10.2 KB
/
build.js
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env node
const esbuild = require("esbuild")
const childProcess = require("child_process")
const path = require("path")
const fs = require("fs-extra")
const { dirname, join, resolve } = require("path")
let watch = false
let fast = false
function getMTime(path) {
try {
const st = fs.statSync(path)
return st.mtimeMs
} catch {
return undefined
}
}
function copyCompiler() {
const to = "website/static/dist/devicescript-compiler.js"
const from = "compiler/built/devicescript-compiler.js"
distCopy(from, to)
}
function copyVM() {
const dist = "website/static/dist/devicescript-vm.js"
const builtDir = "runtime/devicescript-vm/built"
const built = builtDir + "/devicescript-vm.js"
distCopy(dist, built)
}
function distCopy(from, to) {
const toT = getMTime(to)
const fromT = getMTime(from)
if (toT === undefined || fromT > toT + 1) {
console.debug(`cp ${from} ${to}`)
try {
fs.mkdirSync(path.dirname(to))
} catch {}
fs.copyFileSync(from, to)
fs.utimesSync(to, new Date(), new Date(fromT))
}
}
const args = process.argv.slice(2)
if (args[0] == "--watch" || args[0] == "-watch" || args[0] == "-w") {
args.shift()
watch = true
}
if (args[0] == "--fast" || args[0] == "-fast" || args[0] == "-f") {
args.shift()
fast = true
}
if (args.length) {
console.log("Usage: ./build.js [--watch] [--fast]")
process.exit(1)
}
function runTSC(args) {
return new Promise((resolve, reject) => {
let invoked = false
if (watch) args.push("--watch", "--preserveWatchOutput")
console.log("run tsc " + args.join(" "))
let tscPath = "node_modules/typescript/lib/tsc.js"
const process = childProcess.fork(tscPath, args)
process.on("error", err => {
if (invoked) return
invoked = true
reject(err)
})
process.on("exit", code => {
if (invoked) return
invoked = true
if (code == 0) resolve()
else reject(new Error("exit " + code))
})
// in watch mode "go in background"
if (watch)
setTimeout(() => {
if (invoked) return
invoked = true
resolve()
}, 500)
})
}
const files = {
"interop/built/devicescript-interop.mjs": "interop/src/interop.ts",
"interop/built/devicescript-interop.node.cjs": "interop/src/interop.ts",
"compiler/built/devicescript-compiler.js": "compiler/src/devicescript.ts",
"compiler/built/devicescript-compiler.node.cjs":
"compiler/src/devicescript.ts",
"plugin/built/devicescript-plugin.cjs": "plugin/src/plugin.ts",
"dap/built/devicescript-dap.cjs": "dap/src/dsdap.ts",
"cli/built/devicescript-cli.cjs": "cli/src/cli.ts",
"vscode/built/devicescript-vscode.js": "vscode/src/extension.ts",
}
function strcmp(a, b) {
return a < b ? -1 : a > b ? 1 : 0
}
function filesInDir(fn) {
const r = fs.readdirSync(fn)
r.sort(strcmp)
return r
}
const genspecs = ["devicescript-spec.d.ts", "devicescript-boards.d.ts"]
const serversname = "core/src/devicescript-servers.d.ts"
function buildPrelude(folder, outp) {
// get the srvcfg.d.ts extension from archconfig.ts
let additions = fs
.readFileSync("interop/src/archconfig.ts", "utf-8")
.replace(/[^]*^\/\/ BEGIN-DS-SERVERS.*/m, "")
.replace(/^\/\/ END-DS-SERVERS[^]*/m, "")
let srvcfg = fs.readFileSync("runtime/jacdac-c/dcfg/srvcfg.d.ts", "utf-8")
// no reason to encode hex number as strings in full TS syntax
srvcfg = srvcfg
.replace(
"type HexInt = integer | string",
"type HexInt = integer" + "\n" + additions
)
.replace(/type \w*Pin = .*/g, "")
.replace("/srvcfg", "/servers")
.replace(
/(interface BaseServiceConfig[^}]* name)(: string)/,
(_, a, b) => a + "?" + b
)
.replace(/service: /g, `service?: `)
const m = /^\s*type ServiceConfig([^{]+)/m.exec(srvcfg)
let startServ = `\n import * as ds from "@devicescript/core"\n`
startServ += ` import { Pin, InputPin, OutputPin, IOPin, AnalogInPin, AnalogOutPin } from "@devicescript/core"\n\n`
m[1].replace(/\| (\w+)Config/g, (_, s) => {
const url = `https://microsoft.github.io/devicescript/api/clients/${s.toLowerCase()}`
startServ += ` /**\n`
startServ += ` * Start on-board server for ${s}, and returns the client for it.\n`
startServ += ` * @returns client for the on-board server\n`
startServ += ` * @see {@link ${url} Documentation}\n`
startServ += ` */\n`
startServ += ` function start${s}(cfg: ${s}Config): ds.${s}\n\n`
return ""
})
startServ += `
/**
* Configure C runtime.
*/
export function configureHardware(cfg: UserHardwareInfo): void
`
srvcfg = srvcfg.replace(m[0], startServ + m[0])
srvcfg = "// auto-generated! do not edit here\n" + srvcfg
fs.writeFileSync(join(folder, serversname), srvcfg)
const filecont = {}
for (const pkgFolder of filesInDir(folder)) {
const pkgJsonPath = join(folder, pkgFolder, "package.json")
if (fs.existsSync(pkgJsonPath)) {
const pkgStr = fs.readFileSync(pkgJsonPath, "utf-8")
const pkg = JSON.parse(pkgStr)
if (pkg.devicescript?.bundle) {
if (!pkg.private)
throw new Error(
`bundled packages have to private: ${pkgJsonPath}`
)
filecont[pkgFolder + "/package.json"] = pkgStr
const src = join(folder, pkgFolder, "src")
for (const ts of filesInDir(src)) {
if (genspecs.includes(ts)) continue
if (ts.endsWith(".ts")) {
filecont[pkgFolder + "/src/" + ts] = fs.readFileSync(
join(src, ts),
"utf-8"
)
}
}
}
}
}
let r = "export const prelude: Record<string, string> = {\n"
for (const fn of Object.keys(filecont)) {
r += ` "${fn}":\n\``
const lines = filecont[fn].split(/\r?\n/)
while (lines[lines.length - 1] == "") lines.pop()
for (const ln of lines) {
r += ln.replace(/[$`\\]/g, x => "\\" + x) + "\n"
}
r += "`,\n"
}
r += "}\n"
let curr = ""
try {
curr = fs.readFileSync(outp, "utf-8")
} catch {}
if (curr != r) {
console.log("updating " + outp)
fs.writeFileSync(outp, r)
}
}
async function main() {
const rootdir = __dirname
try {
process.chdir(rootdir)
copyVM()
buildPrelude("packages", "compiler/src/prelude.ts")
for (const outfile of Object.keys(files)) {
const src = files[outfile]
const folder = resolve(rootdir, dirname(src))
const isVSCode = outfile.includes("vscode")
const cjs = outfile.endsWith(".cjs") || isVSCode
const mjs = outfile.endsWith(".mjs")
const t0 = Date.now()
let platform = cjs ? "node" : "browser"
if (outfile.endsWith("-web.js")) platform = "browser"
const inj = join(folder, "inject.js")
const inject = []
if (fs.existsSync(inj)) inject.push(inj)
const ctx = await esbuild.context({
entryPoints: [rootdir + "/" + src],
bundle: true,
sourcemap: true,
sourcesContent: false,
outfile: rootdir + "/" + outfile,
logLevel: "warning",
inject,
external: ["serialport", "vscode", "crypto", "update-notifier"],
platform,
metafile: true,
target: "es2019",
format: mjs ? "esm" : cjs ? "cjs" : "iife",
})
if (watch) await ctx.watch()
else {
const res = await ctx.rebuild()
if (res.metafile)
fs.writeFileSync(
outfile + "-meta.json",
JSON.stringify(res.metafile)
)
await ctx.dispose()
}
let size = 0
try {
const st = fs.statSync(outfile)
size = st.size
} catch {}
const sizeStr = (size / 1024).toFixed(1)
console.log(`build ${outfile}: ${sizeStr}kB ${Date.now() - t0}ms`)
}
console.log("bundle done")
copyCompiler()
if (!fast) {
const t0 = Date.now()
await runTSC([
"-b",
"interop/src",
"compiler/src",
"dap/src",
"cli/src",
"plugin/src",
"vscode/src",
])
if (!watch) console.log(` -> ${Date.now() - t0}ms`)
}
const ds = require(rootdir +
"/compiler/built/devicescript-compiler.node.cjs")
for (const specname of genspecs)
fs.writeFileSync(
"packages/core/src/" + specname,
ds.preludeFiles()[
"node_modules/@devicescript/core/src/" + specname
]
)
{
// clients
const mds = ds.clientsMarkdownFiles()
const mdo = "website/docs/api/clients"
fs.emptyDirSync(mdo)
fs.writeJSONSync(path.join(mdo, "_category_.json"), {
label: "Clients",
position: 1,
collapsible: true,
})
Object.keys(mds).forEach(fn =>
fs.writeFileSync(path.join(mdo, fn), mds[fn])
)
}
{
// devices
const mds = ds.boardMarkdownFiles()
const mdo = "website/docs/devices"
Object.keys(mds).forEach(fn => {
fs.ensureDirSync(join(mdo, dirname(fn)))
fs.writeFileSync(path.join(mdo, fn), mds[fn], {
encoding: "utf-8",
})
})
}
} catch (e) {
console.error(e)
process.exit(1)
}
}
main()