Skip to content

Commit

Permalink
fix: don't throw an error if correct symlink already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Aug 24, 2024
1 parent a87323e commit 44d9bb5
Show file tree
Hide file tree
Showing 3 changed files with 54 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)
}
19 changes: 19 additions & 0 deletions test/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down
19 changes: 19 additions & 0 deletions test/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down

0 comments on commit 44d9bb5

Please sign in to comment.