Skip to content

Commit

Permalink
Refactor createSymlink
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Oct 21, 2023
1 parent 4f2b36e commit 97d472a
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions lib/ensure/symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,21 @@ async function createSymlink (srcpath, dstpath, type = false) {
if (areIdentical(srcStat, dstStat)) return
}

return _createSymlink(srcpath, dstpath, type)
await _createSymlink(srcpath, dstpath, type)
} catch {}
}

function _createSymlink (srcpath, dstpath, type, callback) {
symlinkPaths(srcpath, dstpath, (err, relative) => {
if (err) return callback(err)
srcpath = relative.toDst
symlinkType(relative.toCwd, type, (err, type) => {
if (err) return callback(err)
const dir = path.dirname(dstpath)
pathExists(dir, (err, dirExists) => {
if (err) return callback(err)
if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
mkdirs(dir, err => {
if (err) return callback(err)
fs.symlink(srcpath, dstpath, type, callback)
})
})
})
})
async function _createSymlink (srcpath, dstpath, type) {
const relative = await symlinkPaths(srcpath, dstpath)
srcpath = relative.toDst
const toType = await symlinkType(relative.toCwd, type)
const dir = path.dirname(dstpath)

const dirExists = await pathExists(dir)
if (dirExists) return fs.symlink(srcpath, dstpath, toType)

await mkdirs(dir)
return fs.symlink(srcpath, dstpath, toType)
}

function createSymlinkSync (srcpath, dstpath, type) {
Expand Down

0 comments on commit 97d472a

Please sign in to comment.