🏠 Homepage
- node >=14.0.0
- husky >=7.0.2
yarn add @husky-hook-creator/core
This section will help you to install the library on a node project. The following steps use assume you have Node.js >= 14 and Yarn installed.
-
Create new Node Project
-
Inside your project run
yarn add @husky-hook-creator/core ts-node@10.3.0
-
Create a new file called
husky-hooks.ts
and insert the following code
import { CommandHookFactory, HuskyRunnerFactory } from '@husky-hook-creator/core';
const huskyHook = HuskyRunnerFactory.createShellJsRunner();
await huskyHook
.addPreInstallCommand('rm -rf .husky')
.installHusky()
.addCommand(CommandHookFactory.createHookCommand('pre-commit', 'echo this is a pre-commit hook.'))
.addCommand(CommandHookFactory.createHookCommand('pre-push', 'echo this is a pre-push hook.'))
.runAllCommands();
The library ts-node gonna help us to run the typescript code executing and installing all git hooks from the library.
- Install the ts-node executing the command
yarn add -D ts-node@10.3.0
- Execute the created script executing the command
ts-node yourPath/your-runner.ts
For more details, please check the script named create-hooks-with-custom-executor at package.json.
The husky library uses the concept of pipeline to run all commands in order as will be show below.
All available commands are:
- pre-install-command - Execute all commands before install husky library
- install-husky - Install husky library
- husky-hooks - Install git hooks using husky library
- run-all-commands - Execute all commands
Execute all scripts commands before install the husky library.
addPreInstallCommand(scriptCommand: string)
- From an instance of HuskyRunner use the method
addPreInstallCommand(scriptCommand: string)
- Call
runner.addPreInstallCommand('rm -rf library/.husky');
from class HuskyRunner to configure the command.
Full Example
import { CommandHookFactory, HuskyRunnerFactory } from '@husky-hook-creator/core';
const huskyHook = HuskyRunnerFactory.createShellJsRunner();
await huskyHook
.addPreInstallCommand('rm -rf .husky')
.addCommand(CommandHookFactory.createHookCommand('pre-push', 'echo this is a pre-push hook.'))
.runAllCommands();
Execute the script command to install husky library.
installHusky(huskyInstallCommand?: string)
- From an instance of HuskyRunner use the method
installHusky(huskyInstallCommand?: string)
- Call
runner.installHusky();
from class HuskyRunner to install the library.
Full Example
import { CommandHookFactory, HuskyRunnerFactory } from '@husky-hook-creator/core';
const huskyHook = HuskyRunnerFactory.createShellJsRunner();
await huskyHook
.installHusky()
.addCommand(CommandHookFactory.createHookCommand('pre-push', 'echo this is a pre-push hook.'))
.runAllCommands();
If you need to install husky library from a folder that does not contains any .git folder, please check the example in the file.
Execute all git hooks commands.
addCommand(command: CommandHookInterface)
- From an instance of HuskyRunner use the method
addCommand(command: CommandHookInterface)
- Call
runner.addCommand(command);
from class HuskyRunner to create new husky command.
Full Example
import { CommandHookFactory, HuskyRunnerFactory } from '@husky-hook-creator/core';
const huskyHook = HuskyRunnerFactory.createShellJsRunner();
await huskyHook
.addCommand(CommandHookFactory.createHookCommand('pre-push', 'echo this is a pre-push hook.'))
.runAllCommands();
Warning: You need to provide valid git hooks when creating new hooks. For more details go to https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
Start core Runner hooks
- From an instance of HuskyRunner use the method
runAllCommands())
- Call
runner.runAllCommands();
from class HuskyRunner to start husky hooks.
Full Example
import { CommandHookFactory, HuskyRunnerFactory } from '@husky-hook-creator/core';
const huskyHook = HuskyRunnerFactory.createShellJsRunner();
await huskyHook
.addCommand(CommandHookFactory.createHookCommand('pre-push', 'echo this is a pre-push hook.'))
.runAllCommands();
All available Runner are:
- ShellJsExecutor - Runner that uses shellJs to run all commands
- HuskyRunnerInterface - Custom interface to provide your Runner
Default Runner to execute all husky hooks.
- Import all required class
import { CommandHookFactory, HuskyRunnerFactory } from '@husky-hook-creator/core';
- Call the
HuskyRunnerFactory.createShellJsRunner()
to create new ShejjJSRunner
Full Example
import { CommandHookFactory, HuskyRunnerFactory } from '@husky-hook-creator/core';
const huskyHook = HuskyRunnerFactory.createShellJsRunner();
await huskyHook
.addCommand(CommandHookFactory.createHookCommand('pre-push', 'echo this is a pre-push hook.'))
.runAllCommands();
The library provides the interface ExecutorInterface.ts
that help us to create a custom Runner.
- Create new file that implements the interface
ExecutorInterface.ts
- Call Runner Factory
HuskyRunnerFactory.createCustomRunner(runner:ExecutorInterface);
- Execute your husky hooks
Full Example:
import { ExecutorInterface } from '@husky-hook-creator/core';
import execa from 'execa';
export class CustomExecutor implements ExecutorInterface {
public async exec(command: string): Promise<void> {
const resultCommand = await execa(command, undefined, {
shell: true,
});
console.log(resultCommand.stdout);
}
}
import { CommandHookFactory, HuskyRunnerFactory } from '@husky-hook-creator/core';
import { CustomExecutor } from './custom-executor/execa-executor';
// For more details about git hooks go to https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
(async () => {
const huskyHook = HuskyRunnerFactory.createCustomRunner(new CustomExecutor());
await huskyHook
.addPreInstallCommand('rm -rf .husky')
.installHusky('cd .. && husky install sample/.husky')
.addCommand(
CommandHookFactory.createHookCommand('pre-commit', 'echo your second shell script goes here.'),
)
.runAllCommands();
})();
In order to create new local features you need to follow some steps as will be shown below.
To install all library dependencies you should execute the follow command.
cd library && yarn
To execute all unit testing you should execute the follow command.
cd library && yarn test:unit-testing
To execute all coverage unit testing you should execute the follow command.
cd library && yarn test:unit-testing:coverage
👤 thiago lopes da silva thiagoolsilva@gmail.com, kaio monteiro calás da costa kaiomonteiro151@gmail.com
- Website: https://medium.com/@thiagolopessilva
- Github: @thiagoolsilva
- LinkedIn: @thiago-lopes-silva-2b943a25
Feel free to check the code of conduct guide.
Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.
Give a ⭐️ if this project helped you!
Copyright © 2022 thiago lopes da silva thiagoolsilva@gmail.com, kaio monteiro calás da costa kaiomonteiro151@gmail.com.
This project is Apache License 2.0 licensed.
This README was generated with ❤️ by readme-md-generator