From af5d69f03e47ee73edcd4dcb946bc4cffd44359c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kh=E1=BA=A3i?= Date: Tue, 29 Oct 2024 15:24:41 +0700 Subject: [PATCH] fix: unlink ENOENT (#59) 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) }