123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 'use strict'
- const path = require('path')
- const fs = require('../fs')
- const { pathExists } = require('../path-exists')
- const u = require('universalify').fromPromise
- async function symlinkPaths (srcpath, dstpath) {
- if (path.isAbsolute(srcpath)) {
- try {
- await fs.lstat(srcpath)
- } catch (err) {
- err.message = err.message.replace('lstat', 'ensureSymlink')
- throw err
- }
- return {
- toCwd: srcpath,
- toDst: srcpath
- }
- }
- const dstdir = path.dirname(dstpath)
- const relativeToDst = path.join(dstdir, srcpath)
- const exists = await pathExists(relativeToDst)
- if (exists) {
- return {
- toCwd: relativeToDst,
- toDst: srcpath
- }
- }
- try {
- await fs.lstat(srcpath)
- } catch (err) {
- err.message = err.message.replace('lstat', 'ensureSymlink')
- throw err
- }
- return {
- toCwd: srcpath,
- toDst: path.relative(dstdir, srcpath)
- }
- }
- function symlinkPathsSync (srcpath, dstpath) {
- if (path.isAbsolute(srcpath)) {
- const exists = fs.existsSync(srcpath)
- if (!exists) throw new Error('absolute srcpath does not exist')
- return {
- toCwd: srcpath,
- toDst: srcpath
- }
- }
- const dstdir = path.dirname(dstpath)
- const relativeToDst = path.join(dstdir, srcpath)
- const exists = fs.existsSync(relativeToDst)
- if (exists) {
- return {
- toCwd: relativeToDst,
- toDst: srcpath
- }
- }
- const srcExists = fs.existsSync(srcpath)
- if (!srcExists) throw new Error('relative srcpath does not exist')
- return {
- toCwd: srcpath,
- toDst: path.relative(dstdir, srcpath)
- }
- }
- module.exports = {
- symlinkPaths: u(symlinkPaths),
- symlinkPathsSync
- }
|