forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
126 lines (112 loc) · 3.03 KB
/
Copy pathbuild.js
File metadata and controls
126 lines (112 loc) · 3.03 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
#!/usr/bin/env node
const esbuild = require("esbuild")
const fs = require("fs")
const childProcess = require("child_process")
let watch = false
let fast = false
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]")
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"
if (!fs.existsSync(tscPath))
tscPath = "../" + tscPath
if (!fs.existsSync(tscPath))
tscPath = "../" + tscPath
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 = {
"built/jacscript-compiler.bundle.js": "src/jacscript.ts",
"built/jacscript-compiler.node.cjs": "src/jacscript.ts",
}
function buildPrelude(folder, outp) {
const files = fs.readdirSync(folder)
files.sort((a, b) => a < b ? -1 : a > b ? 1 : 0)
const filecont = {}
for (const fn of files) {
filecont[fn] = fs.readFileSync(folder + "/" + fn, "utf-8")
}
const specs = "../jacdac-c/jacdac/dist/jacscript-spec.d.ts"
filecont["../" + specs] = fs.readFileSync(specs, "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() {
try {
buildPrelude("lib", "src/prelude.ts")
for (const outfile of Object.keys(files)) {
const src = files[outfile]
const cjs = outfile.endsWith(".cjs")
const mjs = outfile.endsWith(".mjs")
await esbuild.build({
entryPoints: [src],
bundle: true,
sourcemap: true,
outfile,
logLevel: "warning",
external: [],
platform: cjs ? "node" : "browser",
target: "es2019",
format: mjs ? "esm" : cjs ? "cjs" : "iife",
watch
})
}
console.log("bundle done")
if (!fast)
await runTSC(["-b", "src"])
} catch (e) {
console.error(e)
}
}
main()