Skip to content

Commit

Permalink
chore(cli)!: Remove graceful flag & behavior (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed May 29, 2022
1 parent f6fd962 commit df55898
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 63 deletions.
56 changes: 16 additions & 40 deletions cli/bin/grain.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@ function graincVersion() {
return exec.grainc("--version", program).toString().trim();
}

function wrapAction(action, logError = false) {
return (...args) => {
try {
return action(...args);
} catch (e) {
if (logError) console.error(e);
if (program.opts().graceful) {
process.exit();
} else {
process.exit(1);
}
}
};
}

class ForwardOption extends program.Option {
// A ForwardOption is forwarded to the underlying program
forward = true;
Expand Down Expand Up @@ -107,7 +92,6 @@ program
})
.description("Compile and run Grain programs. 🌾")
.addOption(new program.Option("-p, --print-output").hideHelp())
.option("-g, --graceful", "return a 0 exit code if the program errors")
.forwardOption(
"-I, --include-dirs <dirs>",
"add additional dependency include directories",
Expand Down Expand Up @@ -197,13 +181,11 @@ program
program
.command("compile <file>")
.description("compile a grain program into wasm")
.action(
wrapAction(function (file) {
// The compile subcommand inherits all behaviors/options of the
// top level grain command
compile(file, program);
})
);
.action(function (file) {
// The compile subcommand inherits all behaviors/options of the
// top level grain command
compile(file, program);
});

program
.command("run <file>")
Expand All @@ -217,13 +199,11 @@ program
program
.command("lsp <file>")
.description("check a grain file for LSP")
.action(
wrapAction(function (file) {
// The lsp subcommand inherits all options of the
// top level grain command
lsp(file, program);
})
);
.action(function (file) {
// The lsp subcommand inherits all options of the
// top level grain command
lsp(file, program);
});

program
.command("doc <file|dir>")
Expand All @@ -232,18 +212,14 @@ program
"--current-version <version>",
"provide a version to use as current when generating markdown for `@since` and `@history` attributes"
)
.action(
wrapAction(function (file, options, program) {
doc(file, program);
})
);
.action(function (file, options, program) {
doc(file, program);
});

program
.command("format <file|dir>")
.description("format a grain file")
.action(
wrapAction(function (file, options, program) {
format(file, program);
})
);
.action(function (file, options, program) {
format(file, program);
});
program.parse(process.argv);
35 changes: 13 additions & 22 deletions cli/bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,18 @@ const preparePkg = require("./pkg");
module.exports = async function run(filename, options) {
preparePkg();

try {
let basePath = path.dirname(filename);
let includeDirs = [basePath, ...options.includeDirs, options.stdlib];
let locator = runner.defaultFileLocator(includeDirs);
let GrainRunner = runner.buildGrainRunner(locator, {
initialMemoryPages: options.initialMemoryPages,
maximumMemoryPages: options.maximumMemoryPages,
});
if (options.printOutput) {
let result = await GrainRunner.runFileUnboxed(filename);
await GrainRunner.ensureStringModule();
console.log(GrainRunner.grainValueToString(result));
} else {
await GrainRunner.runFile(filename);
}
} catch (e) {
console.error(e);
if (options.graceful) {
process.exit();
} else {
process.exit(-1);
}
let basePath = path.dirname(filename);
let includeDirs = [basePath, ...options.includeDirs, options.stdlib];
let locator = runner.defaultFileLocator(includeDirs);
let GrainRunner = runner.buildGrainRunner(locator, {
initialMemoryPages: options.initialMemoryPages,
maximumMemoryPages: options.maximumMemoryPages,
});
if (options.printOutput) {
let result = await GrainRunner.runFileUnboxed(filename);
await GrainRunner.ensureStringModule();
console.log(GrainRunner.grainValueToString(result));
} else {
await GrainRunner.runFile(filename);
}
};
2 changes: 1 addition & 1 deletion compiler/test/runner.re
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ let run = (~num_pages=?, file) => {

let cmd =
Array.concat([
[|"grain", "-g"|],
[|"grain"|],
mem_flags,
[|"-S", stdlib, "-I", Filepath.to_string(test_libs_dir), "run", file|],
]);
Expand Down

0 comments on commit df55898

Please sign in to comment.