Skip to content

Commit 2ad5514

Browse files
fix: postinstall should ignore adding hooks if SKIP_INSTALL_SIMPLE_GI… (#128)
Co-authored-by: JounQin <admin@1stg.me>
1 parent 11ceddf commit 2ad5514

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

.changeset/bright-poets-beam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"simple-git-hooks": patch
3+
---
4+
5+
fix: postinstall should ignore adding hooks if SKIP_INSTALL_SIMPLE_GIT_HOOKS is defined

_tests/project_with_configuration_in_package_json/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"pre-commit": "exit 1"
66
},
77
"devDependencies": {
8+
"simple-git-hooks": "1.0.0",
89
"simple-pre-commit": "1.0.0"
910
}
1011
}

cli.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
/**
55
* A CLI tool to change the git hooks to commands from config
66
*/
7-
const {setHooksFromConfig} = require('./simple-git-hooks')
7+
const {setHooksFromConfig, skipInstall} = require('./simple-git-hooks')
88

9-
const {SKIP_INSTALL_SIMPLE_GIT_HOOKS} = process.env
10-
11-
if (['1', 'true'].includes(SKIP_INSTALL_SIMPLE_GIT_HOOKS)) {
12-
console.log(`[INFO] SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to "${SKIP_INSTALL_SIMPLE_GIT_HOOKS}", skipping installing hook.`)
13-
return
9+
if(skipInstall()) {
10+
return;
1411
}
1512

1613
setHooksFromConfig(process.cwd(), process.argv)

postinstall.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#!/usr/bin/env node
22

3-
const {checkSimpleGitHooksInDependencies, getProjectRootDirectoryFromNodeModules, setHooksFromConfig} = require("./simple-git-hooks");
3+
const {checkSimpleGitHooksInDependencies, getProjectRootDirectoryFromNodeModules, setHooksFromConfig, skipInstall} = require("./simple-git-hooks");
44

55

66
/**
77
* Creates the pre-commit from command in config by default
88
*/
99
async function postinstall() {
10+
11+
if(skipInstall()) {
12+
return;
13+
}
14+
1015
let projectDirectory;
1116

1217
/* When script is run after install, the process.cwd() would be like <project_folder>/node_modules/simple-git-hooks

simple-git-hooks.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,22 @@ function _validateHooks(config) {
423423

424424
return true
425425
}
426+
/** Checks if the environment variable for skipping hooks is defined. */
427+
function skipInstall() {
428+
const {SKIP_INSTALL_SIMPLE_GIT_HOOKS} = process.env
429+
if (['1', 'true'].includes(SKIP_INSTALL_SIMPLE_GIT_HOOKS)) {
430+
console.log(`[INFO] SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to "${SKIP_INSTALL_SIMPLE_GIT_HOOKS}", skipping installing hook.`)
431+
return true;
432+
}
433+
return false;
434+
}
426435

427436
module.exports = {
428437
checkSimpleGitHooksInDependencies,
429438
setHooksFromConfig,
430439
getProjectRootDirectoryFromNodeModules,
431440
getGitProjectRoot,
432441
removeHooks,
442+
skipInstall,
433443
PREPEND_SCRIPT
434444
}

simple-git-hooks.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,40 @@ describe("Simple Git Hooks tests", () => {
610610
);
611611
expect(installedHooks).toEqual({ "pre-commit": TEST_SCRIPT });
612612
});
613+
614+
it("does not create git hooks during postinstall hook when SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to 1", () => {
615+
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
616+
execSync(`node ${require.resolve("./postinstall")}`, {
617+
cwd: PROJECT_WITH_CONF_IN_PACKAGE_JSON,
618+
env: {
619+
...process.env,
620+
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "1",
621+
},
622+
});
623+
const installedHooks = getInstalledGitHooks(
624+
path.normalize(
625+
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
626+
)
627+
);
628+
expect(installedHooks).toEqual({});
629+
});
630+
631+
it("creates git hooks during postinstall when SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to 0", () => {
632+
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
633+
execSync(`node ${require.resolve("./postinstall")}`, {
634+
cwd: PROJECT_WITH_CONF_IN_PACKAGE_JSON,
635+
env: {
636+
...process.env,
637+
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "0",
638+
},
639+
});
640+
const installedHooks = getInstalledGitHooks(
641+
path.normalize(
642+
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
643+
)
644+
);
645+
expect(installedHooks).toEqual({ "pre-commit": TEST_SCRIPT });
646+
});
613647
});
614648
});
615649

0 commit comments

Comments
 (0)