admin管理员组

文章数量:1187320

is there a way to pass in an array to grunt.js from the package.json file? I've tried a few different ways and none of them seem to work. I currently have:

/*global module:false*/
module.exports = function(grunt) {

     // Project configuration.
     grunt.initConfig({
    pkg: '<json:package.json>',

    lint: {
      files: '<%= pkg.lint.join(", ") %>'
    }

    // Default task 'lint qunit concat min'
    grunt.registerTask('default', 'lint');
};

package.json

{
  "lint": [   
              "grunt.js",
              "test.js"
          ]
}

The only solution that I have been able to find is to pass in a specific index of the array; e.g. <%= pkg.lint[0] %>. Thanks in advance for your help!

is there a way to pass in an array to grunt.js from the package.json file? I've tried a few different ways and none of them seem to work. I currently have:

/*global module:false*/
module.exports = function(grunt) {

     // Project configuration.
     grunt.initConfig({
    pkg: '<json:package.json>',

    lint: {
      files: '<%= pkg.lint.join(", ") %>'
    }

    // Default task 'lint qunit concat min'
    grunt.registerTask('default', 'lint');
};

package.json

{
  "lint": [   
              "grunt.js",
              "test.js"
          ]
}

The only solution that I have been able to find is to pass in a specific index of the array; e.g. <%= pkg.lint[0] %>. Thanks in advance for your help!

Share Improve this question edited Sep 13, 2012 at 20:40 Phillip Whisenhunt asked Sep 13, 2012 at 14:25 Phillip WhisenhuntPhillip Whisenhunt 1,3052 gold badges18 silver badges30 bronze badges 6
  • 2 Did you try <%= pkg.lint %> instead? The lint task seems to accept an array, not a comma-separated list of files... – Dmitry Pashkevich Commented Sep 13, 2012 at 17:03
  • Yep, I've tried that as well. What is strange is that I can pass in an index to the array, such as pkg.lint[0] and it will lint that specific file. – Phillip Whisenhunt Commented Sep 13, 2012 at 17:27
  • As a quick debug method, try to change the last line to grunt.registerTask('default', 'lint', function() {console.log(grunt.config('lint'))}); and see what gets output in the console... – Dmitry Pashkevich Commented Sep 13, 2012 at 19:19
  • I've also tried creating a helper in grunt.js, but it accepts all of it's parameters as strings... – Phillip Whisenhunt Commented Sep 13, 2012 at 19:43
  • What gets logged if you put the code I suggested above? – Dmitry Pashkevich Commented Sep 14, 2012 at 5:17
 |  Show 1 more comment

3 Answers 3

Reset to default 25

Since gruntjs in run in node you can access the package.json like:

var package = require('./package.json'),
    property = package.property[0];

I think that the <%= … %> syntax (variable interpolation in Underscore's template system) can only output strings, not arrays/objects.

Try this instead:

lint: {
    files: '<config:pkg.lint>'
}

I found this syntax in Grunt's jQuery init task.

grunt.initConfig({
  lint: grunt.file.readJSON('package.json').lint,
});

本文标签: javascriptHow to pass in packagejson array to gruntjsStack Overflow