forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.js
executable file
·151 lines (131 loc) · 4.27 KB
/
repl.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
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
#!/usr/bin/env node
/*
* This script is used to compile a bundled compiler file into a standalone JS bundle.
* The compiled output (including the compiled stdlib files), will be put in the defined
* `PLAYGROUND` directory.
*
* Example:
*
* ```
* mkdir playground && mkdir playground/stdlib
* PLAYGROUND=../playground node scripts/repl.js
* ```
*
* You may also pass an alternative JSOO entrypoint (we have multiple defined
* in `jscomp/snapshot.ninja`) as a first parameter:
*
* ```
* # Builds the "simpler" JS bundle
* mkdir playground && mkdir playground/stdlib
* PLAYGROUND=../playground node scripts/repl.js js_compiler
* ```
*/
//@ts-check
var child_process = require("child_process");
var fs = require("fs");
var path = require("path");
var ocamlVersion = "4.06.1";
var jscompDir = path.join(__dirname, "..", "jscomp");
var sourceDir = path.join(__dirname, "..", "lib", ocamlVersion, "unstable");
var config = {
cwd: jscompDir,
encoding: "utf8",
stdio: [0, 1, 2],
shell: true,
};
/**
*
* @param {string} cmd
*/
function e(cmd) {
console.log(`>>>>>> running command: ${cmd}`);
child_process.execSync(cmd, config);
console.log(`<<<<<<`);
}
if (!process.env.PLAYGROUND) {
var defaultPlayground = path.join(__dirname, "..", "playground");
console.warn(`PLAYGROUND env var unset, defaulting to ${defaultPlayground}`);
process.env.PLAYGROUND = defaultPlayground;
}
const BUILD_THIRD_PARTY = process.env.BUILD_THIRD_PARTY || false;
var playground = process.env.PLAYGROUND;
var OCAMLC = `ocamlc.opt`;
// This mini project is needed to build cmij files for third party deps like @rescript/react
const PLAYGROUND_BUNDLING = path.join(
__dirname,
"..",
"packages",
"playground-bundling"
);
var JSOO = `js_of_ocaml`;
function prepare(isDev, targetCompilerFile) {
var [env, ocamlFlag, jsooFlag] = isDev
? ["development", "-g ", "--pretty "]
: ["production", "", ""];
console.log(
`building byte code version of target compiler file '${targetCompilerFile}' [${env}]`
);
if (!fs.existsSync(playground)) {
console.log(`Creating output folder "${playground}"`);
// Create both, the `playground` and `playground/stdlib` dir
const stdlibDir = path.join(playground, "stdlib");
fs.mkdirSync(stdlibDir, { recursive: true });
}
const mliFile = path.join(sourceDir, targetCompilerFile + ".mli");
const mlFile = path.join(sourceDir, targetCompilerFile + ".ml");
// There may be a situation where the .mli file doesn't exist (mostly when
// the snapshot hasn't been checked into `lib/4.06.1/unstable`
e(`touch ${mliFile}`);
e(
`${OCAMLC} ${ocamlFlag}-w -30-40 -no-check-prims -I ${sourceDir} ${mliFile} ${mlFile} -o jsc.byte `
);
console.log(
`building js version for compiler target '${targetCompilerFile}'`
);
e(`${JSOO} compile jsc.byte ${jsooFlag}-o compiler.js`);
console.log("copy js artifacts");
e(`cp ../lib/js/*.js ${playground}/stdlib`);
e(`mv ./compiler.js ${playground}`);
// BUILDING THIRD PARTY CMIJ FILES
if (BUILD_THIRD_PARTY) {
console.log("Building third party packages...");
e(`cd ${PLAYGROUND_BUNDLING} && npm run build`);
}
}
function prepublish() {
var mainPackageJson = JSON.parse(
fs.readFileSync(path.join(__dirname, "..", "package.json"))
);
var packageJson = JSON.stringify(
{
name: "rescript-compiler",
version: mainPackageJson.version,
license: mainPackageJson.license,
description: mainPackageJson.description,
repository: mainPackageJson.repository,
author: mainPackageJson.author,
maintainers: mainPackageJson.maintainers,
bugs: mainPackageJson.bugs,
homepage: mainPackageJson.homepage,
main: "compiler.js",
},
null,
2
);
fs.writeFileSync(jscompDir + `/${playground}/package.json`, packageJson, {
encoding: "utf8",
});
}
// Relevant target compiler files can be found in jscomp/snapshot.ninja
let targetCompilerFile = "js_playground_compiler";
// Let's derive the target file to compile from the last argument list.
if (process.argv.length > 2) {
const lastArg = process.argv[process.argv.length - 1];
if (!lastArg.startsWith("-")) {
targetCompilerFile = lastArg;
}
}
prepare(process.argv.includes("-development"), targetCompilerFile);
if (process.argv.includes("-prepublish")) {
prepublish();
}