在 git 提交时启动传递参数的 grunt 任务

Launch grunt task passing params on git commit

我从 grunt 开始,我已经定义了这个任务并且工作得很好:

module.exports = function(grunt) {

grunt.initConfig({

    jshint: {
        force: false,
        options: {
            evil: true,
            regexdash: true,
            browser: true,
            wsh: true,
            trailing: true,
            multistr: true,
            sub: true,
            loopfunc: true,
            expr: true,
            jquery: true,
            newcap: true,
            plusplus: false,
            curly: true,
            eqeqeq: true,
            globals: {
                jQuery: true
            }
        },
        src: ['workspace/**/*.js']
    }

});


grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.registerTask('default', ['jshint:src']);


};

现在,我想启动此任务,将 src 像参数一样传递(使用 git 个提交的文件)。

我已经在 .git[= 的 pre-commit 脚本中尝试过26=] 文件夹,但它不起作用:

var exec = require('child_process').exec;

exec('grunt jshint', {
       cwd: 'C:\workspace',
       src: 'subfolder\**\*.js'
     }, function (err, stdout, stderr) {

  console.log(stdout);

  var exitCode = 0;
  if (err) {
    console.log(stderr);
    exitCode = -1;
  }

  process.exit(exitCode);
});

如何将执行时的参数传递给我的 grunt 任务?

非常感谢,最诚挚的问候。

If you want to pass command line parameters to grunt, you have to:

  1. on the command line, use the syntax --paramName=value
  2. in the gruntfile, use grunt.option('paramName')

So in your case you would call

exec('grunt jshint --src=subfolder\**\*.js', {cwd: 'C:\workspace'}, function (err, stdout, stderr) {...});

and you gruntfile would be:

jshint: {
    (...)
    src: [grunt.option('src')]
}