• Stars
    star
    317
  • Rank 131,801 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 11 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Grunt plugin to help bind Grunt tasks to Git hooks

grunt-githooks v0.6.0

Build Status Code Climate Downloads per Month

A Grunt plugin to help bind Grunt tasks to Git hooks

Getting Started

This plugin requires at least Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-githooks --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-githooks');

The "githooks" task

Overview

In your project's Gruntfile, add a section named githooks to the data object passed into grunt.initConfig().

grunt.initConfig({
  githooks: {
    options: {
      // Task-specific options go here.
    },
    all: {
      options: {
        // Target-specific options go here
      },
      // Hook definitions go there
    }
  },
})

Defining a few hooks

Hooks are listed as keys of your target configuration. Any key other than options is considered the name of a hook you want to create. The simplest way to define a hook is to provide a space-separated list of the tasks you want the hook to run as the value.

For example:

grunt.initConfig({
  githooks: {
    all: {
      // Will create `./git/hooks/pre-commit` file which will be used at every commit,
      // so that to run the `jshint` and `test:unit` tasks before commit really happen.
      'pre-commit': 'jshint test:unit',
    }
  }
});

The plugin warns you if the name matches one of the hooks announced in the Git documentation. It will still create the hook, though, in case Git introduces new hooks in the future.

Hook specific options

If you need to override a few options for a given hook only, you can use an Object instead of a String. The taskNames property will then correspond to the tasks you want to run. Any other key will be merged into the options.

grunt.initConfig({
  githooks: {
    all: {
      options: {
        template: 'path/to/a/template'
      },
      // Will bind the jshint and test:unit tasks
      // with the template specified above
      'pre-commit': 'jshint test:unit',

      // Will bind the bower:install task
      // with a specific template
      'post-merge': {
        taskNames: 'bower:install',
        template: 'path/to/another/template'
      }
    }
  }
})

Working with existing hooks

If you happen to have existing hooks in your hook folder, the plugin appends the code launching Grunt at the end of your hooks. You can also insert marker comments in your hooks to specify exactly where you want them inserted. Your existing hook would look something like this:

// Some code run before Grunt starts

// GRUNT-GITHOOKS START // GRUNT-GITHOOKS END

// Some code run after Grunt starts

The markers get automatically inserted when the plugin appends code, so hooks get updated cleanly the next time you run grunt githooks.

Customising hook output

By default, the plugin generate NodeJS scripts for the hooks. Reasoning behind this is that creating Shell scripts won't work well for people using Windows. Plus, NodeJS is already installed as Grunt kinda needs it. However, you're not tied to it and you can customise the generated script entirely. In case of a Shell script:

grunt.initConfig({
  githooks: {
    all: {
      options: {
        // Customize the hashbang to say 'Shell script'
        hashbang: '#!/bin/sh',
        // Plugin comes in with a sheel script template already. Handy, innit?
        template: './node_modules/grunt-githooks/templates/shell.hb',
        // Customize the markers so comments start with #
        startMarker: '## LET THE FUN BEGIN',
        endMarker: '## PARTY IS OVER'
      }
    }
  }
});

In the template, you've got access to the following variables:

  • hook: String with the name of the current hook
  • command: String with the name of the command to run
  • task: String with the name of the tasks to be run
  • args: String with the list of arguments to provide to the task
  • gruntfileDirectory: Absolute path to the directory containing the Gruntfile
  • preventExit: Flag telling if the hook should avoid exiting after the grunt task
  • options: The options provided to the grunt-githooks task to create this hook

Extending the plugin

Pretty annoying when you're using a library that's missing the exact extension point you need to tweak its functionalities? grunt-githooks is based on a lot of small functions and most of them are exposed so you can override them. If you need feel, free to tinker with the internals (at your own risk though ;)). Could be something along:

var gruntGithooks = require('grunt-githooks/tasks/githooks');

var originalFunction = gruntGithooks.internals.Hook.prototype.getHookContent;
gruntGithooks.internals.Hook.prototype.getHookContent = function () {
  console.log('Loading content of an existing hook');
  originalFunction.apply(this, arguments);
};

Options

command

Type: String Defaults: grunt

The command that will be run by the hook. This has initially been introduced to allow specifying the full path to Grunt in some specific cases. It can also allow you to run another command than Grunt if you need.

taskNames

Type: String

A space separated list of tasks that will be run by the hook.

args

Type: String

Additional CLI arguments to be passed to the command run by the hook.

