resolve-link-target
Version:
Resolve a symlink's (or junction's) target
34 lines (28 loc) • 751 B
JavaScript
const fs = require('graceful-fs')
const path = require('path')
module.exports = getLinkTarget
module.exports.sync = getLinkTargetSync
function getLinkTarget (linkPath) {
linkPath = path.resolve(linkPath)
return new Promise((resolve, reject) => {
fs.readlink(linkPath, (err, target) => {
if (err) {
reject(err)
return
}
resolve(_resolveLink(linkPath, target))
})
})
}
function getLinkTargetSync (linkPath) {
linkPath = path.resolve(linkPath)
const target = fs.readlinkSync(linkPath)
return _resolveLink(linkPath, target)
}
function _resolveLink (dest, target) {
if (path.isAbsolute(target)) {
return path.resolve(target)
}
return path.join(path.dirname(dest), target)
}