Skip to content

Commit 5281e61

Browse files
committed
add dump subcommand
1 parent e5c4875 commit 5281e61

File tree

3 files changed

+80
-10
lines changed

3 files changed

+80
-10
lines changed

rescript

+17-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ var fs = require("fs");
1414
var bsconfig = "bsconfig.json";
1515

1616
/**
17-
*
17+
*
1818
* @type{string}
1919
*/
20-
var bin_path = path.join(__dirname, process.platform === 'darwin' && process.arch === 'arm64' ? process.platform + process.arch : process.platform)
20+
var bin_path = path.join(
21+
__dirname,
22+
process.platform === "darwin" && process.arch === "arm64"
23+
? process.platform + process.arch
24+
: process.platform
25+
);
2126

2227
/**
2328
* @type{string}
@@ -172,6 +177,7 @@ Subcommands:
172177
clean
173178
format
174179
convert
180+
dump
175181
help
176182
177183
Run \`rescript <subcommand> -h\` for subcommand help. Examples:
@@ -185,7 +191,7 @@ function releaseBuild() {
185191
if (is_building) {
186192
try {
187193
fs.unlinkSync(lockFileName);
188-
} catch (err) { }
194+
} catch (err) {}
189195
is_building = false;
190196
}
191197
}
@@ -219,7 +225,7 @@ if (
219225
/**
220226
* @type {string}
221227
*/
222-
var bsc_exe = path.join(bin_path, "bsc.exe")
228+
var bsc_exe = path.join(bin_path, "bsc.exe");
223229

224230
switch (maybe_subcommand) {
225231
case "format":
@@ -229,6 +235,13 @@ if (
229235
bsc_exe
230236
);
231237
break;
238+
case "dump":
239+
require("./scripts/rescript_dump.js").main(
240+
process.argv.slice(3),
241+
bsb_exe,
242+
bsc_exe
243+
);
244+
break;
232245
case "convert":
233246
// Todo
234247
require("./scripts/rescript_convert.js").main(

scripts/rescript_arg.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ function bad_arg(s) {
3636
*/
3737
function usage_b(b, usage, specs) {
3838
b.add(usage);
39-
b.add(`\nOptions:\n`);
40-
if(specs.length === 0){
41-
return
39+
if (specs.length === 0) {
40+
return;
4241
}
42+
b.add(`\nOptions:\n`);
4343
var max_col = 0;
4444
for (let [key] of specs) {
4545
if (key.length > max_col) {
4646
max_col = key.length;
4747
}
4848
}
4949
for (let i = 0; i < specs.length; i++) {
50-
let [key, _, doc] = specs[i]
50+
let [key, _, doc] = specs[i];
5151
if (!doc.startsWith("*internal*")) {
5252
b.add(" ")
5353
.add(key)
@@ -86,7 +86,7 @@ function stop_raise(usage, error, specs) {
8686
case "Unknown":
8787
if (["-help", "--help", "-h"].includes(error.data)) {
8888
usage_b(b, usage, specs);
89-
console.error(b.val);
89+
process.stderr.write(b.val);
9090
process.exit(0);
9191
} else {
9292
b.add("unknown option: '").add(error.data).add("'.\n");
@@ -163,6 +163,6 @@ function parse_exn(
163163
}
164164
annofun(list);
165165
}
166-
166+
exports.bad_arg = bad_arg;
167167
exports.parse_exn = parse_exn;
168168
exports.ArgError = ArgError;

scripts/rescript_dump.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//@ts-check
2+
var arg = require("./rescript_arg.js");
3+
var dump_usage = `Usage: rescript dump <options> [target]
4+
\`rescript dump\` dumps the information for the target
5+
`;
6+
var child_process = require("child_process");
7+
var path = require("path");
8+
var specs = [];
9+
10+
/**
11+
* @param {string[]} argv
12+
* @param {string} bsb_exe
13+
* @param {string} bsc_exe
14+
*/
15+
function main(argv, bsb_exe, bsc_exe) {
16+
var target;
17+
arg.parse_exn(dump_usage, argv, specs, (xs) => {
18+
if (xs.length !== 1) {
19+
arg.bad_arg(`Expect only one target, ${xs.length} found` );
20+
}
21+
target = xs[0];
22+
});
23+
24+
var { ext } = path.parse(target);
25+
if (ext !== ".cmi") {
26+
console.error("Only .cmi target allowed");
27+
process.exit(2);
28+
}
29+
30+
31+
var output = child_process.spawnSync(
32+
bsb_exe,
33+
["build", "--", target],
34+
{
35+
encoding: "utf-8",
36+
}
37+
);
38+
if (output.status !== 0) {
39+
console.log(output.stdout);
40+
console.error(output.stderr);
41+
process.exit(2);
42+
}
43+
output = child_process.spawnSync(
44+
bsc_exe,
45+
[path.join("lib", "bs", target)],
46+
{
47+
encoding: "utf-8",
48+
}
49+
);
50+
console.log(output.stdout.trimEnd());
51+
if (output.status !== 0) {
52+
console.error(output.stderr);
53+
process.exit(2);
54+
}
55+
}
56+
57+
exports.main = main;

0 commit comments

Comments
 (0)