"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/utils/link.js" (8 Mar 2017, 2070 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 module.exports = link
    2 link.ifExists = linkIfExists
    3 
    4 var fs = require('graceful-fs')
    5 var chain = require('slide').chain
    6 var mkdir = require('mkdirp')
    7 var rm = require('./gently-rm.js')
    8 var path = require('path')
    9 var npm = require('../npm.js')
   10 
   11 function linkIfExists (from, to, gently, cb) {
   12   fs.stat(from, function (er) {
   13     if (er) return cb()
   14     fs.readlink(to, function (er, fromOnDisk) {
   15       // if the link already exists and matches what we would do,
   16       // we don't need to do anything
   17       if (!er) {
   18         var toDir = path.dirname(to)
   19         var absoluteFrom = path.resolve(toDir, from)
   20         var absoluteFromOnDisk = path.resolve(toDir, fromOnDisk)
   21         if (absoluteFrom === absoluteFromOnDisk) return cb()
   22       }
   23       link(from, to, gently, cb)
   24     })
   25   })
   26 }
   27 
   28 function resolveIfSymlink (maybeSymlinkPath, cb) {
   29   fs.lstat(maybeSymlinkPath, function (err, stat) {
   30     if (err) return cb.apply(this, arguments)
   31     if (!stat.isSymbolicLink()) return cb(null, maybeSymlinkPath)
   32     fs.readlink(maybeSymlinkPath, cb)
   33   })
   34 }
   35 
   36 function ensureFromIsNotSource (from, to, cb) {
   37   resolveIfSymlink(from, function (err, fromDestination) {
   38     if (err) return cb.apply(this, arguments)
   39     if (path.resolve(path.dirname(from), fromDestination) === path.resolve(to)) {
   40       return cb(new Error('Link target resolves to the same directory as link source: ' + to))
   41     }
   42     cb.apply(this, arguments)
   43   })
   44 }
   45 
   46 function link (from, to, gently, abs, cb) {
   47   if (typeof cb !== 'function') {
   48     cb = abs
   49     abs = false
   50   }
   51   if (typeof cb !== 'function') {
   52     cb = gently
   53     gently = null
   54   }
   55   if (npm.config.get('force')) gently = false
   56 
   57   to = path.resolve(to)
   58   var toDir = path.dirname(to)
   59   var absTarget = path.resolve(toDir, from)
   60   var relativeTarget = path.relative(toDir, absTarget)
   61   var target = abs ? absTarget : relativeTarget
   62 
   63   chain(
   64     [
   65       [ensureFromIsNotSource, absTarget, to],
   66       [fs, 'stat', absTarget],
   67       [rm, to, gently],
   68       [mkdir, path.dirname(to)],
   69       [fs, 'symlink', target, to, 'junction']
   70     ],
   71     cb
   72   )
   73 }