Skip to content

Commit d9d7823

Browse files
chouchoujiJounQin
andauthored
feat: only remove some hooks that are not in preserveUnused option (#121)
Co-authored-by: JounQin <[email protected]>
1 parent 8486a22 commit d9d7823

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

.changeset/giant-shirts-travel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"simple-git-hooks": minor
3+
---
4+
5+
feat: only remove some hooks that are not in `preserveUnused` option

simple-git-hooks.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const fs = require('fs')
22
const path = require('path')
33
const url = require('url')
44

5+
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'
6+
57
const VALID_GIT_HOOKS = [
68
'applypatch-msg',
79
'pre-applypatch',
@@ -168,7 +170,7 @@ async function setHooksFromConfig(projectRootPath=process.cwd(), argv=process.ar
168170
const config = await _getConfig(projectRootPath, customConfigPath)
169171

170172
if (!config) {
171-
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')
173+
throw(CONFIG_ERROR)
172174
}
173175

174176
const preserveUnused = Array.isArray(config.preserveUnused) ? config.preserveUnused : config.preserveUnused ? VALID_GIT_HOOKS: []
@@ -216,9 +218,19 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
216218
* Deletes all git hooks
217219
* @param {string} projectRoot
218220
*/
219-
function removeHooks(projectRoot=process.cwd()) {
220-
for (let configEntry of VALID_GIT_HOOKS) {
221-
_removeHook(configEntry, projectRoot)
221+
async function removeHooks(projectRoot = process.cwd()) {
222+
const customConfigPath = _getCustomConfigPath(process.argv)
223+
const config = await _getConfig(projectRoot, customConfigPath)
224+
225+
if (!config) {
226+
throw (CONFIG_ERROR)
227+
}
228+
229+
const preserveUnused = Array.isArray(config.preserveUnused) ? config.preserveUnused : []
230+
for (const configEntry of VALID_GIT_HOOKS) {
231+
if(!preserveUnused.includes(configEntry)) {
232+
_removeHook(configEntry, projectRoot)
233+
}
222234
}
223235
}
224236

simple-git-hooks.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ describe("Simple Git Hooks tests", () => {
397397
);
398398
expect(isEqual(installedHooks, { "pre-commit": TEST_SCRIPT })).toBe(true);
399399

400-
simpleGitHooks.removeHooks(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
400+
await simpleGitHooks.removeHooks(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
401401

402402
installedHooks = getInstalledGitHooks(
403403
path.normalize(
@@ -466,6 +466,37 @@ describe("Simple Git Hooks tests", () => {
466466
})
467467
).toBe(true);
468468
});
469+
470+
it("creates git hooks and removes hooks which are not in preserveUnused", async () => {
471+
createGitHooksFolder(PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON);
472+
473+
const installedHooksDir = path.normalize(
474+
path.join(
475+
PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON,
476+
".git",
477+
"hooks"
478+
)
479+
);
480+
481+
fs.writeFileSync(
482+
path.resolve(installedHooksDir, "commit-msg"),
483+
"# do nothing"
484+
);
485+
486+
let installedHooks = getInstalledGitHooks(installedHooksDir);
487+
488+
expect(isEqual(installedHooks, { "commit-msg": "# do nothing" })).toBe(true);
489+
490+
await simpleGitHooks.setHooksFromConfig(PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON);
491+
492+
installedHooks = getInstalledGitHooks(installedHooksDir);
493+
expect(isEqual(installedHooks, { "pre-commit": TEST_SCRIPT, "commit-msg": "# do nothing" })).toBe(true);
494+
495+
await simpleGitHooks.removeHooks(PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON);
496+
497+
installedHooks = getInstalledGitHooks(installedHooksDir);
498+
expect(isEqual(installedHooks, { "commit-msg": "# do nothing" })).toBe(true);
499+
});
469500
});
470501

471502
describe("CLI tests", () => {

uninstall.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const {removeHooks} = require("./simple-git-hooks");
66
/**
77
* Removes the pre-commit from command in config by default
88
*/
9-
function uninstall() {
9+
async function uninstall() {
1010
console.log("[INFO] Removing git hooks from .git/hooks")
1111

1212
try {
13-
removeHooks()
13+
await removeHooks()
1414
console.log("[INFO] Successfully removed all git hooks")
1515
} catch (e) {
1616
console.log("[INFO] Couldn't remove git hooks. Reason: " + e)

0 commit comments

Comments
 (0)