diff --git a/.changeset/giant-shirts-travel.md b/.changeset/giant-shirts-travel.md new file mode 100644 index 0000000..bfd5c15 --- /dev/null +++ b/.changeset/giant-shirts-travel.md @@ -0,0 +1,5 @@ +--- +"simple-git-hooks": minor +--- + +feat: only remove some hooks that are not in `preserveUnused` option diff --git a/simple-git-hooks.js b/simple-git-hooks.js index bf81507..5ba3ddd 100644 --- a/simple-git-hooks.js +++ b/simple-git-hooks.js @@ -2,6 +2,8 @@ const fs = require('fs') const path = require('path') const url = require('url') +const CONFIG_ERROR = '[ERROR] Config was not found! Please add `.simple-git-hooks.cjs` or `.simple-git-hooks.js` or `.simple-git-hooks.mjs` or `simple-git-hooks.cjs` or `simple-git-hooks.js` or `simple-git-hooks.mjs` or `.simple-git-hooks.json` or `simple-git-hooks.json` or `simple-git-hooks` entry in package.json.\r\nCheck README for details' + const VALID_GIT_HOOKS = [ 'applypatch-msg', 'pre-applypatch', @@ -168,7 +170,7 @@ async function setHooksFromConfig(projectRootPath=process.cwd(), argv=process.ar const config = await _getConfig(projectRootPath, customConfigPath) if (!config) { - throw('[ERROR] Config was not found! Please add `.simple-git-hooks.cjs` or `.simple-git-hooks.js` or `.simple-git-hooks.mjs` or `simple-git-hooks.cjs` or `simple-git-hooks.js` or `simple-git-hooks.mjs` or `.simple-git-hooks.json` or `simple-git-hooks.json` or `simple-git-hooks` entry in package.json.\r\nCheck README for details') + throw(CONFIG_ERROR) } const preserveUnused = Array.isArray(config.preserveUnused) ? config.preserveUnused : config.preserveUnused ? VALID_GIT_HOOKS: [] @@ -216,9 +218,19 @@ function _setHook(hook, command, projectRoot=process.cwd()) { * Deletes all git hooks * @param {string} projectRoot */ -function removeHooks(projectRoot=process.cwd()) { - for (let configEntry of VALID_GIT_HOOKS) { - _removeHook(configEntry, projectRoot) +async function removeHooks(projectRoot = process.cwd()) { + const customConfigPath = _getCustomConfigPath(process.argv) + const config = await _getConfig(projectRoot, customConfigPath) + + if (!config) { + throw (CONFIG_ERROR) + } + + const preserveUnused = Array.isArray(config.preserveUnused) ? config.preserveUnused : [] + for (const configEntry of VALID_GIT_HOOKS) { + if(!preserveUnused.includes(configEntry)) { + _removeHook(configEntry, projectRoot) + } } } diff --git a/simple-git-hooks.test.js b/simple-git-hooks.test.js index 36d32b4..cede579 100644 --- a/simple-git-hooks.test.js +++ b/simple-git-hooks.test.js @@ -397,7 +397,7 @@ describe("Simple Git Hooks tests", () => { ); expect(isEqual(installedHooks, { "pre-commit": TEST_SCRIPT })).toBe(true); - simpleGitHooks.removeHooks(PROJECT_WITH_CONF_IN_PACKAGE_JSON); + await simpleGitHooks.removeHooks(PROJECT_WITH_CONF_IN_PACKAGE_JSON); installedHooks = getInstalledGitHooks( path.normalize( @@ -466,6 +466,37 @@ describe("Simple Git Hooks tests", () => { }) ).toBe(true); }); + + it("creates git hooks and removes hooks which are not in preserveUnused", async () => { + createGitHooksFolder(PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON); + + const installedHooksDir = path.normalize( + path.join( + PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON, + ".git", + "hooks" + ) + ); + + fs.writeFileSync( + path.resolve(installedHooksDir, "commit-msg"), + "# do nothing" + ); + + let installedHooks = getInstalledGitHooks(installedHooksDir); + + expect(isEqual(installedHooks, { "commit-msg": "# do nothing" })).toBe(true); + + await simpleGitHooks.setHooksFromConfig(PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON); + + installedHooks = getInstalledGitHooks(installedHooksDir); + expect(isEqual(installedHooks, { "pre-commit": TEST_SCRIPT, "commit-msg": "# do nothing" })).toBe(true); + + await simpleGitHooks.removeHooks(PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON); + + installedHooks = getInstalledGitHooks(installedHooksDir); + expect(isEqual(installedHooks, { "commit-msg": "# do nothing" })).toBe(true); + }); }); describe("CLI tests", () => { diff --git a/uninstall.js b/uninstall.js index 43a1916..46175dd 100644 --- a/uninstall.js +++ b/uninstall.js @@ -6,11 +6,11 @@ const {removeHooks} = require("./simple-git-hooks"); /** * Removes the pre-commit from command in config by default */ -function uninstall() { +async function uninstall() { console.log("[INFO] Removing git hooks from .git/hooks") try { - removeHooks() + await removeHooks() console.log("[INFO] Successfully removed all git hooks") } catch (e) { console.log("[INFO] Couldn't remove git hooks. Reason: " + e)