### Install and Basic Setup for simple-git-hooks Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt This snippet shows how to install simple-git-hooks as a development dependency, configure it within package.json, and then manually install the Git hooks using the CLI. It's a fundamental setup for using the tool. ```bash # Install as dev dependency npm install simple-git-hooks --save-dev # Configure in package.json { "simple-git-hooks": { "pre-commit": "npx lint-staged", "pre-push": "npm test" } } # Install hooks npx simple-git-hooks ``` -------------------------------- ### CLI Usage for Installing and Updating Git Hooks Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt This snippet covers the command-line interface (CLI) usage for simple-git-hooks. It shows how to install or update Git hooks manually, including commands for npm, yarn 2+, and yarn 1. It also provides examples of expected output and how to manage hooks when they are already up-to-date. ```bash # Install or update hooks from config npx simple-git-hooks # Expected output when hooks are created/updated: # [INFO] Successfully set the pre-commit with command: npx lint-staged # [INFO] Successfully set the pre-push with command: npm test # [INFO] Successfully set all git hooks # No output when hooks are already up-to-date # For yarn 2+ yarn dlx simple-git-hooks # For yarn 1 ynpx simple-git-hooks ``` -------------------------------- ### Integrate simple-git-hooks with lint-staged Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt Configures `simple-git-hooks` to use `lint-staged` for pre-commit hook functionality. This example shows `package.json` settings for installing `simple-git-hooks`, `lint-staged`, `eslint`, and `prettier`, and defines which files `lint-staged` should process. ```json { "devDependencies": { "simple-git-hooks": "^2.13.1", "lint-staged": "^10.5.4", "eslint": "^7.19.0", "prettier": "^2.2.1" }, "simple-git-hooks": { "pre-commit": "npx lint-staged" }, "lint-staged": { "*.{js,jsx,ts,tsx}": [ "eslint --fix", "prettier --write" ], "*.{json,md,yml}": [ "prettier --write" ] } } ``` -------------------------------- ### Skip Git Hook Installation with Environment Variables Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt This section explains how to skip the automatic installation of Git hooks managed by simple-git-hooks, primarily using environment variables. It covers skipping during `npm install` and skipping hooks for multiple or single Git operations. ```bash # Skip installation during npm install export SKIP_INSTALL_SIMPLE_GIT_HOOKS=1 npm install simple-git-hooks --save-dev # Skip hooks for multiple git operations export SKIP_SIMPLE_GIT_HOOKS=1 git add . git commit -m "commit message" # pre-commit hooks skipped git push origin main # pre-push hooks skipped # Skip hooks for single git operation git commit -m "commit message" --no-verify # Skip with shorthand git commit -m "commit message" -n ``` -------------------------------- ### Install simple-git-hooks (npm) Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md Installs simple-git-hooks as a development dependency using npm. This command should be run from the project's root directory. ```sh npm install simple-git-hooks --save-dev ``` -------------------------------- ### Skip Git Hooks Installation on CI Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md This command sets an environment variable to skip the installation of simple-git-hooks, commonly used in CI environments. It's followed by the installation command. ```sh export SKIP_INSTALL_SIMPLE_GIT_HOOKS=1 npm install simple-git-hooks --save-dev ``` -------------------------------- ### Migrating from Husky to simple-git-hooks Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt This bash script outlines the steps to migrate from the Husky git hook manager to simple-git-hooks. It includes uninstalling Husky, resetting git hook paths, removing old configuration, installing simple-git-hooks, and updating package.json. ```bash # 1. Remove husky npm uninstall husky # 2. Reset git hooks path (husky changes this to .husky) git config core.hooksPath .git/hooks/ # 3. Verify the setting git config core.hooksPath # Should output: .git/hooks/ # 4. Remove .husky directory rm -rf .husky # 5. Install simple-git-hooks npm install simple-git-hooks --save-dev # 6. Update package.json { "scripts": { "prepare": "npx simple-git-hooks" }, "simple-git-hooks": { "pre-commit": "npx lint-staged" } } # 7. Install hooks npx simple-git-hooks ``` -------------------------------- ### Robust Git Hook Setup with Error Handling Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt This JavaScript function demonstrates how to robustly set up git hooks using `simple-git-hooks`. It includes error handling for common issues like missing configuration, invalid hook formats, and non-git repositories. ```javascript const { setHooksFromConfig } = require('simple-git-hooks'); async function robustHookSetup() { try { await setHooksFromConfig(process.cwd()); console.log('✓ Hooks installed successfully'); } catch (error) { if (error.includes('Config was not found')) { console.error('Missing configuration. Add simple-git-hooks config to package.json'); process.exit(1); } if (error.includes('not in correct format')) { console.error('Invalid hook name in configuration'); process.exit(1); } if (error.includes('No `.git` root folder found')) { console.error('Not a git repository. Run: git init'); process.exit(1); } console.error('Unexpected error:', error); process.exit(1); } } robustHookSetup(); ``` -------------------------------- ### Integrate simple-git-hooks with commitlint Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt Sets up `simple-git-hooks` to use `commitlint` for enforcing conventional commit message formats. The `package.json` configuration includes installing `simple-git-hooks`, `@commitlint/cli`, and `@commitlint/config-conventional`, and specifies the `commit-msg` hook. ```json { "devDependencies": { "simple-git-hooks": "^2.13.1", "@commitlint/cli": "^17.0.0", "@commitlint/config-conventional": "^17.0.0" }, "simple-git-hooks": { "commit-msg": "npx commitlint --edit $1" }, "commitlint": { "extends": ["@commitlint/config-conventional"] } } ``` -------------------------------- ### Extract Project Root from Node Modules Path Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt Extracts the project root directory from a `node_modules` path using `getProjectRootDirectoryFromNodeModules`. It handles standard npm/yarn and pnpm installations, returning `undefined` for unsupported paths. ```javascript const { getProjectRootDirectoryFromNodeModules } = require('simple-git-hooks'); // Standard npm/yarn installation const projectRoot = getProjectRootDirectoryFromNodeModules( '/home/user/project/node_modules/simple-git-hooks' ); console.log(projectRoot); // /home/user/project // pnpm installation const pnpmRoot = getProjectRootDirectoryFromNodeModules( '/home/user/project/node_modules/.pnpm/simple-git-hooks@2.13.1/node_modules/simple-git-hooks' ); console.log(pnpmRoot); // /home/user/project // Returns undefined for unsupported paths const invalid = getProjectRootDirectoryFromNodeModules('/some/other/path'); console.log(invalid); // undefined ``` -------------------------------- ### Set Git Hooks Programmatically using simple-git-hooks Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt Installs or updates git hooks programmatically from Node.js code using `setHooksFromConfig`. It takes a project path and command-line arguments or a custom config path. The function returns whether the hooks were changed and handles errors. ```javascript const { setHooksFromConfig } = require('simple-git-hooks'); async function setupHooks() { try { const result = await setHooksFromConfig('/path/to/project', process.argv); if (result.isHookChanged) { console.log('Hooks were updated'); } else { console.log('Hooks were already up-to-date'); } } catch (error) { console.error('Failed to set hooks:', error); } } setupHooks(); // With custom config path const result = await setHooksFromConfig( '/path/to/project', ['node', 'script.js', './custom-config.js'] ); ``` -------------------------------- ### Check simple-git-hooks Installation in Dependencies Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt Verifies if `simple-git-hooks` is installed in the project's dependencies using `checkSimpleGitHooksInDependencies`. This function takes the project root path and returns a boolean indicating its presence. ```javascript const { checkSimpleGitHooksInDependencies } = require('simple-git-hooks'); const projectRoot = '/path/to/project'; const isInstalled = checkSimpleGitHooksInDependencies(projectRoot); if (isInstalled) { console.log('simple-git-hooks found in devDependencies'); } else { console.log('simple-git-hooks not found in dependencies'); } ``` -------------------------------- ### Uninstall and Clean Up simple-git-hooks Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt Provides instructions for uninstalling the `simple-git-hooks` package and cleaning up any installed git hooks. Uninstalling the package typically removes hooks automatically via a preuninstall script, but manual removal is also possible. ```bash # Uninstall package (automatically removes hooks via preuninstall script) npm uninstall simple-git-hooks # Manual hook removal node node_modules/simple-git-hooks/uninstall.js # Verify hooks are removed ls .git/hooks/ # Only sample hooks should remain (*.sample files) ``` -------------------------------- ### Set simple-git-hooks RC Environment Variable Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md This command sets the global environment variable 'SIMPLE_GIT_HOOKS_RC' to point to the custom initialization script for simple-git-hooks. This script is executed before git hooks run, ensuring proper environment setup for Node.js binaries. ```sh export SIMPLE_GIT_HOOKS_RC="$HOME/.simple-git-hooks.rc" ``` -------------------------------- ### Configure Node Version Managers for GUI Git Clients Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt Sets up an RC file (`~/.simple-git-hooks.rc`) to configure init scripts for GUI git clients when using Node version managers like nvm or mise. This ensures GUI clients can find Node.js and npx commands. ```bash # Create RC file: ~/.simple-git-hooks.rc export PATH="$HOME/.local/share/mise/shims:$PATH" # For nvm users export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # Add to ~/.zshenv or ~/.bashrc export SIMPLE_GIT_HOOKS_RC="$HOME/.simple-git-hooks.rc" # Now GUI git clients will find npx and node commands ``` -------------------------------- ### Use Custom Configuration File Path with simple-git-hooks CLI Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt Shows how to specify a custom path to the configuration file when running the simple-git-hooks CLI. This allows flexibility in organizing configuration files within the project structure. ```bash # Use custom config file npx simple-git-hooks ./config/git-hooks.js # Custom config file structure // config/git-hooks.js module.exports = { "pre-commit": "npm run lint && npm run type-check", "pre-push": "npm run build && npm test" } ``` -------------------------------- ### List Supported Git Hooks Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt This JavaScript object lists all valid git hooks that can be configured with simple-git-hooks. Each hook is mapped to a command string that will be executed when the hook is triggered. ```javascript module.exports = { "applypatch-msg": "echo 'Applying patch...'", "pre-applypatch": "npm run lint", "post-applypatch": "npm run test", "pre-commit": "npx lint-staged", "pre-merge-commit": "npm run lint", "prepare-commit-msg": "exec < /dev/tty && npx cz --hook || true", "commit-msg": "npx commitlint --edit $1", "post-commit": "git status", "pre-rebase": "npm test", "post-checkout": "npm install", "post-merge": "npm install", "pre-push": "npm test", "pre-receive": "echo 'Receiving...'", "update": "echo 'Updating...'", "proc-receive": "echo 'Processing...'", "post-receive": "echo 'Received'", "post-update": "echo 'Updated'", "reference-transaction": "echo 'Reference updated'", "push-to-checkout": "echo 'Pushing...'", "pre-auto-gc": "echo 'Auto GC...'", "post-rewrite": "npm install", "sendemail-validate": "echo 'Validating email...'", "fsmonitor-watchman": "echo 'FSMonitor...'", "p4-changelist": "echo 'P4 changelist...'", "p4-prepare-changelist": "echo 'P4 prepare...'", "p4-post-changelist": "echo 'P4 post...'", "p4-pre-submit": "echo 'P4 pre-submit...'", "post-index-change": "echo 'Index changed...'" } ``` -------------------------------- ### Configure simple-git-hooks with CommonJS File Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt This shows how to configure simple-git-hooks using a separate CommonJS file (e.g., `simple-git-hooks.js`). This approach is useful for more complex configurations or when you prefer to keep hook definitions separate from package.json. It exports an object where keys are hook names and values are commands. ```javascript // simple-git-hooks.js module.exports = { "pre-commit": "npx lint-staged", "pre-push": "npm run format && npm test", "post-checkout": "npm install", "post-merge": "npm install", "preserveUnused": true } ``` -------------------------------- ### Update Git Hooks with CLI (npm/npx) Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md Executes the simple-git-hooks CLI to apply the configured hooks to the project's Git repository. This command should be run from the project's root directory. ```sh npx simple-git-hooks ``` -------------------------------- ### Configure Git Hooks with CommonJS File Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md This snippet demonstrates how to configure git hooks using a CommonJS module. The keys represent the git hook names, and the values are the commands to be executed. This file should be named '.simple-git-hooks.cjs' or 'simple-git-hooks.cjs'. ```javascript module.exports = { "pre-commit": "npx lint-staged", "pre-push": "npm run format" }; ``` -------------------------------- ### Update Git Hooks with CLI (Yarn 2) Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md Executes the simple-git-hooks CLI using Yarn 2's dlx command. This is the recommended way to run the CLI for Yarn 2 users. ```sh yarn dlx simple-git-hooks ``` -------------------------------- ### Find Git Project Root Directory Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt Recursively finds the `.git` directory from any project subdirectory using `getGitProjectRoot`. This is useful for locating the Git repository root, and it supports git worktrees and submodules. ```javascript const { getGitProjectRoot } = require('simple-git-hooks'); const gitRoot = getGitProjectRoot('/path/to/project/src/components'); if (gitRoot) { console.log('Git root found at:', gitRoot); // Output: /path/to/project/.git } else { console.log('Not a git repository'); } // Works with git worktrees and submodules const worktreeRoot = getGitProjectRoot('/path/to/worktree'); // Resolves to common .git directory ``` -------------------------------- ### Configure Node Version Manager Shim Path Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md This snippet shows how to configure the PATH environment variable to include the shims from a node version manager like 'mise'. This is necessary for GUI Git clients to find Node.js binaries when using such managers. ```sh export PATH="$HOME/.local/share/mise/shims:$PATH" ``` -------------------------------- ### Configure Git Hooks with JSON File Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md This snippet illustrates configuring git hooks using a JSON file. The structure is a simple key-value pair where keys are hook names and values are the commands to execute. This file can be named '.simple-git-hooks.json' or 'simple-git-hooks.json'. ```json { "pre-commit": "npx lint-staged", "pre-push": "npm run format" } ``` -------------------------------- ### Configure simple-git-hooks in package.json Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md Defines Git hooks and their associated commands within the package.json file. This JSON configuration specifies actions for 'pre-commit' and 'pre-push' hooks, with options to preserve unused hooks. ```json { "simple-git-hooks": { "pre-commit": "npx lint-staged", "pre-push": "npm run format", "preserveUnused": true "preserveUnused": ["commit-msg"] } } ``` -------------------------------- ### Configure Git Hooks with ES Modules File Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md This snippet shows how to configure git hooks using an ES Module. Similar to CommonJS, keys are hook names and values are commands. This file should be named '.simple-git-hooks.mjs' or 'simple-git-hooks.mjs'. ```javascript export default { "pre-commit": "npx lint-staged", "pre-push": "npm run format" }; ``` -------------------------------- ### Update Git Hooks with CLI (Yarn 1) Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md Executes the simple-git-hooks CLI using Yarn 1's ynpx command. This is the recommended way to run the CLI for Yarn 1 users. ```sh ynpx simple-git-hooks ``` -------------------------------- ### Fix Husky Git Hooks Path Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md This command updates the Git configuration to point the core.hooksPath to the default '.git/hooks/' directory, resolving issues where Husky might have redirected it to '.husky'. ```sh git config core.hooksPath .git/hooks/ ``` -------------------------------- ### Bypass Git Hooks for a Single Operation Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md This command demonstrates how to bypass git hooks for a specific git operation using the '--no-verify' flag. The '-n' flag is a shorthand for '--no-verify'. ```sh git commit -m "commit message" --no-verify # -n for shorthand ``` -------------------------------- ### Preserve Unused Git Hooks with simple-git-hooks Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt This demonstrates how to control which Git hooks are preserved when updating the configuration. By specifying hooks in the `preserveUnused` array or setting it to `true`, you can prevent simple-git-hooks from removing hooks that are no longer defined in the current configuration. ```json { "simple-git-hooks": { "pre-commit": "npx lint-staged", "preserveUnused": ["commit-msg", "post-merge"] } } ``` ```javascript // Preserve all unused hooks module.exports = { "pre-commit": "npx lint-staged", "pre-push": "npm test", "preserveUnused": true } ``` -------------------------------- ### Remove All Git Hooks Programmatically using simple-git-hooks Source: https://context7.com/toplenboren/simple-git-hooks/llms.txt Removes all configured git hooks programmatically from a specified project directory using the `removeHooks` function. It logs success or error messages. ```javascript const { removeHooks } = require('simple-git-hooks'); async function cleanupHooks() { try { await removeHooks('/path/to/project'); console.log('All git hooks removed successfully'); } catch (error) { console.error('Failed to remove hooks:', error); } } cleanupHooks(); ``` -------------------------------- ### Uninstall simple-git-hooks Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md This command uninstalls the simple-git-hooks package and removes any associated git hooks from the project. ```sh npm uninstall simple-git-hooks ``` -------------------------------- ### Bypass Git Hooks for a Terminal Session Source: https://github.com/toplenboren/simple-git-hooks/blob/master/README.md This command sets an environment variable to bypass all subsequent git hooks within the current terminal session. Any git operation performed after setting this variable will not trigger hooks. ```sh # Set the environment variable export SKIP_SIMPLE_GIT_HOOKS=1 # Subsequent Git commands will skip the hooks git add . git commit -m "commit message" # pre-commit hooks are bypassed git push origin main # pre-push hooks are bypassed ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.