husky-commit-triggers
v1.0.0
Published
husky-commit-triggers automates environment setup tasks during branch checkouts. It scans commit messages for specific markers and automatically runs configured commands when you switch branches. The tool supports directional behavior by running one comma
Readme
husky-commit-triggers
husky-commit-triggers automates environment setup tasks during branch checkouts. It scans commit messages for specific markers and automatically runs configured commands when you switch branches. The tool supports directional behavior by running one command when moving to a newer commit ("up") and another when reverting to an older commit ("down").
Features
- Automated Setup on Checkout: On branch switches, the tool scans commit messages between the previous and current commits for markers defined in your configuration.
- Directional Behavior: It compares commit timestamps to determine whether you are moving "up" (to a newer commit) or "down" (to an older commit), executing the corresponding command.
- Ordered Execution: Markers are processed in the order they appear in your
package.json, and each marker is executed only once per checkout. - Simple Configuration: Define your commit triggers in your
package.jsonwithout any extra setup.
Installation
Install the package from npm:
npm install husky-commit-triggers --save-devThe package’s postinstall script will automatically:
- Add a
postCheckoutScriptsproperty to yourpackage.jsonif one does not already exist. - Set up a Husky post-checkout hook that runs the commit message scanning logic.
Configuration
Define your commit triggers in your package.json under the postCheckoutScripts property. Each key is a commit marker, and its value can be either a string (the default command) or an object with up and down keys for directional behavior. The order that the keys are defined in your package.json determines the order in which they are executed
For example:
"postCheckoutScripts": {
"install_dependencies": "npm install",
"migrate_db": {
"up": "node scripts/runMigrations.js",
"down": "node scripts/rollbackMigrations.js"
}
}In this example:
- A commit message containing
[install_dependencies]will triggernpm install. - A commit message containing
[migrate_db]will triggernode scripts/runMigrations.jswhen switching to a newer commit andnode scripts/rollbackMigrations.jswhen switching to an older commit.
Usage
Include Markers in Commit Messages: When making commits, include markers in square brackets. For example:
chore: update dependencies [install_dependencies]feat: add new migration [migrate_db]
Switch Branches: When you switch branches, the Husky post-checkout hook will:
- Compare the commit timestamps between the branch you are leaving and the branch you are checking out.
- Scan the commit messages in that range for any defined markers.
- Execute the corresponding command for each marker in the order they are defined in your
package.json.
Advanced Configuration
To control the verbosity of the output, you can set the LOG_LEVEL environment variable. For example, to display only error messages:
LOG_LEVEL=errorContributing
Contributions are welcome! Please open issues or submit pull requests if you have suggestions or improvements.
License
This project is licensed under the MIT License.
