From 52c5048d3bad71a5d6d781c8901f566f3bad6e79 Mon Sep 17 00:00:00 2001 From: NihadBadalov <32594553+NihadBadalov@users.noreply.github.com> Date: Sat, 4 May 2024 20:53:40 +0200 Subject: [PATCH] feat: delete broken symlinks if need to stow --- src/commands/stow.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/commands/stow.js b/src/commands/stow.js index 362ed2d..1b38174 100644 --- a/src/commands/stow.js +++ b/src/commands/stow.js @@ -53,13 +53,19 @@ export default async function stow(cwd, force, recursive) { } } - if (fs.existsSync(stowPath)) { + let isBrokenSymlink = false; + try { + isBrokenSymlink = !!fs.lstatSync(stowPath).isSymbolicLink(); + } catch (e) { + isBrokenSymlink = false; + } + if (fs.existsSync(stowPath) || isBrokenSymlink) { if (fs.lstatSync(stowPath).isSymbolicLink()) { if (force) { fs.unlinkSync(stowPath); } else { - console.error(`Failed to stow ${dotfileFilePath}: file already exists at stow location`); - continue; + console.error(`Failed to stow ${dotfileFilePath}: file already exists at stow location`); + continue; } } else if (fs.lstatSync(stowPath).isDirectory()) { @@ -69,7 +75,7 @@ export default async function stow(cwd, force, recursive) { if (force) { fs.unlinkSync(stowPath); } else { - console.error(`Failed to stow ${dotfileFilePath}: file already exists at stow location`); + console.error(`Failed to stow ${dotfileFilePath}: file already exists at stow location`); } } }