Skip to content

Commit

Permalink
Merge pull request #72 from rtfeldman/0.19
Browse files Browse the repository at this point in the history
Update for 0.19
  • Loading branch information
Richard Feldman authored Aug 28, 2018
2 parents 8dd2be4 + 665e6d7 commit 48f29fc
Show file tree
Hide file tree
Showing 21 changed files with 577 additions and 198 deletions.
23 changes: 5 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
sudo: false

cache:
directories:
- test/elm-stuff/build-artifacts
- sysconfcpus

os:
- linux
- osx

env:
matrix:
- ELM_VERSION=0.18.0 TARGET_NODE_VERSION=node
- ELM_VERSION=0.19.0-bugfix2 TARGET_NODE_VERSION=4.0
- ELM_VERSION=0.19.0-bugfix2 TARGET_NODE_VERSION=6.0
- ELM_VERSION=0.19.0-bugfix2 TARGET_NODE_VERSION=8.0
- ELM_VERSION=0.19.0-bugfix2 TARGET_NODE_VERSION=node

before_install:
- rm -rf ~/.elm
- if [ ${TRAVIS_OS_NAME} == "osx" ];
then brew update; brew install nvm; mkdir ~/.nvm; export NVM_DIR=~/.nvm; source $(brew --prefix nvm)/nvm.sh;
fi
- | # epic build time improvement - see https://github.com/elm-lang/elm-compiler/issues/1473#issuecomment-245704142
if [ ! -d sysconfcpus/bin ];
then
git clone https://github.com/obmarg/libsysconfcpus.git;
cd libsysconfcpus;
./configure --prefix=$TRAVIS_BUILD_DIR/sysconfcpus;
make && make install;
cd ..;
fi

install:
- nvm install $TARGET_NODE_VERSION
- nvm use $TARGET_NODE_VERSION
- node --version
- npm --version
- npm install -g elm@$ELM_VERSION
- mv $(npm config get prefix)/bin/elm-make $(npm config get prefix)/bin/elm-make-old
- printf "#\041/bin/bash\n\necho \"Running elm-make with sysconfcpus -n 2\"\n\n$TRAVIS_BUILD_DIR/sysconfcpus/bin/sysconfcpus -n 2 elm-make-old \"\$@\"" > $(npm config get prefix)/bin/elm-make
- chmod +x $(npm config get prefix)/bin/elm-make
- npm install

script:
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# node-elm-compiler [![Version](https://img.shields.io/npm/v/node-elm-compiler.svg)](https://www.npmjs.com/package/node-elm-compiler) [![Travis build Status](https://travis-ci.org/rtfeldman/node-elm-compiler.svg?branch=master)](http://travis-ci.org/rtfeldman/node-elm-compiler) [![AppVeyor build status](https://ci.appveyor.com/api/projects/status/xv83jcomgb81i1iu/branch/master?svg=true)](https://ci.appveyor.com/project/rtfeldman/node-elm-compiler/branch/master)

