BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / fs-extra / lib / ensure / symlink.js

symlink.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
symlink.js 2.0 KiB Download Raw
1var path = require('path')
2var fs = require('graceful-fs')
3var _mkdirs = require('../mkdirs')
4var mkdirs = _mkdirs.mkdirs
5var mkdirsSync = _mkdirs.mkdirsSync
6
7var _symlinkPaths = require('./symlink-paths')
8var symlinkPaths = _symlinkPaths.symlinkPaths
9var symlinkPathsSync = _symlinkPaths.symlinkPathsSync
10
11var _symlinkType = require('./symlink-type')
12var symlinkType = _symlinkType.symlinkType
13var symlinkTypeSync = _symlinkType.symlinkTypeSync
14
15function 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
39function 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
56module.exports = {
57 createSymlink: createSymlink,
58 createSymlinkSync: createSymlinkSync,
59 // alias
60 ensureSymlink: createSymlink,
61 ensureSymlinkSync: createSymlinkSync
62}