Skip to content

Commit

Permalink
Some refactorings + update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svanderburg committed Mar 27, 2017
1 parent 55f6c11 commit e41a6ef
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 159 deletions.
8 changes: 7 additions & 1 deletion lib/generator/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ function generateCollectionExpr(packageSet, dependencies, baseDir, production, i
slasp.sequence([
function(callback) {
/* Resolve the dependencies of each package */
packageSet.resolveDependencies(resolvedDependencies, null, dependency, flatten, callback);
var metadata = {
parent: null,
baseDir: "./.",
boundDependencies: null // Dependencies cannot be bound on this level
};

packageSet.resolveDependencies(resolvedDependencies, metadata, dependency, flatten, callback);
},

function(callback) {
Expand Down
35 changes: 10 additions & 25 deletions lib/generator/composition.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
var path = require('path');
var nijs = require('nijs');

function mustPrefix(target) {
/*
* Prefixes a relative path with ./ if needed so that it can be converted to a
* value belonging to Nix's file type
*/
function prefixRelativePath(target) {
if(path.isAbsolute(target) || target.substring(0, 2) == "./" || target.substring(0, 3) == "../")
return false;
return target;
else
return true;
return "./" + target;
}

/**
Expand All @@ -21,31 +25,12 @@ function mustPrefix(target) {
*/

function generateCompositionExpr(nodeEnvNix, nodePackage, packagesNix, supplementNix, generateSupplement) {
/* Prefix relative paths if needed */
var nodeEnvNixPath;
var packagesNixPath;
var globalBuildInputs;

if(mustPrefix(nodeEnvNix)) {
nodeEnvNixPath = "./" + nodeEnvNix;
} else {
nodeEnvNixPath = nodeEnvNix;
}

if(mustPrefix(packagesNix)) {
packagesNixPath = "./" + packagesNix;
} else {
packagesNixPath = packagesNix;
}
var nodeEnvNixPath = prefixRelativePath(nodeEnvNix);
var packagesNixPath = prefixRelativePath(packagesNix);

if(generateSupplement) {
var supplementNixPath;

if(mustPrefix(supplementNix)) {
supplementNixPath = "./" + supplementNix;
} else {
supplementNixPath = supplementNix;
}
var supplementNixPath = prefixRelativePath(supplementNix);

globalBuildInputs = new nijs.NixFunInvocation({
funExpr: new nijs.NixExpression("pkgs.lib.attrValues"),
Expand Down
5 changes: 2 additions & 3 deletions lib/generator/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ var nijs = require('nijs');
* @param {PackageSet} packageSet Maintains a set of packages and their dependencies
* @param {Object} packageObj A package.json configuration file of a package
* @param {String} baseDir Directory in which the referrer's package.json configuration resides
* @param {Object} src Directory in which the package.json file resides
* @param {Boolean} production Indicates whether to deploy the package in production mode
* @param {Boolean} includePeerDependencies Indicates whether to include peer dependencies with the package
* @param {Boolean} flatten Indicates whether to create a flat dependency structure in which dependencies are as high as possible in the graph
* @param {function(String,Object)} callback Callback function that gets invoked when the work is done.
* If an error occurs, the error parameter is set to contain the error
* If the operation succeeds, it returns an object containing the abstract syntax of a Nix expression
*/
function generatePackageExpr(packageSet, packageObj, baseDir, src, production, includePeerDependencies, flatten, callback) {
function generatePackageExpr(packageSet, packageObj, baseDir, production, includePeerDependencies, flatten, callback) {
/* Construct metadata object for the package that we want to deploy */
var metadata = {
packageObj: packageObj,
parent: null,
src: src,
src: new nijs.NixFile({ value: "./." }),
baseDir: baseDir
};

Expand Down
2 changes: 1 addition & 1 deletion lib/node2nix.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function npmToNix(inputJSON, outputNix, compositionNix, nodeEnvNix, supplementJS
if(Array.isArray(obj)) {
collectionGenerator.generateCollectionExpr(packageSet, obj, baseDir, production, includePeerDependencies, flatten, callback);
} else {
packageGenerator.generatePackageExpr(packageSet, obj, baseDir, new nijs.NixFile({ value: "./." }), production, includePeerDependencies, flatten, callback);
packageGenerator.generatePackageExpr(packageSet, obj, baseDir, production, includePeerDependencies, flatten, callback);
}
} else {
callback("The provided JSON file must consist of an object or an array");
Expand Down
24 changes: 7 additions & 17 deletions lib/packageset.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ PackageSet.prototype.findConformantParent = function(metadata, dependencyName, v
/**
* Checks whether the dependency conflicts when it binds to a package.
*
* @param {String} property Which property to check: resolvedDependencies or boundDependenices
* @param {String} property Which property to check: either resolvedDependencies or boundDependenices
* @param {Object} metadata Metadata of the package to bind a dependency to
* @param {Object} dependencyMetadata Metadata data of a dependency package
* @return {Boolean} true if the package conflicts, else false
*/
PackageSet.prototype.hasConflict = function(property, metadata, dependencyMetadata) {
if(metadata.parent === null || metadata.parent === undefined) {
Expand Down Expand Up @@ -151,7 +152,7 @@ PackageSet.prototype.hasConflict = function(property, metadata, dependencyMetada
* @param {Boolean} flatten Whether to bind to dependency as high as possible in the directory structure
*/
PackageSet.prototype.bindDependency = function(metadata, dependencyMetadata, flatten) {
if(metadata !== null) {
if(metadata.boundDependencies !== null) {
// Bind the package to the given dependency
if(metadata.boundDependencies === undefined) {
metadata.boundDependencies = {};
Expand Down Expand Up @@ -222,22 +223,13 @@ PackageSet.prototype.resolveDependencies = function(resolvedDependencies, metada
if(isBundledDependency(metadata, dependencyName)) { // Only include dependencies that are not bundled
callback();
} else {
var parentDependency;
var baseDir;

if(metadata === null) {
parentDependency = null;
baseDir = "./.";
} else {
parentDependency = self.findConformantParent(metadata.parent, dependencyName, versionSpec);
baseDir = metadata.baseDir;
}
var parentDependency = self.findConformantParent(metadata.parent, dependencyName, versionSpec);

if(parentDependency === null) { // Only include a dependency, if none of the parents can provide the package that matches the version specifier
slasp.sequence([
function(callback) {
// Fetch package meta data from an external source
self.fetchMetaData(baseDir, dependencyName, versionSpec, callback);
self.fetchMetaData(metadata.baseDir, dependencyName, versionSpec, callback);
},

function(callback, dependencyMetadata) {
Expand Down Expand Up @@ -418,7 +410,7 @@ PackageSet.prototype.generatePackageArgsExpr = function(metadata, production, in
homepage = new nijs.NixURL(metadata.packageObj.homepage);
}

var expr = {
callback(null, {
name: metadata.packageObj.name.replace("@", "_at_").replace("/", "_slash_"), // Escape characters from scoped package names that aren't allowed
packageName: metadata.packageObj.name,
version: metadata.packageObj.version,
Expand All @@ -431,9 +423,7 @@ PackageSet.prototype.generatePackageArgsExpr = function(metadata, production, in
license: metadata.packageObj.license
},
production: production
};

callback(null, expr);
});
}
], callback);
};
Expand Down
21 changes: 11 additions & 10 deletions node-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -481,13 +481,13 @@ let
sha1 = "1296a2d58fd45f19a0f6ce01d65701e2c735b6eb";
};
};
"mime-types-2.1.14" = {
"mime-types-2.1.15" = {
name = "mime-types";
packageName = "mime-types";
version = "2.1.14";
version = "2.1.15";
src = fetchurl {
url = "https://registry.npmjs.org/mime-types/-/mime-types-2.1.14.tgz";
sha1 = "f7ef7d97583fcaf3b7d282b6f8b5679dab1e94ee";
url = "https://registry.npmjs.org/mime-types/-/mime-types-2.1.15.tgz";
sha1 = "a4ebf5064094569237b8cf70046776d09fc92aed";
};
};
"oauth-sign-0.8.2" = {
Expand Down Expand Up @@ -796,13 +796,13 @@ let
sha1 = "63bc5dcb61331b92bc05fd528953c33462a06f8d";
};
};
"mime-db-1.26.0" = {
"mime-db-1.27.0" = {
name = "mime-db";
packageName = "mime-db";
version = "1.26.0";
version = "1.27.0";
src = fetchurl {
url = "https://registry.npmjs.org/mime-db/-/mime-db-1.26.0.tgz";
sha1 = "eaffcd0e4fc6935cf8134da246e2e6c35305adff";
url = "https://registry.npmjs.org/mime-db/-/mime-db-1.27.0.tgz";
sha1 = "820f572296bbd20ec25ed55e5b5de869e5436eb1";
};
};
"punycode-1.4.1" = {
Expand Down Expand Up @@ -1361,9 +1361,9 @@ let
sources."is-typedarray-1.0.0"
sources."isstream-0.1.2"
sources."json-stringify-safe-5.0.1"
(sources."mime-types-2.1.14" // {
(sources."mime-types-2.1.15" // {
dependencies = [
sources."mime-db-1.26.0"
sources."mime-db-1.27.0"
];
})
sources."oauth-sign-0.8.2"
Expand Down Expand Up @@ -1572,6 +1572,7 @@ let
meta = {
description = "Generate Nix expressions to build NPM packages";
homepage = https://github.com/svanderburg/node2nix;
license = "MIT";
};
production = true;
};
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,5 @@
"type": "git",
"url": "https://github.com/svanderburg/node2nix"
},
"licenses": [ {
"type": "MIT",
"url": "https://github.com/svanderburg/node2nix/blob/master/LICENSE"
} ]
"license": "MIT"
}
20 changes: 10 additions & 10 deletions tests/grunt/node-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1237,22 +1237,22 @@ let
sha1 = "8710d7af0aa626f8fffa1ce00168545263255748";
};
};
"mime-types-2.1.14" = {
"mime-types-2.1.15" = {
name = "mime-types";
packageName = "mime-types";
version = "2.1.14";
version = "2.1.15";
src = fetchurl {
url = "https://registry.npmjs.org/mime-types/-/mime-types-2.1.14.tgz";
sha1 = "f7ef7d97583fcaf3b7d282b6f8b5679dab1e94ee";
url = "https://registry.npmjs.org/mime-types/-/mime-types-2.1.15.tgz";
sha1 = "a4ebf5064094569237b8cf70046776d09fc92aed";
};
};
"mime-db-1.26.0" = {
"mime-db-1.27.0" = {
name = "mime-db";
packageName = "mime-db";
version = "1.26.0";
version = "1.27.0";
src = fetchurl {
url = "https://registry.npmjs.org/mime-db/-/mime-db-1.26.0.tgz";
sha1 = "eaffcd0e4fc6935cf8134da246e2e6c35305adff";
url = "https://registry.npmjs.org/mime-db/-/mime-db-1.27.0.tgz";
sha1 = "820f572296bbd20ec25ed55e5b5de869e5436eb1";
};
};
"ms-0.7.1" = {
Expand Down Expand Up @@ -1691,9 +1691,9 @@ let
(sources."type-is-1.6.14" // {
dependencies = [
sources."media-typer-0.3.0"
(sources."mime-types-2.1.14" // {
(sources."mime-types-2.1.15" // {
dependencies = [
sources."mime-db-1.26.0"
sources."mime-db-1.27.0"
];
})
];
Expand Down
16 changes: 8 additions & 8 deletions tests/grunt/supplement.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1237,22 +1237,22 @@ let
sha1 = "8710d7af0aa626f8fffa1ce00168545263255748";
};
};
"mime-types-2.1.14" = {
"mime-types-2.1.15" = {
name = "mime-types";
packageName = "mime-types";
version = "2.1.14";
version = "2.1.15";
src = fetchurl {
url = "https://registry.npmjs.org/mime-types/-/mime-types-2.1.14.tgz";
sha1 = "f7ef7d97583fcaf3b7d282b6f8b5679dab1e94ee";
url = "https://registry.npmjs.org/mime-types/-/mime-types-2.1.15.tgz";
sha1 = "a4ebf5064094569237b8cf70046776d09fc92aed";
};
};
"mime-db-1.26.0" = {
"mime-db-1.27.0" = {
name = "mime-db";
packageName = "mime-db";
version = "1.26.0";
version = "1.27.0";
src = fetchurl {
url = "https://registry.npmjs.org/mime-db/-/mime-db-1.26.0.tgz";
sha1 = "eaffcd0e4fc6935cf8134da246e2e6c35305adff";
url = "https://registry.npmjs.org/mime-db/-/mime-db-1.27.0.tgz";
sha1 = "820f572296bbd20ec25ed55e5b5de869e5436eb1";
};
};
"ms-0.7.1" = {
Expand Down
Loading

0 comments on commit e41a6ef

Please sign in to comment.