From b1d4c320751c1e02bef3c118b2df19a56f5d1696 Mon Sep 17 00:00:00 2001 From: typicode Date: Thu, 9 Apr 2020 18:28:30 +0200 Subject: [PATCH] Don't throw error during uninstall if file doesn't exist (#708) --- src/installer/__tests__/index.ts | 9 +++++++++ src/installer/localScript.ts | 5 ++++- src/installer/mainScript.ts | 5 ++++- 3 files changed, 17 insertions(+), 2 deletions(-) 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) + } }