Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 46 additions & 20 deletions jscomp/build_tests/cli_help/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,193 +73,219 @@ const dumpHelp =
"`rescript dump` dumps the information for the target\n";

// Shows build help with --help arg
console.group("build --help");
let out = child_process.spawnSync(`../../../rescript`, ["build", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, buildHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// FIXME: Help works incorrectly in watch mode
console.group("build -w --help");
out = child_process.spawnSync(`../../../rescript`, ["build", "-w", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
// FIXME: Shouldn't have "Start compiling" for help
assert.equal(out.stdout, ">>>> Start compiling\n" + buildHelp);
// FIXME: Don't run the watcher when showing help
assert.match(
out.stderr,
new RegExp(
"Uncaught Exception Error: ENOENT: no such file or directory, watch 'bsconfig.json'\n"
)
);
// FIXME: Should be 0
assert.equal(out.status, 1);
assert.equal(out.stdout, buildHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// FIXME: Has the same problem with `rescript -w`
console.group("-w --help");
out = child_process.spawnSync(`../../../rescript`, ["-w", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, ">>>> Start compiling\n" + buildHelp);
assert.match(
out.stderr,
new RegExp(
"Uncaught Exception Error: ENOENT: no such file or directory, watch 'bsconfig.json'\n"
)
);
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Shows cli help with --help arg even if there are invalid arguments after it
console.group("--help -w");
out = child_process.spawnSync(`../../../rescript`, ["--help", "-w"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Shows build help with -h arg
console.group("build -h");
out = child_process.spawnSync(`../../../rescript`, ["build", "-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, buildHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Exits with build help with unknown arg
console.group("build -foo");
out = child_process.spawnSync(`../../../rescript`, ["build", "-foo"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, 'Error: Unknown option "-foo".\n' + buildHelp);
assert.equal(out.status, 2);
console.groupEnd();

// Shows cli help with --help arg
console.group("--help");
out = child_process.spawnSync(`../../../rescript`, ["--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Shows cli help with -h arg
console.group("-h");
out = child_process.spawnSync(`../../../rescript`, ["-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Shows cli help with help command
console.group("help");
out = child_process.spawnSync(`../../../rescript`, ["help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Exits with cli help with unknown command
console.group("built");
out = child_process.spawnSync(`../../../rescript`, ["built"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, `Error: Unknown command "built".\n` + cliHelp);
assert.equal(out.status, 2);
console.groupEnd();

// Exits with build help with unknown args
console.group("-foo");
out = child_process.spawnSync(`../../../rescript`, ["-foo"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, 'Error: Unknown option "-foo".\n' + buildHelp);
assert.equal(out.status, 2);
console.groupEnd();

// Shows clean help with --help arg
console.group("clean --help");
out = child_process.spawnSync(`../../../rescript`, ["clean", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cleanHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Shows clean help with -h arg
console.group("clean -h");
out = child_process.spawnSync(`../../../rescript`, ["clean", "-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cleanHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Exits with clean help with unknown arg
console.group("clean -foo");
out = child_process.spawnSync(`../../../rescript`, ["clean", "-foo"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, 'Error: Unknown option "-foo".\n' + cleanHelp);
assert.equal(out.status, 2);
console.groupEnd();

// Shows format help with --help arg
console.group("format --help");
out = child_process.spawnSync(`../../../rescript`, ["format", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, formatHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Shows format help with -h arg
console.group("format -h");
out = child_process.spawnSync(`../../../rescript`, ["format", "-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, formatHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Shows convert help with --help arg
console.group("convert --help");
out = child_process.spawnSync(`../../../rescript`, ["convert", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, convertHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Shows convert help with -h arg
console.group("convert -h");
out = child_process.spawnSync(`../../../rescript`, ["convert", "-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, convertHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Shows dump help with --help arg
console.group("dump --help");
out = child_process.spawnSync(`../../../rescript`, ["dump", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, dumpHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();

// Shows dump help with -h arg
console.group("dump -h");
out = child_process.spawnSync(`../../../rescript`, ["dump", "-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, dumpHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
console.groupEnd();
70 changes: 34 additions & 36 deletions rescript
Original file line number Diff line number Diff line change
Expand Up @@ -67,66 +67,64 @@ process.on("SIGUSR2", exitProcess);
process.on("SIGTERM", exitProcess);
process.on("SIGHUP", exitProcess);

const process_argv = process.argv;
const maybeSubcommand = process_argv[2];
const args = process.argv.slice(2);
const helpArgIndex = args.findIndex(arg => /help|-h|-help|--help/.test(arg));
const firstPositionalArgIndex = args.findIndex(arg => !arg.startsWith("-"));

if (!maybeSubcommand) {
bsb.build([]);
} else {
switch (maybeSubcommand) {
if (helpArgIndex !== -1 && (firstPositionalArgIndex === -1 || helpArgIndex <= firstPositionalArgIndex)) {
console.log(helpMessage);

} else if (/version|-v|-version|--version/.test(args[0])) {
console.log(require("./package.json").version);

} else if (firstPositionalArgIndex !== -1) {
const subcmd = args[firstPositionalArgIndex];
const subcmdArgs = args.slice(firstPositionalArgIndex + 1);

switch (subcmd) {
case "info": {
bsb.info(process_argv.slice(3));
bsb.info(subcmdArgs);
break;
}
case "clean": {
bsb.clean(process_argv.slice(3));
bsb.clean(subcmdArgs);
break;
}
case "build": {
bsb.build(process_argv.slice(3));
bsb.build(subcmdArgs);
break;
}
case "format":
case "format": {
require("./scripts/rescript_format.js").main(
process.argv.slice(3),
subcmdArgs,
rescript_exe,
bsc_exe
);
break;
case "dump":
}
case "dump": {
require("./scripts/rescript_dump.js").main(
process.argv.slice(3),
subcmdArgs,
rescript_exe,
bsc_exe
);
break;
case "convert":
}
case "convert": {
require("./scripts/rescript_convert.js").main(
process.argv.slice(3),
subcmdArgs,
rescript_exe,
bsc_exe
);
break;
case "-h":
case "-help":
case "--help":
case "help":
console.log(helpMessage);
break;
case "-v":
case "-version":
case "--version":
case "version":
console.log(require("./package.json").version);
break;
default:
if (maybeSubcommand.startsWith("-")) {
bsb.build(process_argv.slice(2));
} else {
console.error(
`Error: Unknown command "${maybeSubcommand}".\n${helpMessage}`
);
process.exit(2);
}
}
default: {
console.error(
`Error: Unknown command "${subcmd}".\n${helpMessage}`
);
process.exit(2);
}
}
} else {
bsb.build(args);
}
Loading