基于输入的 Grunt 更改目标

Grunt change target based on input

我的Gruntfile如下:

module.exports = function(grunt) {
  'use strict';

  var dictionary = {
      'all' : '**',
      'html5' : 'some/path'
  };

  require('time-grunt')(grunt);
  require('load-grunt-tasks')(grunt);

  grunt.initConfig({
      eslint : {
        options : {
          config : '.eslintrc'
        },
        target : ['hello/world/js/<HERE>/**.js']
      }
  });

  grunt.registerTask('test', 'Lint a set of files', function(set) {
    set = set || 'all';
    var path = dictionary[set];

    grunt.task.run('eslint');
  });
};

注意代码中的<HERE>。那就是我想要插入 path 变量的地方。我只是不知道该怎么做。

如果我键入 grunt test:html5path 变量将设置为正确的路径,因此我可以正常工作,现在我只需要告诉 ESLint 在哪里进行 lint。但是怎么办?

编辑: 根据接受的答案,我现在有了这个,它有效!我想分享一下,以防其他人想看一看。

module.exports = function(grunt) {
  'use strict';

  var dictionary = {
      'webroot' : 'app/webroot/**',
      'html5' : 'app/webroot/js/some/path'
  };

  require('time-grunt')(grunt);
  require('load-grunt-tasks')(grunt);

  grunt.initConfig({
      eslint : {
        options : {
          config : '.eslintrc'
        },
        target : ['<%= path %>']
      }
  });

  grunt.registerTask('test', 'Lint a set of files', function(pathKey) {
    pathKey = pathKey || 'webroot';
    var path = (dictionary[pathKey] || pathKey) + '/*.js';
    console.log('Parsed path as', path);

    grunt.config.set('path', path);
    grunt.task.run('eslint');
  });
};

在 grunt 配置中保存给定路径的值并引用它:

module.exports = function(grunt) {
  'use strict';

  var dictionary = {
      'all' : '**',
      'html5' : 'some/path'
  };

  require('time-grunt')(grunt);
  require('load-grunt-tasks')(grunt);

  grunt.initConfig({
      eslint : {
        options : {
          config : '.eslintrc'
        },
        target : ['hello/world/js/<%= dir %>/**.js']
      }
  });

  grunt.registerTask('test', 'Lint a set of files', function(pathKey) {
      var dir = dictionary[pathKey]
      grunt.config.set('dir', dir );

    grunt.task.run('eslint');
  });
};

您可以使用 grunt.option 将参数传递给 Gruntfile。

在 Gruntfile 中:

grunt.initConfig({
    eslint : {
      options : {
        config : '.eslintrc'
      },
      target : ['hello/world/js/<%= grunt.option('path') %>/**.js']
    }
});

从 CLI:grunt test --path=foo 获取 'hello/world/js/foo/**.js'