forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_cmijs.js
More file actions
114 lines (92 loc) · 3.15 KB
/
Copy pathgenerate_cmijs.js
File metadata and controls
114 lines (92 loc) · 3.15 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
#!/usr/bin/env node
"use strict";
/*
* Requires the version matching `rescript` binary to be `npm link`ed in this
* project. Or in other words: You need to build cmij files with the same
* rescript version as the compiler bundle.
*
* This script extracts all cmi / cmj files of the rescript/lib/ocaml and all
* dependencies listed in the project root's rescript.json, creates cmij.js
* files for each library and puts them in the compiler playground directory.
*
* The cmij files are representing the marshaled dependencies that can be used with the ReScript
* playground bundle.
*/
const child_process = require("child_process");
const fs = require("fs");
const path = require("path");
const resConfig = require("../rescript.json");
const RESCRIPT_COMPILER_ROOT_DIR = path.join(__dirname, "..", "..", "..");
const PLAYGROUND_DIR = path.join(RESCRIPT_COMPILER_ROOT_DIR, "playground");
// The playground-bundling root dir
const PROJECT_ROOT_DIR = path.join(__dirname, "..");
// Final target output directory where all the cmijs will be stored
const PACKAGES_DIR = path.join(PLAYGROUND_DIR, "packages");
// Making sure this directory exists, since it's not checked in to git
if (!fs.existsSync(PACKAGES_DIR)) {
fs.mkdirSync(PACKAGES_DIR, { recursive: true });
}
const config = {
cwd: PROJECT_ROOT_DIR,
encoding: "utf8",
stdio: [0, 1, 2],
shell: true,
};
function e(cmd) {
console.log(`>>>>>> running command: ${cmd}`);
child_process.execSync(cmd, config);
console.log(`<<<<<<`);
}
e(`npm install`);
e(`npm link ${RESCRIPT_COMPILER_ROOT_DIR}`);
e(`npx rescript clean`);
e(`npx rescript`);
const packages = resConfig["bs-dependencies"];
// We need to build the compiler's builtin modules as a separate cmij.
// Otherwise we can't use them for compilation within the playground.
function buildCompilerCmij() {
const rescriptLibOcamlFolder = path.join(
PROJECT_ROOT_DIR,
"node_modules",
"rescript",
"lib",
"ocaml",
);
const outputFolder = path.join(PACKAGES_DIR, "compiler-builtins");
const cmijFile = path.join(outputFolder, `cmij.js`);
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder, { recursive: true });
}
e(
`find ${rescriptLibOcamlFolder} -name "*.cmi" -or -name "*.cmj" | xargs -n1 basename | xargs js_of_ocaml build-fs -o ${cmijFile} -I ${rescriptLibOcamlFolder}`,
);
}
function buildThirdPartyCmijs() {
packages.forEach(function installLib(pkg) {
const libOcamlFolder = path.join(
PROJECT_ROOT_DIR,
"node_modules",
pkg,
"lib",
"ocaml",
);
const libEs6Folder = path.join(
PROJECT_ROOT_DIR,
"node_modules",
pkg,
"lib",
"es6",
);
const outputFolder = path.join(PACKAGES_DIR, pkg);
const cmijFile = path.join(outputFolder, `cmij.js`);
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder, { recursive: true });
}
e(`find ${libEs6Folder} -name '*.js' -exec cp {} ${outputFolder} \\;`);
e(
`find ${libOcamlFolder} -name "*.cmi" -or -name "*.cmj" | xargs -n1 basename | xargs js_of_ocaml build-fs -o ${cmijFile} -I ${libOcamlFolder}`,
);
});
}
buildCompilerCmij();
buildThirdPartyCmijs();