From 9e690c00a814c60f8986fb0e5b75c79199df1f9d Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 22 Oct 2023 03:23:33 +0800 Subject: [PATCH] Fix stat when path doesn't exist --- lib/ensure/file.js | 7 +++++-- lib/ensure/symlink-paths.js | 41 +++++++++++++++++++------------------ lib/ensure/symlink-type.js | 9 ++++---- lib/ensure/symlink.js | 25 ++++++++++------------ 4 files changed, 42 insertions(+), 40 deletions(-) diff --git a/lib/ensure/file.js b/lib/ensure/file.js index 0b2ade95..c94fa370 100644 --- a/lib/ensure/file.js +++ b/lib/ensure/file.js @@ -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) diff --git a/lib/ensure/symlink-paths.js b/lib/ensure/symlink-paths.js index a615525c..85dda475 100644 --- a/lib/ensure/symlink-paths.js +++ b/lib/ensure/symlink-paths.js @@ -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 @@ -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 } diff --git a/lib/ensure/symlink-type.js b/lib/ensure/symlink-type.js index 3f76f3f2..f741f480 100644 --- a/lib/ensure/symlink-type.js +++ b/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' } @@ -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 { @@ -28,6 +29,6 @@ function symlinkTypeSync (srcpath, type) { } module.exports = { - symlinkType, + symlinkType: u(symlinkType), symlinkTypeSync } diff --git a/lib/ensure/symlink.js b/lib/ensure/symlink.js index 25a2b5ae..18a9f8b2 100644 --- a/lib/ensure/symlink.js +++ b/lib/ensure/symlink.js @@ -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) @@ -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)