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..454fd69 100644 --- a/test/async.ts +++ b/test/async.ts @@ -40,6 +40,18 @@ 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 }) + + t.equal((await symlink('src', 'dest', { overwrite: false })).reused, true) + 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..79c930d 100644 --- a/test/sync.ts +++ b/test/sync.ts @@ -40,6 +40,18 @@ 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 }) + + t.equals(symlink.sync('src', 'dest', { overwrite: false }).reused, true) + t.end() +}) + test('rename target file if it exists', async (t) => { const temp = tempy.directory() t.comment(`testing in ${temp}`)