hashbang

Type: String Defaults: '#!/usr/bin/env node'

The hashbang that will be used at the top of the hook script file. If a hook already exist, the hashbang will be used to check if its ok to append/insert code in it (to avoid inserting Node code in a Python hook for example).

template

Type: String

Path to the Handlebars template used to generate the code that will run Grunt in the hook. Default template is the node.js.hb file located in the templates folder of the plugin. It also contains a shell.hb file with the template for a shell script hook.

Note: Handlebars escapes HTML special characters if you use only two curly braces to insert a variable in your template. Make sure you use three {{{my_var}}} if you need to insert variable that contains quotes, chevrons or anything that would be HTML escaped

startMarker

Type: String Default: '// GRUNT-GITHOOKS START'

endMarker

Type: String Default: '// GRUNT-GITHOOKS END'

startMarker and endMarker are markers the plugin use to know where to insert code if a hook already exist. If the existing hook doesn't have these markers, the code will simply be appended.

preventExit

Type: Boolean Default false

By default, the inserted code will exit the process after Grunt has run, using a -1 exit code if the task(s) failed. If you're inserting the code running Grunt in the middle of an existing hook, you might want to disable this so any code after what was inserted by the plugin runs.

dest

Type: String Default value: '.git/hooks'

This option allows you to choose in which directory the hooks should be generated. Comes in handy if your Gruntfile is not at the root of your Git project.

Contributing

In lieu of a formal style-guide, take care to maintain the existing coding style. Please file a Pull Request along your issues.

  • Add unit tests for any new or changed functionality.
  • Lint and test your code using Grunt.
  • Keep the line length at 80-100 characters per line.

File a Pull Request

The process actually is quite simple:

  1. Please check out your changes on a separate branch named issue-{$integer} and commit against that one.
  2. When your tests pass and are green, merge to dev using --no-ff so we have a separate commit for that merge.
  3. After the review on dev, we can merge to master, again using --no-ff.
  4. The merge to master will get tagged. We use the SemVer standard, so no leading v.

If your PR is successful, you will get added as contributor to the repo. We trust you after your first PR made it into the repo and you then have access for further changes, handling issues, etc. So the important thing is to add your name to the package.json array of contributors when changing or adding some code for a PR. Please do that in a separate commit.

Release History

See Changelog for details or the Release list - (see latest for recent updates).

More Repositories

1

WordPress-Gear

A bunch of gear for WP developers
CSS
452
star
2

wpstarter

Easily bootstrap whole site Composer packages for WordPress.
PHP
244
star
3

GitPHPHooks

Write your Git Hooks in PHP, organize them on a per-project base and automatically add them.
PHP
64
star
4

WP-Strip-Naked

Strips WordPress down to it's bare essentials
PHP
48
star
5

wcm_lang_switch

WordPress plugin to switch language on a per use base
PHP
21
star
6

wp-composer-config

Auto generate your wp-config as Composer Script
PHP
14
star
7

GitPHPHooksLibrary

A collection of Git PHP Hooks that you maybe want to use with GitPHPHooks
PHP
12
star
8

wp-importer

Bug free fork of the official WP_Importer
PHP
10
star
9

wp-cli-composer

Add bash autocomplete for WP-CLI as Composer post-package-install script
PHP
9
star
10

wp-downloader

Composer plugin that downloads WordPress releases zip from official repo.
PHP
8
star
11

wcm-avatar

A WordPress plugin to enable custom attachment uploads to be used as user avatars
PHP
5
star
12

wordpress-early-hook

Small library to safely add WordPress hooks before WordPress is loaded.
PHP
5
star
13

wcm-termlimit

WordPress plugin to set a minimum needed tags/cats/etc. Or limit the amount of tags/cats or custom tax terms. On a per post type basis. Avoid authors using all your tags because of SEO. Or force a minimum because they don't care tagging posts. Just like seen on StackOverflow and other StackExchange sites: minimum 1 tag, maximum 5.
PHP
5
star
14

wp-ssl

Utility class to check if a (local) URl is accessible via SSL or not
PHP
3
star
15

wpguidelines

WordPress coding standards and examples
PHP
3
star
16

wp-package-assets-publisher

A Composer plugin that publishes assets for packages where WordPress can find them.
PHP
2
star
17

wcm_top_spam_ips

A WordPress plugin that collects the Top Spam IP addresses and lists them on a new admin tools page. Allows to export them and to block via your .htaccess file.
PHP
2
star