@husky-hook-creator/core
v1.0.5
Published
### The library gonna help you to configure all your husky hooks
Downloads
1
Maintainers
Readme
Welcome to @husky-hook-creator/core 👋
The library gonna help you to configure all your husky hooks
🏠 Homepage
Table of Contents
Prerequisites
- node >=14.0.0
- husky >=7.0.2
Install
yarn add @husky-hook-creator/core
Getting Started
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 [email protected]
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();
(Optional) ts-node
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 [email protected]
- 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.
Features
The husky library uses the concept of pipeline to run all commands in sequence.
Commands
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
Pre install command
Execute all scripts commands before install the husky library.
Syntax
addPreInstallCommand(scriptCommand: string)
Usage
- 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();
Install Husky command
Execute the script command to install husky library.
Syntax
installHusky(huskyInstallCommand?: string)
Usage
- 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.
Husky hooks command
Execute all git hooks commands.
Syntax
addCommand(command: CommandHookInterface)
Usage
- 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
Run all command
Start core Runner hooks
Usage
- 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();
Runner
All available Runner are:
- ShellJsExecutor - Runner that uses shellJs to run all commands
- HuskyRunnerInterface - Custom interface to provide your Runner
Shell JS Runner
Default Runner to execute all husky hooks.
Usage
- 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();
Custom Runner
The library provides the interface ExecutorInterface.ts
that help us to create a custom Runner.
Usage
- Create new file that implements the interface
ExecutorInterface.ts
- Call Runner Factory
HuskyRunnerFactory.createCustomRunner(runner:ExecutorInterface);
- Execute your husky hooks
Full Example:
File: custom-executor.ts
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);
}
}
File: husky-custom-runner.ts
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();
})();
Local Development
In order to create new local features you need to follow some steps as will be shown below.
install-local-dependencies
To install all library dependencies you should execute the follow command.
cd library && yarn
Run unit testing
To execute all unit testing you should execute the follow command.
cd library && yarn test:unit-testing
Run coverage unit testing
To execute all coverage unit testing you should execute the follow command.
cd library && yarn test:unit-testing:coverage
Author
👤 thiago lopes da silva [email protected], kaio monteiro calás da costa [email protected]
- Website: https://medium.com/@thiagolopessilva
- Github: @thiagoolsilva
- LinkedIn: @thiago-lopes-silva-2b943a25
Code of Conduct
Feel free to check the code of conduct guide.
Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.
Show your support
Give a ⭐️ if this project helped you!
📝 License
Copyright © 2022 thiago lopes da silva [email protected], kaio monteiro calás da costa [email protected].
This project is Apache License 2.0 licensed.
This README was generated with ❤️ by readme-md-generator