fix(fs.hard-link-dir): ignore broken symlinks

This commit is contained in:
Zoltan Kochan
2023-01-30 17:35:35 +02:00
parent 5c4eb0fc3e
commit 6add01403d
2 changed files with 23 additions and 6 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/fs.hard-link-dir": patch
---
Ignore broken symlinks.

View File

@@ -29,16 +29,13 @@ export async function hardLinkDir (src: string, destDirs: string[]) {
destDirs.map(async (destDir) => {
const destFile = path.join(destDir, file)
try {
await linkOrCopy(srcFile, destFile)
await linkOrCopyFile(srcFile, destFile)
} catch (err: any) { // eslint-disable-line
if (err.code === 'ENOENT') {
await fs.mkdir(destDir, { recursive: true })
await linkOrCopy(srcFile, destFile)
// Ignore broken symlinks
return
}
if (err.code !== 'EEXIST') {
throw err
}
throw err
}
})
)
@@ -46,6 +43,21 @@ export async function hardLinkDir (src: string, destDirs: string[]) {
)
}
async function linkOrCopyFile (srcFile: string, destFile: string) {
try {
await linkOrCopy(srcFile, destFile)
} catch (err: any) { // eslint-disable-line
if (err.code === 'ENOENT') {
await fs.mkdir(path.dirname(destFile), { recursive: true })
await linkOrCopy(srcFile, destFile)
return
}
if (err.code !== 'EEXIST') {
throw err
}
}
}
/*
* This function could be optimized because we don't really need to try linking again
* if linking failed once.