Skip to content
Browse files

Mostly style cleanup, but also use the linkModules function for "link…

…" dependencies, and go back to the previous pattern of linking into a name-version folder
  • Loading branch information...
1 parent ebc0854 commit 7eab543a13db0ea0a7ec27be23bb444204097822 @isaacs isaacs committed
Showing with 66 additions and 60 deletions.
  1. +66 −60 lib/build.js
View
126 lib/build.js
@@ -57,8 +57,9 @@ function buildStep (pkg, cb) {
// symlink ROOT/{name}-{version}.js to ROOT/.npm/{name}/{version}/main.js
, [linkMain, pkg]
- // shim ROOT/{name}-{version}/{module-name} to ROOT/.npm/{name}/{version}/package/{module-path}
- , [linkModules, pkg]
+ // shim ROOT/.npm/{name}-{version}/{module-name}
+ // to ROOT/.npm/{name}/{version}/package/{module}
+ , [linkModules, pkg, path.join(npm.root, pkg.name+"-"+pkg.version)]
// symlink any bins into the node install prefix
, [linkBins, pkg]
@@ -217,8 +218,8 @@ function dependencyLink (pkg, cb) {
chain([ [ mkdir, dependsOn ]
, [ link, fromRoot, dependsOn ]
, [ shimIfExists, fromMain, toMain ]
- , [ linkIfExists, fromLib, toLib ]
- , linkToLib && [ linkIfExists, fromLib, linkToLib ]
+ , [ linkModules, dep, toLib ]
+ , linkToLib && [ linkModules, dep, linkToLib ]
, linkToMain && [ shimIfExists, fromMain, linkToMain ]
, cb
].filter(function (_) { return _ }))
@@ -236,86 +237,91 @@ function createMain (pkg,cb) {
)
}
-// shim ROOT/{name}-{version}/{module-name} to ROOT/.npm/{name}/{version}/package/{module-path}
-function linkModules(pkg, cb) {
- var mod = pkg.modules;
- if (!mod) {
- var lib = pkg.directories && pkg.directories.lib || pkg.lib
- if (lib) {
- log("The package 'directories' feature is deprecated and will be removed.", "! WARNING !");
- log("Use the package 'modules' feature instead.", "! WARNING !");
- return linkDefaultModules(pkg, cb);
- } else
- return cb();
- }
- log(Object.keys(mod).map(function(k) { return mod[k]; }), "modules");
-
- var pkgDir = path.join(npm.dir, pkg.name, pkg.version, 'package');
- var versionDir = path.join(npm.dir, pkg.name, pkg.version);
-
- var calls = Object.keys(mod).map(function(key) {
- var value = mod[key] + ".js";
- key = key + ".js";
- var from = path.join(pkgDir, value),
- dep = path.join(npm.dir, pkg.name, pkg.version, "dependencies"),
- to = path.join(versionDir, key);
- return function(done) { shim(from, to, dep, done); }
- });
- calls.push(cb);
- chain(calls);
+// shim {target}/{module-name} to ROOT/.npm/{name}/{version}/package/{module-path}
+function linkModules (pkg, target, cb) {
+ var mod = pkg.modules
+ if (!mod) {
+ var lib = pkg.directories && pkg.directories.lib || pkg.lib
+ if (!lib) return cb()
+ log("Auto-linking the 'directories.lib' is deprecated and will be removed.", "! WARNING !")
+ log("Use the package 'modules' feature instead.", "! WARNING !")
+ return linkDefaultModules(pkg, lib, target, cb)
+ }
+
+ var versionDir = path.join(npm.dir, pkg.name, pkg.version)
+ , pkgDir = path.join(versionDir, "package")
+
+ // todo: don't use chain here. do them concurrently.
+ chain(Object.keys(mod).map(function (key) {
+ var value = mod[key] + ".js"
+ key = key + ".js"
+ var from = path.join(pkgDir, value)
+ , dep = path.join(versionDir, "dependencies")
+ , to = path.join(target, key)
+ return function (done) { shim(from, to, dep, done) }
+ }).concat(cb))
}
+/////
+///// DEPRECATED
+///// FIXME: remove by 0.2.0
+/////
// shim ROOT/{name}-{version}/**/*.js to ROOT/.npm/{name}/{version}/{lib}/**/*.js
-function linkDefaultModules(pkg, cb) {
- var from = path.join(npm.dir, pkg.name, pkg.version, "package", "lib")
- , dep = path.join(npm.dir, pkg.name, pkg.version, "dependencies")
- , to = path.join(npm.dir, pkg.name, pkg.version)
+function linkDefaultModules(pkg, lib, target, cb) {
+ var pkgDir = path.join(npm.dir, pkg.name, pkg.version, "package")
+ , libDir = path.join(pkgDir, lib)
- var calls = walk(from).map(function(filename) {
- return function(done) {
- shim(filename, to + filename.replace(from, ''), dep, done)
- };
- });
- calls.push(cb);
- chain(calls);
+ // create a modules hash from the lib folder.
+ // this sucks, and is going away eventually.
+ pkg.modules = {}
+ walk(libDir).map(function (filename) {
+ filename = filename.replace(/\.(js|node)$/, '')
+ pkg.modules[filename.substr(libDir.length + 1)] = filename.substr(pkgDir.length + 1)
+ })
+ return linkModules(pkg, target, cb)
}
-
-function walk(filename){
- var filenames = [];
- var stats = fs.statSync(filename);
- if(stats.isFile() && filename.match(/\.js$/)) {
+function walk (filename) {
+ var filenames = []
+ , stats = fs.statSync(filename)
+ if(stats.isFile() && filename.match(/\.(js|node)$/)) {
// Filename
- filenames.push(filename);
- } else if(stats.isDirectory()) {
+ filenames.push(filename)
+ } else if (stats.isDirectory()) {
// Directory - walk recursive
- var files = fs.readdirSync(filename);
- for(var i = 0; i < files.length; i++) {
- walk(filename + '/' + files[i]).forEach(function(fn) {
- filenames.push(fn);
- });
+ var files = fs.readdirSync(filename)
+ for (var i = 0; i < files.length; i++) {
+ walk(filename + '/' + files[i]).forEach(function (fn) {
+ filenames.push(fn)
+ })
}
}
- return filenames;
+ return filenames
}
+///// end
+///// DEPRECATED
+///// FIXME: remove by 0.1.30
+/////
+
+
-function shim(from, to, dep, cb) {
+function shim (from, to, dep, cb) {
fs.lstat(to, function (er) {
if (!er) rm(to, function (er) {
if (er) cb(er)
else shim(from, to, dep, cb)
})
else {
- writeShim(from, to, dep, cb)
- }
+ writeShim(from, to, dep, cb)
+ }
})
}
function linkMain (pkg, cb) {
if (!pkg.main) return cb()
var from = path.join(npm.dir, pkg.name, pkg.version, "main.js")
- , dep = path.join(npm.dir, pkg.name, pkg.version, "dependencies")
+ , dep = path.join(npm.dir, pkg.name, pkg.version, "dependencies")
, to = path.join(npm.root, pkg.name+"-"+pkg.version+".js")
- shim(from, to, dep, cb);
+ shim(from, to, dep, cb)
}
function linkBins (pkg, cb) {

0 comments on commit 7eab543

Please sign in to comment.
Something went wrong with that request. Please try again.