Permalink
Browse files

Relative links! Fix #104. Fix #21.

All shims and linking uses the relativize function to calculate the shortest relative path from the source to destination.  In cases where there is no common ancestor in the paths, or if the relative path is more characters than the absolute path, then the absolute path is used instead.

This should make npm a LOT easier to integrate with programs like ivy and nave, which keep the binaries and modules in a managed location.
  • Loading branch information...
1 parent 99beafa commit 6662d6eff4cca7957dd9e82721c2656273737be8 @isaacs isaacs committed Jul 26, 2010
Showing with 19 additions and 8 deletions.
  1. +2 −1 lib/activate.js
  2. +2 −1 lib/build.js
  3. +2 −1 lib/utils/link.js
  4. +13 −5 lib/utils/write-shim.js
View
@@ -15,6 +15,7 @@ var mkdir = require("./utils/mkdir-p")
, readJson = require("./utils/read-json")
, link = require("./utils/link")
, linkIfExists = link.ifExists
+ , shimIfExists = require("./utils/write-shim").ifExists
module.exports = activate
@@ -70,7 +71,7 @@ function activate_ (arg, cb) {
chain
( [lifecycle, data, "preactivate"]
, [link, from, to]
- , [linkIfExists, fromMain, toMain]
+ , [shimIfExists, fromMain, toMain]
, [linkIfExists, fromLib, toLib]
, [linkBins, data]
, [lifecycle, data, "activate"]
View
@@ -24,6 +24,7 @@ var npm = require("../npm")
, writeShim = require("./utils/write-shim")
, link = require("./utils/link")
, linkIfExists = link.ifExists
+ , shimIfExists = writeShim.ifExists
module.exports = build
@@ -199,7 +200,7 @@ function dependencyLink (pkg, cb) {
, linkToMain = linkToLib ? linkToLib + ".js" : null
chain([ [ mkdir, dependsOn ]
, [ link, fromRoot, dependsOn ]
- , [ linkIfExists, fromMain, toMain ]
+ , [ shimIfExists, fromMain, toMain ]
, [ linkIfExists, fromLib, toLib ]
, linkToLib && [ linkIfExists, fromLib, linkToLib ]
, linkToMain && [ linkIfExists, fromMain, linkToMain ]
View
@@ -8,6 +8,7 @@ var fs = require("fs")
, rm = require("./rm-rf")
, log = require("./log")
, path = require("path")
+ , relativize = require("./relativize")
function linkIfExists (from, to, cb) {
fs.stat(from, function (er) {
@@ -21,7 +22,7 @@ function link (from, to, cb) {
( [fs, "stat", from]
, [rm, to]
, [mkdir, path.dirname(to)]
- , [fs, "symlink", from, to]
+ , [fs, "symlink", relativize(from, to), to]
, log.er(cb, "linking "+from+" to "+to, "failed")
)
}
@@ -1,12 +1,23 @@
module.exports = writeShim
+writeShim.ifExists = shimIfExists
var fs = require("fs")
, path = require("path")
+ , relativize = require("./relativize")
+
+function shimIfExists (from, to, dep, cb) {
+ if (!cb) cb = dep, dep = false
+ fs.stat(from, function (er) {
+ if (er) return cb()
+ writeShim(from, to, dep, cb)
+ })
+}
function writeShim (from, to, dep, cb) {
if (!cb) cb = dep, dep = false
- if (dep) dep = JSON.stringify(dep)
+ if (dep) dep = JSON.stringify(relativize(dep, to))
+ from = JSON.stringify(relativize(from, to).replace(/\.(js|node)$/, ''))
var nodePath = process.execPath
|| path.join(process.installPrefix, "bin", "node")
, code = "#!"+nodePath+"\n"
@@ -15,9 +26,7 @@ function writeShim (from, to, dep, cb) {
+ "if (!depMet) require.paths.unshift("+dep+")\n"
: ""
)
- + "module.exports = require("
- + JSON.stringify(from.replace(/\.(js|node)$/, ''))
- + ")\n"
+ + "module.exports = require(" + from + ")\n"
+ ( dep ? "if (!depMet) {\n" // get the possibly-changed location
+ " var i = require.paths.indexOf("+dep+")\n"
+ " if (i !== -1) require.paths.splice(i, 1)\n"
@@ -29,4 +38,3 @@ function writeShim (from, to, dep, cb) {
fs.chmod(to, 0755, cb)
})
}
-

0 comments on commit 6662d6e

Please sign in to comment.