Permalink
Browse files

Don't use the 'gently' param to rimraf

  • Loading branch information...
1 parent 6975ca6 commit 6c6458def7beccb41b02c0c8a618f140512be4a5 @isaacs isaacs committed Jan 30, 2012
Showing with 52 additions and 6 deletions.
  1. +5 −4 lib/unbuild.js
  2. +45 −0 lib/utils/gently-rm.js
  3. +2 −2 lib/utils/link.js
View
@@ -3,6 +3,7 @@ unbuild.usage = "npm unbuild <folder>\n(this is plumbing)"
var readJson = require("./utils/read-json.js")
, rm = require("rimraf")
+ , gentlyRm = require("./utils/gently-rm.js")
, npm = require("./npm.js")
, path = require("path")
, fs = require("graceful-fs")
@@ -57,7 +58,7 @@ function rmBins (pkg, folder, parent, top, cb) {
rm(path.resolve(binRoot, b) + ".cmd", cb)
} else {
rm( path.resolve(binRoot, b)
- , { gently: !npm.config.get("force") && folder }
+ , !npm.config.get("force") && folder
, cb )
}
}, cb)
@@ -78,8 +79,8 @@ function rmMans (pkg, folder, parent, top, cb) {
: pkg.name + "-" + bn)
+ "." + sxn + gz
)
- rm( manDest
- , { gently: !npm.config.get("force") && folder }
- , cb )
+ gentlyRm( manDest
+ , !npm.config.get("force") && folder
+ , cb )
}, cb)
}
View
@@ -0,0 +1,45 @@
+// only remove the thing if it's a symlink into a specific folder.
+// This is a very common use-case of npm's, but not so common elsewhere.
+
+module.exports = gentlyRm
+
+var rimraf = require("rimraf")
+ , fs = require("graceful-fs")
+ , npm = require("../npm.js")
+ , path = require("path")
+
+function gentlyRm (target, gently, cb) {
+ if (npm.config.get("force") || !gently) {
+ return rimraf(target, cb)
+ }
+
+ gently = path.resolve(gently)
+
+ // lstat it, see if it's a symlink.
+ fs.lstat(target, function (er, st) {
+ if (er) return rimraf(target, cb)
+ if (!s.isSymbolicLink()) next(null, path.resolve(p))
+ realish(p, next)
+ })
+
+ function next (er, rp) {
+ if (rp && rp.indexOf(gently) !== 0) {
+ return clobberFail(target, gently, cb)
+ }
+ rimraf(target, cb)
+ }
+}
+
+function realish (p, cb) {
+ fs.readlink(p, function (er, r) {
+ if (er) return cb(er)
+ return cb(null, path.resolve(path.dirname(p), r))
+ })
+}
+
+function clobberFail (p, g, cb) {
+ var er = new Error("Refusing to delete: "+p+" not in "+g)
+ er.code = "EEXIST"
+ er.path = p
+ return cb(er)
+}
View
@@ -5,7 +5,7 @@ link.ifExists = linkIfExists
var fs = require("graceful-fs")
, chain = require("slide").chain
, mkdir = require("./mkdir-p.js")
- , rm = require("rimraf")
+ , rm = require("./gently-rm.js")
, log = require("./log.js")
, path = require("path")
, relativize = require("./relativize.js")
@@ -23,7 +23,7 @@ function link (from, to, gently, cb) {
if (npm.config.get("force")) gently = false
chain
( [ [fs, "stat", from]
- , [rm, to, { gently: gently }]
+ , [rm, to, gently]
, [mkdir, path.dirname(to)]
, [fs, "symlink", relativize(from, to), to] ]
, cb)

0 comments on commit 6c6458d

Please sign in to comment.