Skip to content

Commit 618affb

Browse files
committed
Fx compile status with help command
1 parent dc7eb63 commit 618affb

File tree

4 files changed

+306
-53
lines changed

4 files changed

+306
-53
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// @ts-check
2+
3+
const assert = require("assert");
4+
const child_process = require("child_process");
5+
6+
// Shows compile time for `rescript build` command
7+
let out = child_process.spawnSync(`../../../rescript`, ["build"], {
8+
encoding: "utf8",
9+
cwd: __dirname,
10+
});
11+
assert.match(
12+
out.stdout,
13+
new RegExp(`>>>> Start compiling
14+
Dependency Finished
15+
>>>> Finish compiling \\d+ mseconds`)
16+
);
17+
18+
// Shows compile time for `rescript` command
19+
out = child_process.spawnSync(`../../../rescript`, {
20+
encoding: "utf8",
21+
cwd: __dirname,
22+
});
23+
assert.match(
24+
out.stdout,
25+
new RegExp(`>>>> Start compiling
26+
Dependency Finished
27+
>>>> Finish compiling \\d+ mseconds`)
28+
);
29+
30+
// Doesn't show compile time for `rescript build -verbose` command
31+
// Because we can't be sure that -verbose is a valid argument
32+
// And bsb won't fail with a usage message.
33+
// It works this way not only for -verbose, but any other arg, including -h/--help/-help
34+
out = child_process.spawnSync(`../../../rescript`, ["build", "-verbose"], {
35+
encoding: "utf8",
36+
cwd: __dirname,
37+
});
38+
assert.match(
39+
out.stdout,
40+
new RegExp(
41+
`Package stack: test \nDependency Finished\nninja.exe -C lib/bs \n`
42+
)
43+
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "test",
3+
"version": "0.1.0",
4+
"sources": []
5+
}

