-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
99 lines (90 loc) · 3.21 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
import { writeFileSync, readFileSync, chmodSync, constants } from 'fs';
import { exec, execSync } from 'child_process';
/**
* Environment variables
* - PATH_PREFIX: where to find the source files for the imports
* - NO_TSC: don't compile typescript
*/
// It really sucks that there isn't a viable preprocessor for TypeScript
console.log("Compensating for TypeScript's lack of a preprocessor...");
// Generate runtime source file
// TODO OPTIMIZE strip comments from WAT?
writeFileSync(
'./lib/rt.wat.ts',
`export default ${JSON.stringify(readFileSync('./lib/rt.wat').toString())
};\n\nexport const noRuntime = ${JSON.stringify(readFileSync('./lib/no_rt.wat').toString())};`,
);
console.log('Generated runtime source import');
// Make standard library not dependent on file-system (ie - for use within browser)
writeFileSync(
'./std/index.ts',
`
// For browser version this should be a url
const pathPrefix = ${process.env.PATH_PREFIX
? JSON.stringify(process.env.PATH_PREFIX)
: 'import.meta.url.slice(7, -8) + "../../std/"'};
export default \{
// These are generated via the build.js script, do not edit directly!
'bits' : \{
src: ${JSON.stringify(readFileSync('./std/bits.phs').toString())},
path: pathPrefix + 'bits.phs',
\},
'either' : \{
src: ${JSON.stringify(readFileSync('./std/either.phs').toString())},
path: pathPrefix + 'either.phs',
\},
'io' : \{
src: ${JSON.stringify(readFileSync('./std/io.phs').toString())},
path: pathPrefix + 'io.phs',
\},
'lang' : \{
src: ${JSON.stringify(readFileSync('./std/lang.phs').toString())},
path: pathPrefix + 'lang.phs',
\},
'list' : \{
src: ${JSON.stringify(readFileSync('./std/list.phs').toString())},
path: pathPrefix + 'list.phs',
\},
'math' : \{
src: ${JSON.stringify(readFileSync('./std/math.phs').toString())},
path: pathPrefix + 'math.phs',
\},
'maybe' : \{
src: ${JSON.stringify(readFileSync('./std/maybe.phs').toString())},
path: pathPrefix + 'maybe.phs',
\},
'mem' : \{
src: ${JSON.stringify(readFileSync('./std/mem.phs').toString())},
path: pathPrefix + 'mem.phs',
\},
'monad' : \{
src: ${JSON.stringify(readFileSync('./std/monad.phs').toString())},
path: pathPrefix + 'monad.phs',
\},
'number' : \{
src: ${JSON.stringify(readFileSync('./std/number.phs').toString())},
path: pathPrefix + 'number.phs',
\},
'str' : \{
src: ${JSON.stringify(readFileSync('./std/str.phs').toString())},
path: pathPrefix + 'str.phs',
\},
\};`,
);
console.log('Generated standard library import');
// Run tsc
console.log('Compiling TypeScript');
if (!process.env.NO_TSC)
exec(
'tsc',
(err, stdout, stderr) => {
if (err) console.error(err);
if (stdout) console.log(stdout);
if (stderr) console.error(stderr);
// Remove wat import (keep for ide)
// fs.unlinkSync('./lib/rt.wat.ts');
console.log('Compiled TypeScript');
console.log('Marking index.js as executable');
execSync('chmod +x ./dist/index.js');
}
);