Permalink
Browse files

Initial implementation of package 'modules.' Issue 144.

  • Loading branch information...
1 parent 6310180 commit 9e6031019f6ae136ebd8bf5f520837dc36cce27f @alexkwolfe alexkwolfe committed with isaacs Aug 13, 2010
Showing with 79 additions and 39 deletions.
  1. +7 −0 doc/folders.md
  2. +1 −1 lib/activate.js
  3. +62 −33 lib/build.js
  4. +8 −4 lib/utils/write-shim.js
  5. +1 −1 package.json
View
@@ -15,6 +15,13 @@ version 1.0.0
foo. This is a shim, not a symbolic link, so that relative paths will work
appropriately.
+`root/.npm/foo/1.0.0/**/*.js` Generated shims for each `.js` file in
+`root/.npm/foo/1.0.0/package/lib` These are only generated if the
+directories lib option is used.
+
+`root/.npm/foo/1.0.0/{module-name}.js` Generated shim corresponding to a module
+defined in the modules option.
+
`root/.npm/foo/active` symlink to the active version.
`root/foo-1.0.0.js` shim to `.npm/foo/1.0.0/main.js`
View
@@ -46,7 +46,7 @@ function activate_ (arg, cb) {
, to = path.join(npm.dir, pkg, "active")
, fromMain = path.join(npm.dir, pkg, "active/main.js")
, toMain = path.join(npm.root, pkg+".js")
- , fromLib = path.join(npm.dir, pkg, "active/lib")
+ , fromLib = path.join(npm.dir, pkg, "active")
, toLib = path.join(npm.root, pkg)
, jsonFile = path.join(npm.dir, pkg, version, "/package/package.json")
View
@@ -57,9 +57,12 @@ function buildStep (pkg, cb) {
// symlink ROOT/{name}-{version}.js to ROOT/.npm/{name}/{version}/main.js
, [linkMain, pkg]
- // symlink ROOT/{name}-{version}/ to ROOT/.npm/{name}/{version}/{lib}
+ // shim ROOT/{name}-{version}/**/*.js to ROOT/.npm/{name}/{version}/{lib}/**/*.js
, [linkLib, pkg]
+ // shim ROOT/{name}-{version}/{module-name} to ROOT/.npm/{name}/{version}/package/{module-path}
+ , [linkModules, pkg]
+
// symlink any bins into the node install prefix
, [linkBins, pkg]
@@ -227,7 +230,7 @@ function dependencyLink (pkg, cb) {
function createMain (pkg,cb) {
if (!pkg.main) return cb()
- log(pkg.main, "createMain")
+ log(pkg.main, "main")
writeShim
( path.join(npm.dir, pkg.name, pkg.version, "package", pkg.main)
, path.join(npm.dir, pkg.name, pkg.version, "main.js")
@@ -236,55 +239,81 @@ function createMain (pkg,cb) {
)
}
-// symlink ROOT/{name}-{version}/ to ROOT/.npm/{name}/{version}/{lib}
+// symlink ROOT/{name}-{version}/{key} to ROOT/.npm/{name}/{version}/package/{value}
+function linkModules(pkg, cb) {
+ var mod = pkg.modules;
+ if (!mod) 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),
+ to = path.join(versionDir, key);
+ return function(done) { shim(from, to, done); }
+ });
+ calls.push(cb);
+ chain(calls);
+}
+
+// symlink ROOT/{name}-{version}/**/*.js to ROOT/.npm/{name}/{version}/{lib}/**/*.js
function linkLib (pkg, cb) {
var lib = pkg.directories && pkg.directories.lib || pkg.lib
if (!lib) return cb()
log(pkg._id, "linkLib")
log(" ", "! WARNING !")
- log("Symlinking the lib directory is deprecated", "! WARNING !")
- log("Please don't rely on this feature, as it will be removed.", "! WARNING !")
- log("Use the 'main' module instead.", "! WARNING !")
+ log("The package 'directories' feature is deprecated and will be removed.", "! WARNING !")
+ log("Use the package 'modules' feature or the 'main' module instead.", "! WARNING !")
log(" ", "! WARNING !")
+
var from = path.join(npm.dir, pkg.name, pkg.version, "package", lib)
- , toInternal = path.join(npm.dir, pkg.name, pkg.version, "lib")
- , to = path.join(npm.root, pkg.name+"-"+pkg.version)
+ , to = path.join(npm.dir, pkg.name, pkg.version)
- function doLink (er) {
- if (er) return cb(er)
- chain
- ( [rm, toInternal]
- , [rm, to]
- , function (cb) { link(from, toInternal, function (er) {
- if (er) return cb(er)
- link(toInternal, to, cb)
- })}
- , cb
- )
+ var calls = walk(from).map(function(filename) {
+ return function(done) {
+ shim(filename, to + filename.replace(from, ''), done)
+ };
+ });
+ calls.push(cb);
+ chain(calls);
+}
+
+function walk(filename){
+ var filenames = [];
+ var stats = fs.statSync(filename);
+ if(stats.isFile() && filename.match(/\.js$/)) {
+ // Filename
+ 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);
+ });
+ }
}
+ return filenames;
+}
- fs.stat(from, function (er, s) {
- if (er) return cb(new Error("Libs dir not found "+from))
- if (!s.isDirectory()) return cb(new Error("Libs dir not a dir: "+lib))
- // make sure that it doesn't already exist. If so, rm it.
- fs.lstat(to, function (er, s) {
- if (!er) fs.unlink(to, doLink)
- else doLink()
+function shim(from, to, cb) {
+ fs.lstat(to, function (er) {
+ if (!er) rm(to, function (er) {
+ if (er) cb(er)
+ else shim(from, to, cb)
})
+ else writeShim(from, to, cb)
})
}
function linkMain (pkg, cb) {
if (!pkg.main) return cb()
var from = path.join(npm.dir, pkg.name, pkg.version, "main.js")
, to = path.join(npm.root, pkg.name+"-"+pkg.version+".js")
- fs.lstat(to, function (er) {
- if (!er) rm(to, function (er) {
- if (er) cb(er)
- else linkMain(pkg, cb)
- })
- else writeShim(from, to, cb)
- })
+ shim(from, to, cb);
}
function linkBins (pkg, cb) {
@@ -5,6 +5,7 @@ writeShim.ifExists = shimIfExists
var fs = require("fs")
, path = require("path")
, relativize = require("./relativize")
+ , mkdir = require('./mkdir-p')
function shimIfExists (from, to, dep, cb) {
if (!cb) cb = dep, dep = false
@@ -33,8 +34,11 @@ function writeShim (from, to, dep, cb) {
+ "}"
: "\n"
)
- fs.writeFile(to, code, "ascii", function (er, ok) {
- if (er) return cb(er)
- fs.chmod(to, 0755, cb)
- })
+ mkdir(to.substr(0, to.lastIndexOf("/")), function(er) {
+ if (er) return cb(er);
+ fs.writeFile(to, code, "ascii", function (er, ok) {
+ if (er) return cb(er)
+ fs.chmod(to, 0755, cb)
+ })
+ })
}
View
@@ -1,6 +1,6 @@
{ "name" : "npm"
, "description" : "A package manager for node"
-, "version" : "0.1.25"
+, "version" : "0.1.25modules"
, "homepage" : "http://npmjs.org/"
, "author" : "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me)"
, "contributors" :

0 comments on commit 9e60310

Please sign in to comment.