Skip to content

Commit 9601edb

Browse files
committed
add dump subcommand
# Conflicts: # rescript # scripts/rescript_arg.js
1 parent 98e2cc8 commit 9601edb

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

rescript

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Subcommands:
125125
clean
126126
format
127127
convert
128+
dump
128129
help
129130
Run rescript subcommand -h for more details,
130131
For example:
@@ -153,6 +154,13 @@ if (
153154
bsc_exe
154155
);
155156
break;
157+
case "dump":
158+
require("./scripts/rescript_dump.js").main(
159+
process.argv.slice(3),
160+
bsb_exe,
161+
bsc_exe
162+
);
163+
break;
156164
case "convert":
157165
// Todo
158166
require("./scripts/rescript_convert.js").main(

scripts/rescript_arg.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +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
}
49-
for (let [key, _, doc] of specs) {
49+
for (let i = 0; i < specs.length; i++) {
50+
let [key, _, doc] = specs[i];
5051
if (!doc.startsWith("*internal*")) {
5152
b.add(" ")
5253
.add(key)
@@ -83,7 +84,7 @@ function stop_raise(usage, error, specs) {
8384
case "Unknown":
8485
if (["-help", "--help", "-h"].includes(error.data)) {
8586
usage_b(b, usage, specs);
86-
console.error(b.val);
87+
process.stderr.write(b.val);
8788
process.exit(0);
8889
} else {
8990
b.add("unknown option: '").add(error.data).add("'.\n");
@@ -160,6 +161,6 @@ function parse_exn(
160161
}
161162
annofun(list);
162163
}
163-
164+
exports.bad_arg = bad_arg;
164165
exports.parse_exn = parse_exn;
165166
exports.ArgError = ArgError;

scripts/rescript_dump.js

Lines changed: 57 additions & 0 deletions
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)