summaryrefslogtreecommitdiff
path: root/deps/npm/lib/utils/link.js
blob: 9be1221f0ae6261a320daf87442018a956a9a9b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

module.exports = link
link.ifExists = linkIfExists

var fs = require("graceful-fs")
  , chain = require("slide").chain
  , mkdir = require("./mkdir-p.js")
  , rm = require("rimraf")
  , log = require("./log.js")
  , path = require("path")
  , relativize = require("./relativize.js")
  , npm = require("../npm.js")

function linkIfExists (from, to, gently, cb) {
  fs.stat(from, function (er) {
    if (er) return cb()
    link(from, to, gently, cb)
  })
}

function link (from, to, gently, cb) {
  if (typeof cb !== "function") cb = gently, gently = null
  if (npm.config.get("force")) gently = false
  chain
    ( [ [fs, "stat", from]
      , [rm, to, { gently: gently }]
      , [mkdir, path.dirname(to)]
      , [fs, "symlink", relativize(from, to), to] ]
    , cb)
}