Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-poets-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"simple-git-hooks": patch
---

fix: postinstall should ignore adding hooks if SKIP_INSTALL_SIMPLE_GIT_HOOKS is defined
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"pre-commit": "exit 1"
},
"devDependencies": {
"simple-git-hooks": "1.0.0",
"simple-pre-commit": "1.0.0"
}
}
9 changes: 3 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
/**
* A CLI tool to change the git hooks to commands from config
*/
const {setHooksFromConfig} = require('./simple-git-hooks')
const {setHooksFromConfig, skipInstall} = require('./simple-git-hooks')

const {SKIP_INSTALL_SIMPLE_GIT_HOOKS} = process.env

if (['1', 'true'].includes(SKIP_INSTALL_SIMPLE_GIT_HOOKS)) {
console.log(`[INFO] SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to "${SKIP_INSTALL_SIMPLE_GIT_HOOKS}", skipping installing hook.`)
return
if(skipInstall()) {
return;
}

setHooksFromConfig(process.cwd(), process.argv)
Expand Down
7 changes: 6 additions & 1 deletion postinstall.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#!/usr/bin/env node

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


/**
* Creates the pre-commit from command in config by default
*/
async function postinstall() {

if(skipInstall()) {
return;
}

let projectDirectory;

/* When script is run after install, the process.cwd() would be like <project_folder>/node_modules/simple-git-hooks
Expand Down
10 changes: 10 additions & 0 deletions simple-git-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,22 @@ function _validateHooks(config) {

return true
}
/** Checks if the environment variable for skipping hooks is defined. */
function skipInstall() {
const {SKIP_INSTALL_SIMPLE_GIT_HOOKS} = process.env
if (['1', 'true'].includes(SKIP_INSTALL_SIMPLE_GIT_HOOKS)) {
console.log(`[INFO] SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to "${SKIP_INSTALL_SIMPLE_GIT_HOOKS}", skipping installing hook.`)
return true;
}
return false;
}

module.exports = {
checkSimpleGitHooksInDependencies,
setHooksFromConfig,
getProjectRootDirectoryFromNodeModules,
getGitProjectRoot,
removeHooks,
skipInstall,
PREPEND_SCRIPT
}
34 changes: 34 additions & 0 deletions simple-git-hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,40 @@ describe("Simple Git Hooks tests", () => {
);
expect(installedHooks).toEqual({ "pre-commit": TEST_SCRIPT });
});

it("does not create git hooks during postinstall hook when SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to 1", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
execSync(`node ${require.resolve("./postinstall")}`, {
cwd: PROJECT_WITH_CONF_IN_PACKAGE_JSON,
env: {
...process.env,
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "1",
},
});
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
)
);
expect(installedHooks).toEqual({});
});

it("creates git hooks during postinstall when SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to 0", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
execSync(`node ${require.resolve("./postinstall")}`, {
cwd: PROJECT_WITH_CONF_IN_PACKAGE_JSON,
env: {
...process.env,
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "0",
},
});
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
)
);
expect(installedHooks).toEqual({ "pre-commit": TEST_SCRIPT });
});
});
});

Expand Down