Skip to content

Add a way to look up where the elm binaries are #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions examples/compileHelloWorld.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
var compile = require("../index.js").compile;
var compileToString = require("../index.js").compileToString;

var findElmBinaries = require("../index.js").findElmBinaries;

findElmBinaries().then(function(foundPath){
console.log(foundPath);
}).catch(function(err){
console.log(err);
});

compile(["./HelloWorld.elm"], {
output: "compiled-hello-world.js"
}).on('close', function(exitCode) {
Expand Down
22 changes: 21 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var fs = require("fs");
var path = require("path");
var temp = require("temp").track();
var findAllDependencies = require("find-elm-dependencies").findAllDependencies;
var which = require("which");

var defaultOptions = {
emitWarning: console.warn,
Expand Down Expand Up @@ -198,11 +199,30 @@ function compilerArgsFromOptions(options, emitWarning) {
}));
}

// Tries to locate where the Elm binaries are. First looks at node_modules, then globally
//
function findElmBinaries() {
let localElmBin = path.resolve(path.join("node_modules", ".bin", "elm-make"));

return new Promise(function(resolve, reject) {
fs.access(localElmBin, function(err) {
if (!err) return resolve(localElmBin);

which('elm-make', function(err, path) {
if (err) return reject("Unable to find Elm anywhere!");

return resolve(path);
});
})
});
}

module.exports = {
compile: compile,
compileSync: compileSync,
compileWorker: require("./worker.js")(compile),
compileToString: compileToString,
compileToStringSync: compileToStringSync,
findAllDependencies: findAllDependencies
findAllDependencies: findAllDependencies,
findElmBinaries: findElmBinaries
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this name is bad?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about preferedElmBinaries or elmBinariesLocation?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, based on the name I'd be surprised to learn this was searching node_modules first... 🤔

That said, I haven't thought of a better one. 😅

};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"cross-spawn": "4.0.0",
"find-elm-dependencies": "1.0.2",
"lodash": "4.14.2",
"temp": "^0.8.3"
"temp": "^0.8.3",
"which": "^1.3.0"
},
"devDependencies": {
"chai": "3.5.0",
Expand Down