Wraps [Elm](https://elm-lang.org) and exposes a [Node](https://nodejs.org) API to compile Elm sources.

Supports Elm version 0.17, 0.18
Wraps [Elm](https://elm-lang.org) and exposes a [Node](https://nodejs.org) API to compile Elm 0.19 sources.

# Example

Expand All @@ -14,6 +12,10 @@ $ node compileHelloWorld.js

# Releases

## 5.0.0

Add 0.19 support. Remove `yes` option. Add `optimize` option. Throw exceptions instead of emitting warnings or using process.exit.

## 4.5.0

Add `runtimeOptions`
Expand Down
5 changes: 3 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
environment:
ELM_VERSION: "0.18.0"
ELM_VERSION: "0.19.0-bugfix2"
matrix:
- nodejs_version: "5.0"
- nodejs_version: "8.0"
- nodejs_version: "6.0"
- nodejs_version: "4.0"

platform:
Expand Down
8 changes: 4 additions & 4 deletions examples/compileHelloWorld.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ compile(["./HelloWorld.elm"], {
});


compileToString(["./HelloWorld.elm"], { yes: true }).then(function(data){
console.log("Text", data.toString());
compileToString(["./HelloWorld.elm"], {}).then(function(data){
console.log("compileToString produced a string with this length:", data.toString().length);
});

compileToString(["./HelloWorld.elm"], { yes: true, output: "index.html" }).then(function(data){
console.log("Text", data.toString());
compileToString(["./HelloWorld.elm"], { output: "index.html" }).then(function(data){
console.log("compileToString --output index.html produced a string with this length:", data.toString().length);
});
15 changes: 0 additions & 15 deletions examples/elm-package.json

This file was deleted.

24 changes: 24 additions & 0 deletions examples/elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "application",
"source-directories": [
"."
],
"elm-version": "0.19.0",
"dependencies": {
"direct": {
"elm/browser": "1.0.0",
"elm/core": "1.0.0",
"elm/html": "1.0.0"
},
"indirect": {
"elm/json": "1.0.0",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.0"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
88 changes: 43 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@

var spawn = require("cross-spawn");
var _ = require("lodash");
var compilerBinaryName = "elm-make";
var elmBinaryName = "elm";
var fs = require("fs");
var path = require("path");
var temp = require("temp").track();
var findAllDependencies = require("find-elm-dependencies").findAllDependencies;

var defaultOptions = {
emitWarning: console.warn,
spawn: spawn,
cwd: undefined,
pathToMake: undefined,
yes: undefined,
pathToElm: undefined,
help: undefined,
output: undefined,
report: undefined,
warn: undefined,
debug: undefined,
verbose: false,
processOpts: undefined,
docs: undefined,
optimize: undefined,
};

var supportedOptions = _.keys(defaultOptions);
Expand All @@ -40,9 +37,9 @@ function prepareOptions(options, spawnFn) {

function prepareProcessArgs(sources, options) {
var preparedSources = prepareSources(sources);
var compilerArgs = compilerArgsFromOptions(options);

var compilerArgs = compilerArgsFromOptions(options, options.emitWarning);
return preparedSources ? preparedSources.concat(compilerArgs) : compilerArgs;
return ["make"].concat(preparedSources ? preparedSources.concat(compilerArgs) : compilerArgs);
}

function prepareProcessOpts(options) {
Expand All @@ -51,7 +48,7 @@ function prepareProcessOpts(options) {

}

function runCompiler(sources, options, pathToMake) {
function runCompiler(sources, options, pathToElm) {
if (typeof options.spawn !== "function") {
throw "options.spawn was a(n) " + (typeof options.spawn) + " instead of a function.";
}
Expand All @@ -60,48 +57,50 @@ function runCompiler(sources, options, pathToMake) {
var processOpts = prepareProcessOpts(options);

if (options.verbose) {
console.log(["Running", pathToMake].concat(processArgs || []).join(" "));
console.log(["Running", pathToElm].concat(processArgs).join(" "));
}

return options.spawn(pathToMake, processArgs, processOpts);
return options.spawn(pathToElm, processArgs, processOpts);
}

function handleCompilerError(err, pathToMake) {
function compilerErrorToString(err, pathToElm) {
if ((typeof err === "object") && (typeof err.code === "string")) {
handleError(pathToMake, 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(pathToMake) + ":\n");
return ("Exception thrown when attempting to run Elm compiler " + JSON.stringify(pathToElm) + ":\n");
}
throw err;

process.exit(1);
}

function compileSync(sources, options) {
var optionsWithDefaults = prepareOptions(options, options.spawn || spawn.sync);
var pathToMake = options.pathToMake || compilerBinaryName;
var pathToElm = options.pathToElm || elmBinaryName;

try {
return runCompiler(sources, optionsWithDefaults, pathToMake);
return runCompiler(sources, optionsWithDefaults, pathToElm);
} catch (err) {
handleCompilerError(err, pathToMake);
throw compilerErrorToString(err, pathToElm);
}
}

function compile(sources, options) {
var optionsWithDefaults = prepareOptions(options, options.spawn || spawn);
var pathToMake = options.pathToMake || compilerBinaryName;
var pathToElm = options.pathToElm || elmBinaryName;


try {
return runCompiler(sources, optionsWithDefaults, pathToMake)
.on('error', function(err) {
handleError(pathToMake, err);

process.exit(1);
});
return runCompiler(sources, optionsWithDefaults, pathToElm)
.on('error', function(err) { throw(err); });
} catch (err) {
handleCompilerError(err, pathToMake);
throw compilerErrorToString(err, pathToElm);
}
}

Expand All @@ -124,7 +123,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");
Expand Down Expand Up @@ -164,33 +169,26 @@ function compileToStringSync(sources, options) {
return fs.readFileSync(file.path, {encoding: "utf8"});
}

function handleError(pathToMake, err) {
if (err.code === "ENOENT") {
console.error("Could not find Elm compiler \"" + pathToMake + "\". Is it installed?")
} else if (err.code === "EACCES") {
console.error("Elm compiler \"" + pathToMake + "\" did not have permission to run. Do you need to give it executable permissions?");
} else {
console.error("Error attempting to run Elm compiler \"" + pathToMake + "\":\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) {
case "yes": return ["--yes"];
case "help": return ["--help"];
case "output": return ["--output", value];
case "report": return ["--report", value];
case "warn": return ["--warn"];
case "debug": return ["--debug"];
case "docs": return ["--docs", value]
case "runtimeOptions": return [].concat(["+RTS"], value ,["-RTS"])
case "docs": return ["--docs", value];
case "optimize": return ["--optimize"];
case "runtimeOptions": return [].concat(["+RTS"], value ,["-RTS"]);
default:
if (supportedOptions.indexOf(opt) === -1) {
emitWarning('Unknown Elm compiler option: ' + opt);
if (opt === "yes") {
throw new Error('node-elm-compiler received the `yes` option, but that was removed in Elm 0.19. Try re-running without passing the `yes` option.');
} else {
throw new Error('node-elm-compiler was given an unrecognized Elm compiler option: ' + opt);
}
}

return [];
Expand Down
Loading

0 comments on commit 48f29fc

Please sign in to comment.