source: trip-planner-front/node_modules/fs-extra/lib/ensure/symlink.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.0 KB
Line 
1'use strict'
2
3const u = require('universalify').fromCallback
4const path = require('path')
5const fs = require('graceful-fs')
6const _mkdirs = require('../mkdirs')
7const mkdirs = _mkdirs.mkdirs
8const mkdirsSync = _mkdirs.mkdirsSync
9
10const _symlinkPaths = require('./symlink-paths')
11const symlinkPaths = _symlinkPaths.symlinkPaths
12const symlinkPathsSync = _symlinkPaths.symlinkPathsSync
13
14const _symlinkType = require('./symlink-type')
15const symlinkType = _symlinkType.symlinkType
16const symlinkTypeSync = _symlinkType.symlinkTypeSync
17
18const pathExists = require('../path-exists').pathExists
19
20function createSymlink (srcpath, dstpath, type, callback) {
21 callback = (typeof type === 'function') ? type : callback
22 type = (typeof type === 'function') ? false : type
23
24 pathExists(dstpath, (err, destinationExists) => {
25 if (err) return callback(err)
26 if (destinationExists) return callback(null)
27 symlinkPaths(srcpath, dstpath, (err, relative) => {
28 if (err) return callback(err)
29 srcpath = relative.toDst
30 symlinkType(relative.toCwd, type, (err, type) => {
31 if (err) return callback(err)
32 const dir = path.dirname(dstpath)
33 pathExists(dir, (err, dirExists) => {
34 if (err) return callback(err)
35 if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
36 mkdirs(dir, err => {
37 if (err) return callback(err)
38 fs.symlink(srcpath, dstpath, type, callback)
39 })
40 })
41 })
42 })
43 })
44}
45
46function createSymlinkSync (srcpath, dstpath, type) {
47 const destinationExists = fs.existsSync(dstpath)
48 if (destinationExists) return undefined
49
50 const relative = symlinkPathsSync(srcpath, dstpath)
51 srcpath = relative.toDst
52 type = symlinkTypeSync(relative.toCwd, type)
53 const dir = path.dirname(dstpath)
54 const exists = fs.existsSync(dir)
55 if (exists) return fs.symlinkSync(srcpath, dstpath, type)
56 mkdirsSync(dir)
57 return fs.symlinkSync(srcpath, dstpath, type)
58}
59
60module.exports = {
61 createSymlink: u(createSymlink),
62 createSymlinkSync
63}
Note: See TracBrowser for help on using the repository browser.