简体   繁体   中英

In a grunt plugin, how do I get the combination of a global and target specifc option that's an array?

I'm writing a grunt plugin that has options that can be arrays of values. The values specifically are files (different than the files specified in the files property on the task itself). My task setup can look like the following:

grunt.initConfig({
    assemble: {
      options: {
        data: ['test/common/data/common1.json', 'test/common/data/common2.json']
      },
      dev: {
        options: {
          data: ['test/dev/data/dev.json']
        },
        files: {
          'test/actual': ['test/files/dev.hbs']
        }
      },
      prod: {
        options: {
          data: ['test/prod/data/prod.json']
        },
        files: {
          'test/actual': ['test/files/prod.hbs']
        }
      },
    }

});

In my plugin, I'd like to be able to get the data option with a list of all the files specified in the global options and the target options.

For the dev target grunt assemble:dev I would see this in this.options.data

['test/common/data/common1.json',
 'test/common/data/common2.json',
 'test/dev/data/dev.json']

For the prod target grunt assemble:prod I would see this in this.options.data

['test/common/data/common1.json',
 'test/common/data/common2.json',
 'test/prod/data/prod.json']

I have found a way to solve this, but I'm not sure if it's the best option.

In my plugin, I can access the global and target specific options through the grunt.config method.

var globalDataFiles = grunt.config(['assemble', 'options', 'data']) || [];
var targetDataFiles = grunt.config(['assemble', this.target, 'options', 'data']) || [];

Using lodash... var _ = require('lodash');

I can union the arrays:

var data = _.union(globalDataFiles, targetDataFiles);

I do a little more with this in my plugin, but that's how I'm initially solving this issue.

Please take a look at https://github.com/assemble/assemble/blob/master/tasks/assemble.js to see all the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM