Permalink
Browse files

Replace relativize with path.relative

  • Loading branch information...
1 parent aaf7e05 commit 0892bab0080217b374ba58f8fa68f412b383d3e6 @isaacs isaacs committed Jun 12, 2012
Showing with 8 additions and 102 deletions.
  1. +3 −5 lib/install.js
  2. +1 −2 lib/link.js
  3. +0 −2 lib/ls.js
  4. +1 −2 lib/outdated.js
  5. +1 −2 lib/pack.js
  6. +1 −2 lib/utils/cmd-shim.js
  7. +1 −5 lib/utils/link.js
  8. +0 −81 lib/utils/relativize.js
  9. +0 −1 lib/utils/tar.js
View
@@ -64,7 +64,6 @@ var npm = require("./npm.js")
, cache = require("./cache.js")
, asyncMap = require("slide").asyncMap
, chain = require("slide").chain
- , relativize = require("./utils/relativize.js")
, output
, url = require("url")
, mkdir = require("mkdirp")
@@ -646,14 +645,13 @@ function localLink (target, where, context, cb) {
function resultList (target, where, parentId) {
var nm = path.resolve(where, "node_modules")
, targetFolder = path.resolve(nm, target.name)
- , prettyWhere = relativize(where, process.cwd() + "/x")
+ , prettyWhere = path.relative(process.cwd(), where)
if (prettyWhere === ".") prettyWhere = null
if (!npm.config.get("global")) {
// print out the folder relative to where we are right now.
- // relativize isn't really made for dirs, so you need this hack
- targetFolder = relativize(targetFolder, process.cwd()+"/x")
+ targetFolder = path.relative(process.cwd(), targetFolder)
}
return [ target._id
@@ -666,7 +664,7 @@ function resultList (target, where, parentId) {
function installOne_ (target, where, context, cb) {
var nm = path.resolve(where, "node_modules")
, targetFolder = path.resolve(nm, target.name)
- , prettyWhere = relativize(where, process.cwd() + "/x")
+ , prettyWhere = path.relative(process.cwd, where)
, parent = context.parent
if (prettyWhere === ".") prettyWhere = null
View
@@ -8,7 +8,6 @@ var npm = require("./npm.js")
, asyncMap = require("slide").asyncMap
, chain = require("slide").chain
, path = require("path")
- , relativize = require("./utils/relativize.js")
, rm = require("rimraf")
, output = require("./utils/output.js")
, build = require("./build.js")
@@ -146,7 +145,7 @@ function linkPkg (folder, cb_) {
function resultPrinter (pkg, src, dest, rp, cb) {
if (typeof cb !== "function") cb = rp, rp = null
- var where = relativize(dest, path.resolve(process.cwd(),"x"))
+ var where = path.relative(process.cwd(), dest)
rp = (rp || "").trim()
src = (src || "").trim()
// XXX If --json is set, then look up the data from the package.json
View
@@ -11,7 +11,6 @@ var npm = require("./npm.js")
, readInstalled = require("read-installed")
, output = require("./utils/output.js")
, log = require("npmlog")
- , relativize = require("./utils/relativize.js")
, path = require("path")
, archy = require("archy")
@@ -213,7 +212,6 @@ function makeArchy_ (data, long, dir, depth, parent, d) {
function getExtras (data, dir) {
var extras = []
- , rel = relativize(data.path || "", dir)
, url = require("url")
if (data.description) extras.push(data.description)
View
@@ -25,7 +25,6 @@ var path = require("path")
, asyncMap = require("slide").asyncMap
, npm = require("./npm.js")
, semver = require("semver")
- , relativize = require("./utils/relativize.js")
function outdated (args, silent, cb) {
if (typeof cb !== "function") cb = silent, silent = false
@@ -61,7 +60,7 @@ function makePretty (p) {
}
if (!npm.config.get("global")) {
- dir = relativize(dir, process.cwd()+"/x")
+ dir = path.relative(process.cwd(), dir)
}
return dep + "@" + want + " " + dir
+ " current=" + (has || "MISSING")
View
@@ -11,7 +11,6 @@ var npm = require("./npm.js")
, fs = require("graceful-fs")
, chain = require("slide").chain
, path = require("path")
- , relativize = require("./utils/relativize.js")
, cwd = process.cwd()
pack.usage = "npm pack <pkg>"
@@ -34,7 +33,7 @@ function pack (args, silent, cb) {
function printFiles (files, cb) {
files = files.map(function (file) {
- return relativize(file, cwd)
+ return path.relative(cwd, file)
})
output.write(files.join("\n"), cb)
}
View
@@ -18,7 +18,6 @@ var fs = require("graceful-fs")
, rm = require("rimraf")
, log = require("npmlog")
, path = require("path")
- , relativize = require("./relativize.js")
, npm = require("../npm.js")
, shebangExpr = /^#\!\s*(?:\/usr\/bin\/env)?\s*([^ \t]+)(.*)$/
@@ -60,7 +59,7 @@ function writeShim (from, to, cb) {
}
function writeShim_ (from, to, prog, args, cb) {
- var shTarget = relativize(from, to)
+ var shTarget = from
, target = shTarget.split("/").join("\\")
, longProg
, shProg = prog
View
@@ -7,7 +7,6 @@ var fs = require("graceful-fs")
, mkdir = require("mkdirp")
, rm = require("./gently-rm.js")
, path = require("path")
- , relativize = require("./relativize.js")
, npm = require("../npm.js")
function linkIfExists (from, to, gently, cb) {
@@ -21,13 +20,10 @@ function link (from, to, gently, cb) {
if (typeof cb !== "function") cb = gently, gently = null
if (npm.config.get("force")) gently = false
- // junction symlinks on windows must be absolute
- var rel = process.platform === "win32" ? from : relativize(from, to)
-
chain
( [ [fs, "stat", from]
, [rm, to, gently]
, [mkdir, path.dirname(to)]
- , [fs, "symlink", rel, to, "junction"] ]
+ , [fs, "symlink", from, to, "junction"] ]
, cb)
}
View
@@ -1,81 +0,0 @@
-
-module.exports = relativize
-
-// return the shortest path between two folders.
-// if the original path is shorter, then use that,
-// unless forceRelative is set to true.
-var path = require("path")
-function relativize (dest, src, forceRelative) {
- var orig = dest
- if (!isAbsolute(src)) forceRelative = true
- else if (!isAbsolute(dest)) return false
- src = path.resolve(src)
- dest = path.resolve(dest)
- if (src === dest) return "."
- src = src.split(split)
- dest = dest.split(split)
- var i = 0
- while (src[i] === dest[i]) i++
- if (!forceRelative && i === 1) return orig // nothing in common
- src.splice(0, i + 1)
- var dots = [0, i, "."]
- for (var i = 0, l = src.length; i < l; i ++) dots.push("..")
- dest.splice.apply(dest, dots)
- if (dest[0] === "." && dest[1] === "..") dest.shift()
- dest = dest.join("/")
- return !forceRelative && orig.length < dest.length ? orig : dest
-}
-
-var split = process.platform === "win32" ? /[\/\\]/ : "/"
-
-function isAbsolute (p) {
- if (process.platform !== "win32") return p.charAt(0) === "/"
-
-
- // yanked from node/lib/path.js
- var splitDeviceRe =
- /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?([\\\/])?([\s\S]*?)$/
-
- var result = p.match(splitDeviceRe)
- , device = result[1] || ""
- , isUnc = device && device.charAt(1) !== ":"
- , isAbs = !!result[2] || isUnc // UNC always absolute
-
- return isAbs
-}
-
-if (module === require.main) {
- // from, to, result, relativeForced
- var assert = require("assert")
-
- ; [ ["/bar" ,"/foo" ,"/bar" ,"./bar" ]
- , ["/foo/baz" ,"/foo/bar/baz" ,"../baz" ,"../baz" ]
- , ["/a/d" ,"/a/b/c/d/e/f" ,"/a/d" ,"../../../../d" ]
- // trailing slashes are ignored.
- , ["/a/d" ,"/a/b/c/d/e/" ,"/a/d" ,"../../../d" ]
- , ["./foo/bar" ,"./foo/baz" ,"./bar" ,"./bar" ]
- // force relative when the src is relative.
- , ["./d" ,"./a/b/c/d/e" ,"../../../../d" ,"../../../../d" ]
- // if src is abs and dest is relative, then fail
- , ["./d" ,"/a/b" ,false ,false ]
- ].forEach(function (test) {
- var d = test[0]
- , s = test[1]
- , r = test[2]
- , rr = test[3]
- , ra = relativize(d, s)
- , rra = relativize(d, s, true)
- console.log([d, s, r, rr], [ra, rra], [r === ra, rr === rra])
- assert.equal(r, ra)
- assert.equal(rr, rra)
- if (!r) return
- // contract: this is the relative path from absolute A to absolute B
- var ad = path.resolve(d)
- , as = path.resolve(s)
- , dir = path.dirname(as)
- assert.equal(path.resolve(dir, rr), ad)
- assert.equal(path.resolve(dir, r), ad)
- })
-
- console.log("ok")
-}
View
@@ -8,7 +8,6 @@ var npm = require("../npm.js")
, uidNumber = require("uid-number")
, rm = require("rimraf")
, readJson = require("read-package-json")
- , relativize = require("./relativize.js")
, cache = require("../cache.js")
, myUid = process.getuid && process.getuid()
, myGid = process.getgid && process.getgid()

0 comments on commit 0892bab

Please sign in to comment.