| [9af201e] | 1 | 'use strict'
|
|---|
| 2 |
|
|---|
| 3 | const u = require('universalify').fromCallback
|
|---|
| 4 | const path = require('path')
|
|---|
| 5 | const fs = require('../fs')
|
|---|
| 6 | const _mkdirs = require('../mkdirs')
|
|---|
| 7 | const mkdirs = _mkdirs.mkdirs
|
|---|
| 8 | const mkdirsSync = _mkdirs.mkdirsSync
|
|---|
| 9 |
|
|---|
| 10 | const _symlinkPaths = require('./symlink-paths')
|
|---|
| 11 | const symlinkPaths = _symlinkPaths.symlinkPaths
|
|---|
| 12 | const symlinkPathsSync = _symlinkPaths.symlinkPathsSync
|
|---|
| 13 |
|
|---|
| 14 | const _symlinkType = require('./symlink-type')
|
|---|
| 15 | const symlinkType = _symlinkType.symlinkType
|
|---|
| 16 | const symlinkTypeSync = _symlinkType.symlinkTypeSync
|
|---|
| 17 |
|
|---|
| 18 | const pathExists = require('../path-exists').pathExists
|
|---|
| 19 |
|
|---|
| 20 | const { areIdentical } = require('../util/stat')
|
|---|
| 21 |
|
|---|
| 22 | function 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 |
|
|---|
| 39 | function _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 |
|
|---|
| 58 | function 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 |
|
|---|
| 79 | module.exports = {
|
|---|
| 80 | createSymlink: u(createSymlink),
|
|---|
| 81 | createSymlinkSync
|
|---|
| 82 | }
|
|---|