Skip to content

Writing Generator Hooks

Lakshan Perera edited this page Feb 27, 2014 · 6 revisions

Generator hooks run after each page is generated (when running punch generate). A generator hook should implement a run function. Punch calls a generator hook with the request path of the generated file, options hash containing flags to indicate whether the generated file was modified and generation process is complete. Final argument is a callback function. You have to invoke this callback after hook finishes its execution.

Here's how the implementation of a generator hook would look like:

    module.exports = {

        run: function(path, options, callback) {
                  var modified = options.modified || false;
                  var finished = options.finished || false;

                  // do something   

              return callback();
        }

    };

How to use a generator hook

To use a generator hook in a project, you have to define it as a plugin in project's configuration (config.json).

    "plugins": {
        "generator_hooks": {
            "custom_hook": "custom_hook_path" 
        }
    }

Path of the generator hook should be specified as a valid Node.js module path.

Something went wrong with that request. Please try again.