3 link.ifExists = linkIfExists
5 var fs = require("graceful-fs")
6 , chain = require("slide").chain
7 , mkdir = require("mkdirp")
8 , rm = require("./gently-rm.js")
9 , path = require("path")
10 , npm = require("../npm.js")
12 function linkIfExists (from, to, gently, cb) {
13 fs.stat(from, function (er) {
15 link(from, to, gently, cb)
19 function link (from, to, gently, abs, cb) {
20 if (typeof cb !== "function") cb = abs, abs = false
21 if (typeof cb !== "function") cb = gently, gently = null
22 if (npm.config.get("force")) gently = false
25 var target = from = path.resolve(from)
26 if (!abs && process.platform !== "win32") {
27 // junctions on windows must be absolute
28 target = path.relative(path.dirname(to), from)
29 // if there is no folder in common, then it will be much
30 // longer, and using a relative link is dumb.
31 if (target.length >= from.length) target = from
35 ( [ [fs, "stat", from]
37 , [mkdir, path.dirname(to)]
38 , [fs, "symlink", target, to, "junction"] ]