Skip to content

Commit

Permalink
chore(cli): Reduce boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed May 29, 2022
1 parent 6e782ee commit 24610fa
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 68 deletions.
11 changes: 0 additions & 11 deletions cli/bin/compile.js

This file was deleted.

5 changes: 0 additions & 5 deletions cli/bin/doc.js

This file was deleted.

10 changes: 7 additions & 3 deletions cli/bin/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ function getGrainc() {

const grainc = getGrainc();

function execGrainc(commandOrFile = "", program, execOpts = { stdio: "pipe" }) {
function execGrainc(
commandOrFile = "",
program,
execOpts = { stdio: "inherit" }
) {
const flags = [];
const options = program.opts();
program.options.forEach((option) => {
Expand Down Expand Up @@ -47,7 +51,7 @@ const graindoc = getGraindoc();
function execGraindoc(
commandOrFile = "",
program,
execOpts = { stdio: "pipe" }
execOpts = { stdio: "inherit" }
) {
const flags = [];
// Inherit compiler flags passed to the parent
Expand Down Expand Up @@ -80,7 +84,7 @@ const grainformat = getGrainformat();
function execGrainformat(
commandOrFile = "",
program,
execOpts = { stdio: "pipe" }
execOpts = { stdio: "inherit" }
) {
const flags = [];
// Inherit compiler flags passed to the parent
Expand Down
5 changes: 0 additions & 5 deletions cli/bin/format.js

This file was deleted.

26 changes: 16 additions & 10 deletions cli/bin/grain.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ v8.setFlagsFromString("--experimental-wasm-return-call");

const program = require("commander");
const exec = require("./exec.js");
const compile = require("./compile.js");
const run = require("./run.js");
const lsp = require("./lsp.js");
const doc = require("./doc.js");
const format = require("./format.js");

const stdlibPath = require("@grain/stdlib");

Expand All @@ -31,7 +27,7 @@ function num(val) {
}

function graincVersion() {
return exec.grainc("--version", program).toString().trim();
return exec.grainc("--version", program, { stdio: "pipe" }).toString().trim();
}

class ForwardOption extends program.Option {
Expand Down Expand Up @@ -175,7 +171,12 @@ program
// The root command that compiles & runs
.arguments("<file>")
.action(function (file, options, program) {
run(compile(file, program), options);
exec.grainc(file, program);
if (options.o) {
run(options.o, program.opts());
} else {
run(file.replace(/\.gr$/, ".gr.wasm"), program.opts());
}
});

program
Expand All @@ -184,7 +185,7 @@ program
.action(function (file) {
// The compile subcommand inherits all behaviors/options of the
// top level grain command
compile(file, program);
exec.grainc(file, program);
});

program
Expand All @@ -202,7 +203,12 @@ program
.action(function (file) {
// The lsp subcommand inherits all options of the
// top level grain command
lsp(file, program);

// call the compiler, passing stdio through so the compiler gets the source code on stdin
// and we get the compiler output in stdout
// we still take the file name so we have it available

exec.grainc(`--lsp ${file}`, program);
});

program
Expand All @@ -213,13 +219,13 @@ program
"provide a version to use as current when generating markdown for `@since` and `@history` attributes"
)
.action(function (file, options, program) {
doc(file, program);
exec.graindoc(file, program);
});

program
.command("format <file|dir>")
.description("format a grain file")
.action(function (file, options, program) {
format(file, program);
exec.grainformat(file, program);
});
program.parse(process.argv);
7 changes: 2 additions & 5 deletions cli/bin/grainc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// A wrapper around grainc_js.bc.js that prepares the `pkg` env
const preparePkg = require("./pkg");

preparePkg();

// A wrapper around grainc.bc.js that prepares the `pkg` env
require("./pkg");
require("./grainc.bc.js");
7 changes: 2 additions & 5 deletions cli/bin/graindoc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// A wrapper around grainc_js.bc.js that prepares the `pkg` env
const preparePkg = require("./pkg");

preparePkg();

// A wrapper around grainc.bc.js that prepares the `pkg` env
require("./pkg");
require("./graindoc.bc.js");
7 changes: 2 additions & 5 deletions cli/bin/grainformat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// A wrapper around grainformat_js.bc.js that prepares the `pkg` env
const preparePkg = require("./pkg");

preparePkg();

// A wrapper around grainformat.bc.js that prepares the `pkg` env
require("./pkg");
require("./grainformat.bc.js");
9 changes: 0 additions & 9 deletions cli/bin/lsp.js

This file was deleted.

8 changes: 1 addition & 7 deletions cli/bin/pkg.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// This function paves over some of the weird virtual filesystem stuff inside `pkg`
function preparePkg() {
if (!process.pkg) {
return;
}

if (process.pkg) {
const fs = require("fs");
const path = require("path");
const stdlibPath = require("@grain/stdlib");
Expand All @@ -29,5 +25,3 @@ function preparePkg() {
}
);
}

module.exports = preparePkg;
4 changes: 1 addition & 3 deletions cli/bin/run.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const path = require("path");
const runner = require("@grain/js-runner");
const preparePkg = require("./pkg");
require("./pkg");

module.exports = async function run(filename, options) {
preparePkg();

let basePath = path.dirname(filename);
let includeDirs = [basePath, ...options.includeDirs, options.stdlib];
let locator = runner.defaultFileLocator(includeDirs);
Expand Down

0 comments on commit 24610fa

Please sign in to comment.