From 0d733d50e241c3b77ae0e63c01fe76f58bfe2537 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 29 Oct 2024 14:34:51 +0700 Subject: [PATCH] fix: unlink ENOENT Fixes https://github.com/pnpm/symlink-dir/issues/57 --- src/index.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4a5dc93..0bf7e3c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import betterPathResolve = require('better-path-resolve') import { promises as fs, symlinkSync, mkdirSync, readlinkSync, unlinkSync } from 'fs' +import util = require('util') import pathLib = require('path') import renameOverwrite = require('rename-overwrite') @@ -103,7 +104,13 @@ async function forceSymlink ( if (opts?.overwrite === false) { throw initialErr } - await fs.unlink(path) + try { + await fs.unlink(path) + } catch (error) { + if (!util.types.isNativeError(error) || !('code' in error) || error.code !== 'ENOENT') { + throw error + } + } return await forceSymlink(target, path, opts) } @@ -194,6 +201,12 @@ function forceSymlinkSync ( if (opts?.overwrite === false) { throw initialErr } - unlinkSync(path) + try { + unlinkSync(path) + } catch (error) { + if (!util.types.isNativeError(error) || !('code' in error) || error.code !== 'ENOENT') { + throw error + } + } return forceSymlinkSync(target, path, opts) }