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
executable file
·167 lines (150 loc) · 4.69 KB
/
build.js
File metadata and controls
executable file
·167 lines (150 loc) · 4.69 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
#!/usr/bin/env node
const esbuild = require("esbuild")
const fs = require("fs")
const childProcess = require("child_process")
const path = 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 = "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]")
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/devicescript-compiler.js": "src/devicescript.ts",
"built/devicescript-compiler.node.cjs": "src/devicescript.ts",
"../cli/built/devicescript-cli.cjs": "../cli/src/cli.ts",
}
const specname = "devicescript-spec.d.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) {
if (fn == specname) continue
filecont[fn] = fs.readFileSync(folder + "/" + fn, "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 {
copyVM()
buildPrelude("../jacs/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")
const t0 = Date.now()
await esbuild.build({
entryPoints: [src],
bundle: true,
sourcemap: true,
outfile,
logLevel: "warning",
external: ["websocket-polyfill", "@devicescript/compiler"],
platform: cjs ? "node" : "browser",
target: "es2019",
format: mjs ? "esm" : cjs ? "cjs" : "iife",
watch,
})
console.log(`build ${outfile}: ${Date.now() - t0}ms`)
}
console.log("bundle done")
copyCompiler()
if (!fast) {
await runTSC(["-b", "src"])
await runTSC(["-b", "../cli/src"])
}
const ds = require("./built/devicescript-compiler.node.cjs")
fs.writeFileSync("../jacs/lib/" + specname, ds.preludeFiles()[specname])
} catch (e) {
console.error(e)
}
}
main()