diff --git a/src/installer/__tests__/index.ts b/src/installer/__tests__/index.ts index 1083edb33..ec0b532fa 100644 --- a/src/installer/__tests__/index.ts +++ b/src/installer/__tests__/index.ts @@ -236,5 +236,14 @@ describe('install', (): void => { expectHookToExist('.git/hooks/pre-commit') }) + it('should not throw error during uninstall if some files do not exist', (): void => { + writeFile('package.json', pkg) + + install() + fs.unlinkSync(path.join(tempDir, '.git/hooks/pre-commit')) + fs.unlinkSync(path.join(tempDir, '.git/hooks/husky.sh')) + + expect(uninstall).not.toThrow() + }) hooksManagers.forEach(testMigration) }) diff --git a/src/installer/localScript.ts b/src/installer/localScript.ts index 941d56d1b..ee3521821 100644 --- a/src/installer/localScript.ts +++ b/src/installer/localScript.ts @@ -26,5 +26,8 @@ export function createLocalScript( } export function removeLocalScript(gitHooksDir: string): void { - fs.unlinkSync(path.join(gitHooksDir, 'husky.local.sh')) + const filename = path.join(gitHooksDir, 'husky.local.sh') + if (fs.existsSync(filename)) { + fs.unlinkSync(filename) + } } diff --git a/src/installer/mainScript.ts b/src/installer/mainScript.ts index bf51f2005..2588073b9 100644 --- a/src/installer/mainScript.ts +++ b/src/installer/mainScript.ts @@ -18,5 +18,8 @@ export function createMainScript(gitHooksDir: string): void { } export function removeMainScript(gitHooksDir: string): void { - fs.unlinkSync(path.join(gitHooksDir, 'husky.sh')) + const filename = path.join(gitHooksDir, 'husky.sh') + if (fs.existsSync(filename)) { + fs.unlinkSync(filename) + } }