node-elm-compiler
Advanced tools
Comparing version 4.3.0 to 4.3.1
152
index.js
@@ -9,5 +9,3 @@ 'use strict'; | ||
var temp = require("temp").track(); | ||
var firstline = require("firstline"); | ||
var depsLoader = require('./dependencies.js'); | ||
var glob = require("glob"); | ||
var findAllDependencies = require("find-elm-dependencies").findAllDependencies; | ||
@@ -110,150 +108,2 @@ var defaultOptions = { | ||
function getBaseDir(file) { | ||
return firstline(file).then(function(line) { | ||
return new Promise(function(resolve, reject) { | ||
var matches = line.match(/^(?:port\s+)?module\s+([^\s]+)/); | ||
if (matches) { | ||
// e.g. Css.Declarations | ||
var moduleName = matches[1]; | ||
// e.g. Css/Declarations | ||
var dependencyLogicalName = moduleName.replace(/\./g, "/"); | ||
// e.g. ../.. | ||
var backedOut = dependencyLogicalName.replace(/[^/]+/g, ".."); | ||
// e.g. /.. | ||
var trimmedBackedOut = backedOut.replace(/^../, ""); | ||
return resolve(path.normalize(path.dirname(file) + trimmedBackedOut)); | ||
} else if (!line.match(/^(?:port\s+)?module\s/)) { | ||
// Technically you're allowed to omit the module declaration for | ||
// beginner applications where it'd just be `module Main exposing (..)` | ||
// If there is no module declaration, we'll assume we have one of these, | ||
// and succeed with the file's directory itself. | ||
// | ||
// See https://github.com/rtfeldman/node-elm-compiler/pull/36 | ||
return resolve(path.dirname(file)); | ||
} | ||
return reject(file + " is not a syntactically valid Elm module. Try running elm-make on it manually to figure out what the problem is."); | ||
}); | ||
}); | ||
} | ||
// Returns a Promise that returns a flat list of all the Elm files the given | ||
// Elm file depends on, based on the modules it loads via `import`. | ||
function findAllDependencies(file, knownDependencies, baseDir, knownFiles) { | ||
if (!knownDependencies) { | ||
knownDependencies = []; | ||
} | ||
if (typeof knownFiles === "undefined"){ | ||
knownFiles = []; | ||
} else if (knownFiles.indexOf(file) > -1){ | ||
return knownDependencies; | ||
} | ||
if (baseDir) { | ||
return findAllDependenciesHelp(file, knownDependencies, baseDir, knownFiles).then(function(thing){ | ||
return thing.knownDependencies; | ||
}); | ||
} else { | ||
return getBaseDir(file).then(function(newBaseDir) { | ||
return findAllDependenciesHelp(file, knownDependencies, newBaseDir, knownFiles).then(function(thing){ | ||
return thing.knownDependencies; | ||
}); | ||
}) | ||
} | ||
} | ||
function findAllDependenciesHelp(file, knownDependencies, baseDir, knownFiles) { | ||
return new Promise(function(resolve, reject) { | ||
// if we already know the file, return known deps since we won't learn anything | ||
if (knownFiles.indexOf(file) !== -1){ | ||
return resolve({ | ||
file: file, | ||
error: false, | ||
knownDependencies: knownDependencies | ||
}); | ||
} | ||
// read the imports then parse each of them | ||
depsLoader.readImports(file).then(function(lines){ | ||
// when lines is null, the file was not read so we just return what we know | ||
// and flag the error state | ||
if (lines === null){ | ||
return resolve({ | ||
file: file, | ||
error: true, | ||
knownDependencies: knownDependencies | ||
}); | ||
} | ||
var newImports = _.compact(lines.map(function(line) { | ||
var matches = line.match(/^import\s+([^\s]+)/); | ||
// if the line is not actually an import line | ||
if (!matches) { | ||
return null; | ||
} | ||
// e.g. Css.Declarations | ||
var moduleName = matches[1]; | ||
// e.g. Css/Declarations | ||
var dependencyLogicalName = moduleName.replace(/\./g, "/"); | ||
// all non-native modules are .elm | ||
var extension = ".elm"; | ||
// all native modules are .js | ||
if (moduleName.startsWith("Native.")){ | ||
extension = ".js"; | ||
} | ||
// e.g. ~/code/elm-css/src/Css/Declarations.elm | ||
var result = path.join(baseDir, dependencyLogicalName + extension); | ||
return _.includes(knownDependencies, result) ? null : result; | ||
})); | ||
knownFiles.push(file); | ||
var validDependencies = _.flatten(newImports); | ||
var newDependencies = knownDependencies.concat(validDependencies); | ||
var recursePromises = _.compact(validDependencies.map(function(dependency) { | ||
return path.extname(dependency) === ".elm" ? | ||
findAllDependenciesHelp(dependency, newDependencies, baseDir, knownFiles) : null; | ||
})); | ||
Promise.all(recursePromises).then(function(extraDependencies) { | ||
// keep track of files that weren't found in our src directory | ||
var externalPackageFiles = []; | ||
var justDeps = extraDependencies.map(function(thing){ | ||
// if we had an error, we flag the file as a bad thing | ||
if (thing.error){ | ||
externalPackageFiles.push(thing.file) | ||
return []; | ||
} | ||
return thing.knownDependencies; | ||
}); | ||
var flat = _.uniq(_.flatten(knownDependencies.concat(justDeps))).filter(function(file){ | ||
return externalPackageFiles.indexOf(file) === -1; | ||
}); | ||
resolve({ | ||
file: file, | ||
error: false, | ||
knownDependencies: flat | ||
}); | ||
}).catch(reject); | ||
}).catch(reject); | ||
}); | ||
} | ||
// write compiled Elm to a string output | ||
@@ -260,0 +110,0 @@ // returns a Promise which will contain a Buffer of the text |
{ | ||
"name": "node-elm-compiler", | ||
"version": "4.3.0", | ||
"description": "A Node.js interface to the Elm compiler binaries. Supports Elm version 0.17", | ||
"version": "4.3.1", | ||
"description": "A Node.js interface to the Elm compiler binaries. Supports Elm version 0.17, 0.18", | ||
"main": "index.js", | ||
@@ -29,3 +29,3 @@ "scripts": { | ||
"cross-spawn": "4.0.0", | ||
"firstline": "1.2.0", | ||
"find-elm-dependencies": "1.0.1", | ||
"lodash": "4.14.2", | ||
@@ -32,0 +32,0 @@ "temp": "^0.8.3" |
@@ -17,2 +17,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) | ||
## 4.3.1 | ||
Upgrade `findAllDependencies` dependency to correctly report all dependencies within a multi-source-directory project. | ||
## 4.3.0 | ||
@@ -19,0 +23,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
102
6
26978
31
509
+ Addedfind-elm-dependencies@1.0.1
+ Addedfind-elm-dependencies@1.0.1(transitive)
- Removedfirstline@1.2.0