Security update for permissions_by_term
[yaffs-website] / node_modules / fs-extra / lib / ensure / symlink.js
1 var path = require('path')
2 var fs = require('graceful-fs')
3 var _mkdirs = require('../mkdirs')
4 var mkdirs = _mkdirs.mkdirs
5 var mkdirsSync = _mkdirs.mkdirsSync
6
7 var _symlinkPaths = require('./symlink-paths')
8 var symlinkPaths = _symlinkPaths.symlinkPaths
9 var symlinkPathsSync = _symlinkPaths.symlinkPathsSync
10
11 var _symlinkType = require('./symlink-type')
12 var symlinkType = _symlinkType.symlinkType
13 var symlinkTypeSync = _symlinkType.symlinkTypeSync
14
15 function createSymlink (srcpath, dstpath, type, callback) {
16   callback = (typeof type === 'function') ? type : callback
17   type = (typeof type === 'function') ? false : type
18
19   fs.exists(dstpath, function (destinationExists) {
20     if (destinationExists) return callback(null)
21     symlinkPaths(srcpath, dstpath, function (err, relative) {
22       if (err) return callback(err)
23       srcpath = relative.toDst
24       symlinkType(relative.toCwd, type, function (err, type) {
25         if (err) return callback(err)
26         var dir = path.dirname(dstpath)
27         fs.exists(dir, function (dirExists) {
28           if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
29           mkdirs(dir, function (err) {
30             if (err) return callback(err)
31             fs.symlink(srcpath, dstpath, type, callback)
32           })
33         })
34       })
35     })
36   })
37 }
38
39 function createSymlinkSync (srcpath, dstpath, type, callback) {
40   callback = (typeof type === 'function') ? type : callback
41   type = (typeof type === 'function') ? false : type
42
43   var destinationExists = fs.existsSync(dstpath)
44   if (destinationExists) return undefined
45
46   var relative = symlinkPathsSync(srcpath, dstpath)
47   srcpath = relative.toDst
48   type = symlinkTypeSync(relative.toCwd, type)
49   var dir = path.dirname(dstpath)
50   var exists = fs.existsSync(dir)
51   if (exists) return fs.symlinkSync(srcpath, dstpath, type)
52   mkdirsSync(dir)
53   return fs.symlinkSync(srcpath, dstpath, type)
54 }
55
56 module.exports = {
57   createSymlink: createSymlink,
58   createSymlinkSync: createSymlinkSync,
59   // alias
60   ensureSymlink: createSymlink,
61   ensureSymlinkSync: createSymlinkSync
62 }