jscomp/build_tests/cli_help/input.js

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
// @ts-check
2+
3+
const assert = require("assert");
4+
const child_process = require("child_process");
5+
6+
const cliHelp =
7+
"Usage: rescript <options> <subcommand>\n" +
8+
"\n" +
9+
"`rescript` is equivalent to `rescript build`\n" +
10+
"\n" +
11+
"Options:\n" +
12+
" -v, -version display version number\n" +
13+
" -h, -help display help\n" +
14+
"\n" +
15+
"Subcommands:\n" +
16+
" build\n" +
17+
" clean\n" +
18+
" format\n" +
19+
" convert\n" +
20+
" dump\n" +
21+
" help\n" +
22+
"\n" +
23+
"Run `rescript <subcommand> -h` for subcommand help. Examples:\n" +
24+
" rescript build -h\n" +
25+
" rescript format -h\n" +
26+
"The default `rescript` is equivalent to `rescript build` subcommand\n" +
27+
"\n";
28+
29+
const buildHelp =
30+
"Usage: rescript build <options> -- <ninja_options>\n" +
31+
"\n" +
32+
"`rescript build` builds the project with dependencies\n" +
33+
"\n" +
34+
"`rescript -- -h` for Ninja options (internal usage only; unstable)\n" +
35+
"\n" +
36+
"Options:\n" +
37+
" -w Watch mode\n" +
38+
" -ws [host]:port set up host & port for WebSocket build notifications\n" +
39+
" -verbose Set the output to be verbose\n" +
40+
" -with-deps *deprecated* This is the default behavior now. This option will be removed in a future release\n";
41+
42+
const cleanHelp =
43+
"Usage: rescript clean <options>\n" +
44+
"\n" +
45+
"`rescript clean` cleans build artifacts\n" +
46+
"\n" +
47+
"Options:\n" +
48+
" -verbose Set the output to be verbose\n" +
49+
" -with-deps *deprecated* This is the default behavior now. This option will be removed in a future release\n";
50+
51+
const formatHelp =
52+
"Usage: rescript format <options> [files]\n" +
53+
"\n" +
54+
"`rescript format` formats the current directory\n" +
55+
"\n" +
56+
"Options:\n" +
57+
" -stdin [.res|.resi|.ml|.mli] Read the code from stdin and print\n" +
58+
" the formatted code to stdout in ReScript syntax\n" +
59+
" -all Format the whole project \n" +
60+
" -check Check formatting for file or the whole project. Use `-all` to check the whole project\n";
61+
62+
const convertHelp =
63+
"Usage: rescript convert <options> [files]\n" +
64+
"\n" +
65+
"`rescript convert` converts the current directory\n" +
66+
"\n" +
67+
"**This command removes old OCaml files and creates new ReScript \n" +
68+
"files. Make sure your work is saved first!**\n" +
69+
"\n" +
70+
"Options:\n" +
71+
" -all Convert the whole project\n";
72+
73+
const dumpHelp =
74+
"Usage: rescript dump <options> [target]\n" +
75+
"`rescript dump` dumps the information for the target\n";
76+
77+
// Shows build help with --help arg
78+
let out = child_process.spawnSync(`../../../rescript`, ["build", "--help"], {
79+
encoding: "utf8",
80+
cwd: __dirname,
81+
});
82+
assert.equal(out.stdout, buildHelp);
83+
84+
// Shows build help with -h arg
85+
out = child_process.spawnSync(`../../../rescript`, ["build", "-h"], {
86+
encoding: "utf8",
87+
cwd: __dirname,
88+
});
89+
assert.equal(out.stdout, buildHelp);
90+
91+
// Exits with build help with unknown arg
92+
out = child_process.spawnSync(`../../../rescript`, ["build", "-wtf"], {
93+
encoding: "utf8",
94+
cwd: __dirname,
95+
});
96+
assert.equal(out.stderr, "Error: unknown option: '-wtf'.\n" + buildHelp + "\n");
97+
98+
// Shows cli help with --help arg
99+
out = child_process.spawnSync(`../../../rescript`, ["--help"], {
100+
encoding: "utf8",
101+
cwd: __dirname,
102+
});
103+
assert.equal(out.stdout, cliHelp);
104+
105+
// Shows cli help with -h arg
106+
out = child_process.spawnSync(`../../../rescript`, ["-h"], {
107+
encoding: "utf8",
108+
cwd: __dirname,
109+
});
110+
assert.equal(out.stdout, cliHelp);
111+
112+
// Shows cli help with help command
113+
out = child_process.spawnSync(`../../../rescript`, ["help"], {
114+
encoding: "utf8",
115+
cwd: __dirname,
116+
});
117+
assert.equal(out.stdout, cliHelp);
118+
119+
// Shows cli help with unknown command
120+
out = child_process.spawnSync(`../../../rescript`, ["built"], {
121+
encoding: "utf8",
122+
cwd: __dirname,
123+
});
124+
// Should write to stderr instead ???
125+
assert.equal(out.stdout, cliHelp);
126+
127+
// Shows cli help with unknown args
128+
out = child_process.spawnSync(`../../../rescript`, ["-w"], {
129+
encoding: "utf8",
130+
cwd: __dirname,
131+
});
132+
// Should write to stderr instead ???
133+
assert.equal(out.stdout, cliHelp);
134+
135+
// Shows clean help with --help arg
136+
out = child_process.spawnSync(`../../../rescript`, ["clean", "--help"], {
137+
encoding: "utf8",
138+
cwd: __dirname,
139+
});
140+
assert.equal(out.stdout, cleanHelp);
141+
142+
// Shows clean help with -h arg
143+
out = child_process.spawnSync(`../../../rescript`, ["clean", "-h"], {
144+
encoding: "utf8",
145+
cwd: __dirname,
146+
});
147+
assert.equal(out.stdout, cleanHelp);
148+
149+
// Exits with clean help with unknown arg
150+
out = child_process.spawnSync(`../../../rescript`, ["clean", "-wtf"], {
151+
encoding: "utf8",
152+
cwd: __dirname,
153+
});
154+
assert.equal(out.stderr, "Error: unknown option: '-wtf'.\n" + cleanHelp + "\n");
155+
156+
// Shows format help with --help arg
157+
out = child_process.spawnSync(`../../../rescript`, ["format", "--help"], {
158+
encoding: "utf8",
159+
cwd: __dirname,
160+
});
161+
// Note: it writes to stderr
162+
assert.equal(out.stderr, formatHelp);
163+
164+
// Shows format help with -h arg
165+
out = child_process.spawnSync(`../../../rescript`, ["format", "-h"], {
166+
encoding: "utf8",
167+
cwd: __dirname,
168+
});
169+
// Note: it writes to stderr
170+
assert.equal(out.stderr, formatHelp);
171+
172+
// Shows convert help with --help arg
173+
out = child_process.spawnSync(`../../../rescript`, ["convert", "--help"], {
174+
encoding: "utf8",
175+
cwd: __dirname,
176+
});
177+
// Note: it writes to stderr
178+
assert.equal(out.stderr, convertHelp);
179+
180+
// Shows convert help with -h arg
181+
out = child_process.spawnSync(`../../../rescript`, ["convert", "-h"], {
182+
encoding: "utf8",
183+
cwd: __dirname,
184+
});
185+
// Note: it writes to stderr
186+
assert.equal(out.stderr, convertHelp);
187+
188+
// Shows dump help with --help arg
189+
out = child_process.spawnSync(`../../../rescript`, ["dump", "--help"], {
190+
encoding: "utf8",
191+
cwd: __dirname,
192+
});
193+
// Note: it writes to stderr
194+
assert.equal(out.stderr, dumpHelp);
195+
196+
// Shows dump help with -h arg
197+
out = child_process.spawnSync(`../../../rescript`, ["dump", "-h"], {
198+
encoding: "utf8",
199+
cwd: __dirname,
200+
});
201+
// Note: it writes to stderr
202+
assert.equal(out.stderr, dumpHelp);

