From 44d9bb5fa1f9e3d3b985d69ced2400020992fb93 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sat, 24 Aug 2024 02:35:41 +0200 Subject: [PATCH] fix: don't throw an error if correct symlink already exists --- src/index.ts | 22 ++++++++++++++++------ test/async.ts | 19 +++++++++++++++++++ test/sync.ts | 19 +++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 728f892..4a5dc93 100644 --- a/src/index.ts +++ b/src/index.ts @@ -42,6 +42,7 @@ async function forceSymlink ( renameTried?: boolean } ): Promise<{ reused: boolean, warn?: string }> { + let initialErr: Error try { await fs.symlink(target, path, symlinkType) return { reused: false } @@ -60,9 +61,7 @@ async function forceSymlink ( return { reused: false } case 'EEXIST': case 'EISDIR': - if (opts?.overwrite === false) { - throw err - } + initialErr = err // If the target file already exists then we proceed. // Additional checks are done below. break @@ -75,6 +74,9 @@ async function forceSymlink ( try { linkString = await fs.readlink(path) } catch (err) { + if (opts?.overwrite === false) { + throw initialErr + } // path is not a link const parentDir = pathLib.dirname(path) let warn!: string @@ -98,6 +100,9 @@ async function forceSymlink ( if (target === linkString) { return { reused: true } } + if (opts?.overwrite === false) { + throw initialErr + } await fs.unlink(path) return await forceSymlink(target, path, opts) } @@ -128,10 +133,12 @@ function forceSymlinkSync ( renameTried?: boolean } ): { reused: boolean, warn?: string } { + let initialErr: Error try { symlinkSync(target, path, symlinkType) return { reused: false } } catch (err) { + initialErr = err switch ((err).code) { case 'ENOENT': try { @@ -146,9 +153,6 @@ function forceSymlinkSync ( return { reused: false } case 'EEXIST': case 'EISDIR': - if (opts?.overwrite === false) { - throw err - } // If the target file already exists then we proceed. // Additional checks are done below. break @@ -161,6 +165,9 @@ function forceSymlinkSync ( try { linkString = readlinkSync(path) } catch (err) { + if (opts?.overwrite === false) { + throw initialErr + } // path is not a link const parentDir = pathLib.dirname(path) let warn!: string @@ -184,6 +191,9 @@ function forceSymlinkSync ( if (target === linkString) { return { reused: true } } + if (opts?.overwrite === false) { + throw initialErr + } unlinkSync(path) return forceSymlinkSync(target, path, opts) } diff --git a/test/async.ts b/test/async.ts index 764a05b..8e288c7 100644 --- a/test/async.ts +++ b/test/async.ts @@ -40,6 +40,25 @@ test('do not rename target folder if overwrite is set to false', async (t) => { t.end() }) +test('do not fail if correct target folder already exists', async (t) => { + const temp = tempy.directory() + t.comment(`testing in ${temp}`) + process.chdir(temp) + + await fs.mkdir('src') + await symlink('src', 'dest', { overwrite: false }) + + let err!: Error + try { + await symlink('src', 'dest', { overwrite: false }) + } catch (_err) { + err = _err + } + + t.equals(err, undefined) + t.end() +}) + test('rename target file if it exists', async (t) => { const temp = tempy.directory() t.comment(`testing in ${temp}`) diff --git a/test/sync.ts b/test/sync.ts index ac5cf98..050c746 100644 --- a/test/sync.ts +++ b/test/sync.ts @@ -40,6 +40,25 @@ test('do not rename target folder if overwrite is set to false', async (t) => { t.end() }) +test('do not fail if correct target folder already exists', async (t) => { + const temp = tempy.directory() + t.comment(`testing in ${temp}`) + process.chdir(temp) + + await fs.mkdir('src') + symlink.sync('src', 'dest', { overwrite: false }) + + let err!: Error + try { + symlink.sync('src', 'dest', { overwrite: false }) + } catch (_err) { + err = _err + } + + t.equals(err, undefined) + t.end() +}) + test('rename target file if it exists', async (t) => { const temp = tempy.directory() t.comment(`testing in ${temp}`)