source: frontend/node_modules/fs-extra/lib/ensure/symlink.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.5 KB
Line 
1'use strict'
2
3const u = require('universalify').fromCallback
4const path = require('path')
5const fs = require('../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
20const { areIdentical } = require('../util/stat')
21
22function createSymlink (srcpath, dstpath, type, callback) {
23 callback = (typeof type === 'function') ? type : callback
24 type = (typeof type === 'function') ? false : type
25
26 fs.lstat(dstpath, (err, stats) => {
27 if (!err && stats.isSymbolicLink()) {
28 Promise.all([
29 fs.stat(srcpath),
30 fs.stat(dstpath)
31 ]).then(([srcStat, dstStat]) => {
32 if (areIdentical(srcStat, dstStat)) return callback(null)
33 _createSymlink(srcpath, dstpath, type, callback)
34 })
35 } else _createSymlink(srcpath, dstpath, type, callback)
36 })
37}
38
39function _createSymlink (srcpath, dstpath, type, callback) {
40 symlinkPaths(srcpath, dstpath, (err, relative) => {
41 if (err) return callback(err)
42 srcpath = relative.toDst
43 symlinkType(relative.toCwd, type, (err, type) => {
44 if (err) return callback(err)
45 const dir = path.dirname(dstpath)
46 pathExists(dir, (err, dirExists) => {
47 if (err) return callback(err)
48 if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
49 mkdirs(dir, err => {
50 if (err) return callback(err)
51 fs.symlink(srcpath, dstpath, type, callback)
52 })
53 })
54 })
55 })
56}
57
58function createSymlinkSync (srcpath, dstpath, type) {
59 let stats
60 try {
61 stats = fs.lstatSync(dstpath)
62 } catch {}
63 if (stats && stats.isSymbolicLink()) {
64 const srcStat = fs.statSync(srcpath)
65 const dstStat = fs.statSync(dstpath)
66 if (areIdentical(srcStat, dstStat)) return
67 }
68
69 const relative = symlinkPathsSync(srcpath, dstpath)
70 srcpath = relative.toDst
71 type = symlinkTypeSync(relative.toCwd, type)
72 const dir = path.dirname(dstpath)
73 const exists = fs.existsSync(dir)
74 if (exists) return fs.symlinkSync(srcpath, dstpath, type)
75 mkdirsSync(dir)
76 return fs.symlinkSync(srcpath, dstpath, type)
77}
78
79module.exports = {
80 createSymlink: u(createSymlink),
81 createSymlinkSync
82}
Note: See TracBrowser for help on using the repository browser.