-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
81 lines (68 loc) · 2.09 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
const { execSync } = require('child_process')
const { resolve } = require('path')
const { createWriteStream, copyFileSync, rmSync, statSync } = require('fs')
const { platform } = require('os')
const { request } = require('https')
const wabtVersion = '1.0.34'
const os = platform()
const exe = os === 'win32' ? '.exe' : ''
const curPlatform = {
win32: 'windows',
linux: 'ubuntu',
darwin: 'maxos',
}[os]
if (!curPlatform) {
console.error(`No wabt release available for the current platform: "${os}"`)
process.exit(1)
}
const buildDir = resolve(__dirname, 'build')
const extract = function () {
execSync(`tar${exe} -x -f wabt.tar.gz wabt-${wabtVersion}/bin/wat2wasm${exe}`, { cwd: buildDir })
copyFileSync(
resolve(buildDir, `wabt-${wabtVersion}/bin/wat2wasm${exe}`),
resolve(buildDir, `wat2wasm${exe}`)
)
rmSync(resolve(buildDir, `wabt-${wabtVersion}`), { force: true, recursive: true })
rmSync(resolve(buildDir, 'wabt.tar.gz'))
}
const compile = function () {
execSync(`./wat2wasm${exe} -o ../lib/chunk.wasm ../lib/chunk.wat`, { cwd: buildDir })
}
try {
// Check that wat2wasm exists
statSync(resolve(buildDir, `./wat2wasm${exe}`))
compile()
} catch (err) {
// Download wabt archive
const file = createWriteStream(resolve(buildDir, 'wabt.tar.gz'), {
flags: 'wx', // Fail if the file already exists
})
file.on('open', () => {
const req = request({
method: 'GET',
host: 'github.com',
path: `/WebAssembly/wabt/releases/download/${wabtVersion}/wabt-${wabtVersion}-${curPlatform}.tar.gz`
})
const onResponse = function (res) {
if (res.statusCode === 302) {
const req = request(res.headers.location)
req.on('response', onResponse)
req.end()
return
} else if (res.statusCode != 200) {
console.log(`Failed to download wabt with status "${res.statusCode}".`)
process.exit(1)
}
res.pipe(file)
}
req.on('response', onResponse)
req.end()
})
file.on('error', () => {
console.log('using existing wabt.tar.gz')
})
file.on('close', () => {
extract()
compile()
})
}