Skip to content

Commit

Permalink
fix: don't throw an error if correct symlink already exists (#54)
Browse files Browse the repository at this point in the history
* fix: don't throw an error if correct symlink already exists

* refactor: test
  • Loading branch information
zkochan authored Aug 25, 2024
1 parent a87323e commit dfc3128
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 ((<NodeJS.ErrnoException>err).code) {
case 'ENOENT':
try {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}
12 changes: 12 additions & 0 deletions test/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down
12 changes: 12 additions & 0 deletions test/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down

0 comments on commit dfc3128

Please sign in to comment.