Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't throw an error if correct symlink already exists #54

Merged
merged 2 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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