@husky-hook-creator/core
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

Welcome to @husky-hook-creator/core 👋

The library gonna help you to configure all your husky hooks

Version Prerequisite Documentation Maintenance

🏠 Homepage

Table of Contents

  1. Prerequisites
  2. Install
  3. Getting Started
  4. Features
    1. Commands
      1. Pre install command
      2. Install Husky command
      3. Husky hooks command
      4. Run all command
    2. Runner
      1. ShellJs Runner
      2. custom Runner
  5. Local Development
    1. Install Local Dependencies
    2. Run unit testing
    3. Run coverage unit testing

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.

  1. Create new Node Project

  2. Inside your project run yarn add @husky-hook-creator/core ts-node@10.3.0

  3. 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.

  1. Install the ts-node executing the command yarn add -D ts-node@10.3.0
  2. 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:

  1. pre-install-command - Execute all commands before install husky library
  2. install-husky - Install husky library
  3. husky-hooks - Install git hooks using husky library
  4. run-all-commands - Execute all commands

Pre install command

Execute all scripts commands before install the husky library.

Syntax

addPreInstallCommand(scriptCommand: string)

Usage

  1. From an instance of HuskyRunner use the method addPreInstallCommand(scriptCommand: string)
  2. 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

  1. From an instance of HuskyRunner use the method installHusky(huskyInstallCommand?: string)
  2. 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

  1. From an instance of HuskyRunner use the method addCommand(command: CommandHookInterface)
  2. 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

  1. From an instance of HuskyRunner use the method runAllCommands())
  2. 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:

  1. ShellJsExecutor - Runner that uses shellJs to run all commands
  2. HuskyRunnerInterface - Custom interface to provide your Runner

Shell JS Runner

Default Runner to execute all husky hooks.

Usage

  1. Import all required class import { CommandHookFactory, HuskyRunnerFactory } from '@husky-hook-creator/core';
  2. 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

  1. Create new file that implements the interface ExecutorInterface.ts
  2. Call Runner Factory HuskyRunnerFactory.createCustomRunner(runner:ExecutorInterface);
  3. 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 thiagoolsilva@gmail.com, kaio monteiro calás da costa kaiomonteiro151@gmail.com

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 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

Package Sidebar

Install

npm i @husky-hook-creator/core

Weekly Downloads

0

Version

1.0.5

License

Apache-2.0

Unpacked Size

265 kB

Total Files

107

Last publish

Collaborators

  • thiagoolsilva