rescript

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -512,59 +512,62 @@ Please pick a different one using the \`-ws [host:]port\` flag from bsb.`);
512512
buildFinishedCallback(0);
513513
});
514514
} else {
515-
switch (maybeSubcommand) {
516-
case "info":
517-
case "clean": {
518-
delegateCommand(process_argv.slice(2));
519-
break;
520-
}
521-
case undefined:
522-
case "build": {
523-
logStartCompiling();
524-
delegateCommand(process_argv.slice(2), logFinishCompiling);
525-
break;
515+
const isDefinitelyBuild =
516+
maybeSubcommand === undefined ||
517+
(maybeSubcommand === "build" && process_argv.length === 3);
518+
519+
if (isDefinitelyBuild) {
520+
logStartCompiling();
521+
delegateCommand(process_argv.slice(2), logFinishCompiling);
522+
} else {
523+
switch (maybeSubcommand) {
524+
case "info":
525+
case "clean":
526+
case "build": {
527+
delegateCommand(process_argv.slice(2));
528+
break;
529+
}
530+
case "format":
531+
require("./scripts/rescript_format.js").main(
532+
process.argv.slice(3),
533+
rescript_exe,
534+
bsc_exe
535+
);
536+
break;
537+
case "dump":
538+
require("./scripts/rescript_dump.js").main(
539+
process.argv.slice(3),
540+
rescript_exe,
541+
bsc_exe
542+
);
543+
break;
544+
case "dump":
545+
require("./scripts/rescript_dump.js").main(
546+
process.argv.slice(3),
547+
rescript_exe,
548+
bsc_exe
549+
);
550+
break;
551+
case "convert":
552+
require("./scripts/rescript_convert.js").main(
553+
process.argv.slice(3),
554+
rescript_exe,
555+
bsc_exe
556+
);
557+
break;
558+
case "-h":
559+
case "-help":
560+
case "help":
561+
help();
562+
break;
563+
case "-v":
564+
case "-version":
565+
console.log(require("./package.json").version);
566+
break;
567+
default:
568+
console.error(`Unknown subcommand or flags: ${maybeSubcommand}`);
569+
help();
570+
process.exit(2);
526571
}
527-
case "format":
528-
require("./scripts/rescript_format.js").main(
529-
process.argv.slice(3),
530-
rescript_exe,
531-
bsc_exe
532-
);
533-
break;
534-
case "dump":
535-
require("./scripts/rescript_dump.js").main(
536-
process.argv.slice(3),
537-
rescript_exe,
538-
bsc_exe
539-
);
540-
break;
541-
case "dump":
542-
require("./scripts/rescript_dump.js").main(
543-
process.argv.slice(3),
544-
rescript_exe,
545-
bsc_exe
546-
);
547-
break;
548-
case "convert":
549-
// Todo
550-
require("./scripts/rescript_convert.js").main(
551-
process.argv.slice(3),
552-
rescript_exe,
553-
bsc_exe
554-
);
555-
break;
556-
case "-h":
557-
case "-help":
558-
case "help":
559-
help();
560-
break;
561-
case "-v":
562-
case "-version":
563-
console.log(require("./package.json").version);
564-
break;
565-
default:
566-
console.error(`Unknown subcommand or flags: ${maybeSubcommand}`);
567-
help();
568-
process.exit(2);
569572
}
570573
}

0 commit comments

Comments
 (0)