From fbb37d4a872dc64c0ea77cd92592f188d3f10f09 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sun, 15 Apr 2018 16:51:28 +0200 Subject: [PATCH] Throw instead of warning. Fixes #68 --- index.js | 51 +++++++++++++++++++++------------------------ test/compile.js | 26 +++++++++++------------ test/compileSync.js | 11 +++++----- 3 files changed, 42 insertions(+), 46 deletions(-) diff --git a/index.js b/index.js index d040dab..84439bc 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,6 @@ var temp = require("temp").track(); var findAllDependencies = require("find-elm-dependencies").findAllDependencies; var defaultOptions = { - emitWarning: console.warn, spawn: spawn, cwd: undefined, pathToElm: undefined, @@ -39,7 +38,7 @@ function prepareOptions(options, spawnFn) { function prepareProcessArgs(sources, options) { var preparedSources = prepareSources(sources); - var compilerArgs = compilerArgsFromOptions(options, options.emitWarning); + var compilerArgs = compilerArgsFromOptions(options); return ["make"].concat(preparedSources ? preparedSources.concat(compilerArgs) : compilerArgs); } @@ -65,15 +64,21 @@ function runCompiler(sources, options, pathToElm) { return options.spawn(pathToElm, processArgs, processOpts); } -function handleCompilerError(err, pathToElm) { +function compilerErrorToString(err, pathToElm) { if ((typeof err === "object") && (typeof err.code === "string")) { - handleError(pathToElm, err); + switch (err.code) { + case "ENOENT": + return ("Could not find Elm compiler \"" + pathToElm + "\". Is it installed?") + + case "EACCES": + return ("Elm compiler \"" + pathToElm + "\" did not have permission to run. Do you need to give it executable permissions?"); + + default: + return ("Error attempting to run Elm compiler \"" + pathToElm + "\":\n" + err); + } } else { - console.error("Exception thrown when attempting to run Elm compiler " + JSON.stringify(pathToElm) + ":\n"); + return ("Exception thrown when attempting to run Elm compiler " + JSON.stringify(pathToElm) + ":\n"); } - throw err; - - process.exit(1); } function compileSync(sources, options) { @@ -83,7 +88,7 @@ function compileSync(sources, options) { try { return runCompiler(sources, optionsWithDefaults, pathToElm); } catch (err) { - handleCompilerError(err, pathToElm); + throw compilerErrorToString(err, pathToElm); } } @@ -94,13 +99,9 @@ function compile(sources, options) { try { return runCompiler(sources, optionsWithDefaults, pathToElm) - .on('error', function(err) { - handleError(pathToElm, err); - - process.exit(1); - }); + .on('error', function(err) { throw(err); }); } catch (err) { - handleCompilerError(err, pathToElm); + throw compilerErrorToString(err, pathToElm); } } @@ -123,7 +124,13 @@ function compileToString(sources, options){ options.output = info.path; options.processOpts = { stdio: 'pipe' } - var compiler = compile(sources, options); + var compiler; + + try { + compiler = compile(sources, options); + } catch(compileError) { + return reject(compileError); + } compiler.stdout.setEncoding("utf8"); compiler.stderr.setEncoding("utf8"); @@ -163,19 +170,9 @@ function compileToStringSync(sources, options) { return fs.readFileSync(file.path, {encoding: "utf8"}); } -function handleError(pathToElm, err) { - if (err.code === "ENOENT") { - console.error("Could not find Elm compiler \"" + pathToElm + "\". Is it installed?") - } else if (err.code === "EACCES") { - console.error("Elm compiler \"" + pathToElm + "\" did not have permission to run. Do you need to give it executable permissions?"); - } else { - console.error("Error attempting to run Elm compiler \"" + pathToElm + "\":\n" + err); - } -} - // Converts an object of key/value pairs to an array of arguments suitable // to be passed to child_process.spawn for elm-make. -function compilerArgsFromOptions(options, emitWarning) { +function compilerArgsFromOptions(options) { return _.flatten(_.map(options, function(value, opt) { if (value) { switch(opt) { diff --git a/test/compile.js b/test/compile.js index 148c8b7..14b29a7 100644 --- a/test/compile.js +++ b/test/compile.js @@ -34,21 +34,18 @@ describe("#compile", function() { }); }); - it("invokes custom `emitWarning`", function (done) { + it("throws when given an unrecognized argument", function () { var opts = { foo: "bar", - emitWarning: chai.spy(), output: "/dev/null", verbose: true, cwd: fixturesDir }; - var compileProcess = compiler.compile(prependFixturesDir("Parent.elm"), opts); - compileProcess.on("exit", function(exitCode) { - var desc = "Expected emitWarning to have been called"; - expect(opts.emitWarning, desc).to.have.been.called(); - done(); - }); + expect(function() { + var compileProcess = compiler.compile(prependFixturesDir("Parent.elm"), opts); + + }).to.throw(); }); }); @@ -98,18 +95,21 @@ describe("#compileToString", function() { }); }); - it("invokes custom `emitWarning`", function () { + it("Rejects the Promise when given an unrecognized argument like `yes`", function () { var opts = { foo: "bar", - emitWarning: chai.spy(), verbose: true, cwd: fixturesDir }; + var compilePromise = compiler.compileToString(prependFixturesDir("Parent.elm"), opts); - return compilePromise.then(function(err) { - var desc = "Expected emitWarning to have been called"; - expect(opts.emitWarning, desc).to.have.been.called(); + return new Promise(function(resolve, reject) { + return compilePromise.then(function() { + reject("Expected the compilation promise to be rejected due to the unrecognized compiler argument."); + }).catch(function() { + resolve(); + }); }); }); diff --git a/test/compileSync.js b/test/compileSync.js index 212a18a..56b6ce3 100644 --- a/test/compileSync.js +++ b/test/compileSync.js @@ -26,17 +26,16 @@ describe("#compileSync", function() { expect(exitCode, desc).to.equal(1); }); - it("invokes custom `emitWarning`", function () { + it("throws when given an unrecognized argument like `yes`", function () { var opts = { - foo: "bar", - emitWarning: chai.spy(), + yes: true, output: "/dev/null", verbose: true, cwd: fixturesDir }; - var compileProcess = compiler.compileSync(prependFixturesDir("Parent.elm"), opts); - var desc = "Expected emitWarning to have been called"; - expect(opts.emitWarning, desc).to.have.been.called(); + expect(function() { + var compileProcess = compiler.compileSync(prependFixturesDir("Parent.elm"), opts); + }).to.throw(); }); });