wecodemore / grunt-githooks

A Grunt plugin to help bind Grunt tasks to Git hooks
https://npmjs.org/package/grunt-githooks
MIT License
317 stars 24 forks source link

Running githooks conditionally #49

Closed danwellman closed 8 years ago

danwellman commented 8 years ago

Is there a way to run the githooks conditionally based on a command-line flag? E.g. I have my test tasks being run on git commit, which is great, but I'd like to be able to do a commit without running the tests by passing --no-test or similar. Is this possible? I've tried using a function as the value of the git hook and returning the list of tasks to run, and I've tried setting module.exports to a function and returning the whole configuration object. Neither of these worked

franz-josef-kaiser commented 8 years ago

@danwellman First off: Do you really want to run all your tests on all commits? I'd avoid that and just run them when merging feature-/issue-branches to the dev or master branch (and exit the merge in case tests do not pass).

Also, could you show in code what you have tried? Thanks in advance.

danwellman commented 8 years ago

Thanks; well, we have the tests running when a feature branch is merged to dev on Teamcity, but the problem I'm trying to solve is that devs kept breaking the build because they'd forget to run the tests before committing, then the tests (or lint) would fail on the build server, resulting in wasted time for everyone. Running the tests on every commit has fixed this issue entirely - the build hasn't broken since we added the githooks task. Now though, it's a pain to do a WIP commit in order to e.g change branches.

The current task configuration is:

module.exports = {
    dev: {
        'pre-commit': 'lint test'
    }
};

I've tried:

module.exports = {
    dev: {
        'pre-commit': function () {
            return 'lint test';
        }
    }
};

And also:

module.exports = function () {
    return {
        dev: {
            'pre-commit': 'lint test'
        }
    };
}

But both variations failed with the same error:

exec('grunt function () {
     ^^^^^^^^^^^^^^^^^^^^

SyntaxError: Unexpected token ILLEGAL
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:430:10)
    at startup (node.js:141:18)
    at node.js:1003:3
franz-josef-kaiser commented 8 years ago

@danwellman You could possible fetch the arguments in the node.js.hb template and abort based on your arguments (or args).

Have you gotten around the fact that you, as user, are meant to customize the template to your needs? Just asking because it always seems to me that people do not get around this fact and file PRs for the default just to get changes into upstream.

danwellman commented 8 years ago

Thanks, that sounds ideal. I'll look into customizing the template, I hadn't gotten around this! Thanks for the tip :)

franz-josef-kaiser commented 8 years ago

@danwellman hope that helps. In case it does not, feel free to reopen and ping me.