-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
build.js
executable file
·89 lines (84 loc) · 2.15 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
#!/usr/bin/env node
import { exec } from 'child_process'
import fs from 'fs'
import path from 'path'
const languages = [
{
npm: 'tree-sitter-java',
dir: '',
wasm: 'java',
},
{
npm: 'tree-sitter-typescript',
dir: 'tsx',
wasm: 'tsx',
},
{
npm: 'tree-sitter-c-sharp',
dir: '',
wasm: 'c_sharp',
},
{
npm: 'tree-sitter-php',
dir: '',
wasm: 'php',
},
{
npm: 'tree-sitter-ruby',
dir: '',
wasm: 'ruby',
},
{
npm: 'tree-sitter-python',
dir: '',
wasm: 'python',
},
{
npm: 'tree-sitter-rust',
dir: '',
wasm: 'rust',
},
]
// Build wasm parsers for supported languages
const distDir = 'dist'
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir)
}
const treeSitterCli = path.join('node_modules', '.bin', 'tree-sitter')
if (!fs.existsSync(treeSitterCli)) {
console.info(`Skipping compilation of tree-sitter wasms - ${treeSitterCli} is not installed`)
} else {
for (const { npm, dir, wasm } of languages) {
const module = path.join('node_modules', npm, dir)
if (!fs.existsSync(module)) {
console.error(
`Module ${module} does not exist. This is likely due to an installation and/or build failure of the module. Please check the logs.`
)
process.exit(1)
}
let command
if (process.env.CI) {
console.log(`Compiling ${module}`)
command = `node_modules/.bin/tree-sitter build-wasm ${module}`
} else {
console.log(`Compiling ${module} inside docker`)
// https://github.com/tree-sitter/tree-sitter/issues/1560
command = `node_modules/.bin/tree-sitter build-wasm ${module} --docker`
}
exec(command, (err) => {
if (err) {
console.error('Failed to build wasm for ' + module + ': ' + err.message)
process.exit(1)
} else {
const output = `tree-sitter-${wasm}.wasm`
const newPath = `${distDir}/${wasm}.wasm`
fs.rename(output, newPath, (err) => {
if (err) {
console.error('Failed to copy built parser: ' + err.message)
process.exit(1)
} else console.log(`Successfully compiled ${newPath}`)
})
}
})
}
}