123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 'use strict'
-
- const path = require('path')
- const fs = require('graceful-fs')
- const pathExists = require('../path-exists').pathExists
-
-
-
- function symlinkPaths (srcpath, dstpath, callback) {
- if (path.isAbsolute(srcpath)) {
- return fs.lstat(srcpath, (err) => {
- if (err) {
- err.message = err.message.replace('lstat', 'ensureSymlink')
- return callback(err)
- }
- return callback(null, {
- 'toCwd': srcpath,
- 'toDst': srcpath
- })
- })
- } else {
- const dstdir = path.dirname(dstpath)
- const relativeToDst = path.join(dstdir, srcpath)
- return pathExists(relativeToDst, (err, exists) => {
- if (err) return callback(err)
- if (exists) {
- return callback(null, {
- 'toCwd': relativeToDst,
- 'toDst': srcpath
- })
- } else {
- return fs.lstat(srcpath, (err) => {
- if (err) {
- err.message = err.message.replace('lstat', 'ensureSymlink')
- return callback(err)
- }
- return callback(null, {
- 'toCwd': srcpath,
- 'toDst': path.relative(dstdir, srcpath)
- })
- })
- }
- })
- }
- }
-
- function symlinkPathsSync (srcpath, dstpath) {
- let exists
- if (path.isAbsolute(srcpath)) {
- exists = fs.existsSync(srcpath)
- if (!exists) throw new Error('absolute srcpath does not exist')
- return {
- 'toCwd': srcpath,
- 'toDst': srcpath
- }
- } else {
- const dstdir = path.dirname(dstpath)
- const relativeToDst = path.join(dstdir, srcpath)
- exists = fs.existsSync(relativeToDst)
- if (exists) {
- return {
- 'toCwd': relativeToDst,
- 'toDst': srcpath
- }
- } else {
- exists = fs.existsSync(srcpath)
- if (!exists) throw new Error('relative srcpath does not exist')
- return {
- 'toCwd': srcpath,
- 'toDst': path.relative(dstdir, srcpath)
- }
- }
- }
- }
-
- module.exports = {
- symlinkPaths,
- symlinkPathsSync
- }
|