Skip to content

Commit

Permalink
Fix stat when path doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Oct 21, 2023
1 parent 97d472a commit 9e690c0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
7 changes: 5 additions & 2 deletions lib/ensure/file.js
Expand Up @@ -6,8 +6,11 @@ const fs = require('../fs')
const mkdir = require('../mkdirs')

async function createFile (file) {
const fileStats = await fs.stat(file)
if (fileStats.isFile()) return
let stats
try {
stats = await fs.stat(file)
} catch { }
if (stats && stats.isFile()) return

const dir = path.dirname(file)

Expand Down
41 changes: 21 additions & 20 deletions lib/ensure/symlink-paths.js
Expand Up @@ -2,7 +2,9 @@

const path = require('path')
const fs = require('../fs')
const pathExists = require('../path-exists').pathExists
const { pathExists } = require('../path-exists')

const u = require('universalify').fromPromise

/**
* Function that returns two types of paths, one relative to symlink, and one
Expand Down Expand Up @@ -66,35 +68,34 @@ async function symlinkPaths (srcpath, dstpath) {
}

function symlinkPathsSync (srcpath, dstpath) {
let exists
if (path.isAbsolute(srcpath)) {
exists = fs.existsSync(srcpath)
const 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)
}
}

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,
symlinkPaths: u(symlinkPaths),
symlinkPathsSync
}
9 changes: 5 additions & 4 deletions lib/ensure/symlink-type.js
@@ -1,13 +1,14 @@
'use strict'

const fs = require('../fs')
const u = require('universalify').fromPromise

async function symlinkType (srcpath, type = false) {
if (type) return type

let stats
try {
await fs.lstat(srcpath)
stats = await fs.lstat(srcpath)
} catch {
return 'file'
}
Expand All @@ -16,9 +17,9 @@ async function symlinkType (srcpath, type = false) {
}

function symlinkTypeSync (srcpath, type) {
let stats

if (type) return type

let stats
try {
stats = fs.lstatSync(srcpath)
} catch {
Expand All @@ -28,6 +29,6 @@ function symlinkTypeSync (srcpath, type) {
}

module.exports = {
symlinkType,
symlinkType: u(symlinkType),
symlinkTypeSync
}
25 changes: 11 additions & 14 deletions lib/ensure/symlink.js
Expand Up @@ -14,23 +14,20 @@ const { pathExists } = require('../path-exists')
const { areIdentical } = require('../util/stat')

async function createSymlink (srcpath, dstpath, type = false) {
let stats
try {
const stats = await fs.lstat(dstpath)

if (stats.isSymbolicLink()) {
const [srcStat, dstStat] = await Promise.all([
fs.stat(srcpath),
fs.stat(dstpath)
])
stats = await fs.lstat(dstpath)
} catch { }

if (areIdentical(srcStat, dstStat)) return
}
if (stats && stats.isSymbolicLink()) {
const [srcStat, dstStat] = await Promise.all([
fs.stat(srcpath),
fs.stat(dstpath)
])

await _createSymlink(srcpath, dstpath, type)
} catch {}
}
if (areIdentical(srcStat, dstStat)) return
}

async function _createSymlink (srcpath, dstpath, type) {
const relative = await symlinkPaths(srcpath, dstpath)
srcpath = relative.toDst
const toType = await symlinkType(relative.toCwd, type)
Expand All @@ -47,7 +44,7 @@ function createSymlinkSync (srcpath, dstpath, type) {
let stats
try {
stats = fs.lstatSync(dstpath)
} catch {}
} catch { }
if (stats && stats.isSymbolicLink()) {
const srcStat = fs.statSync(srcpath)
const dstStat = fs.statSync(dstpath)
Expand Down

0 comments on commit 9e690c0

Please sign in to comment.