سؤال

I want pass a parameter to my Gruntfile.js so the grunt.file.readJSON can read the filename passed in. What should I do?

  grunt.initConfig({
    filename: grunt.option('filename'),
    config: grunt.file.readJSON('<%= filename %>'),
    ... ... ... 
  });

The above does not work.

> grunt build-dev --filename=test.json
Loading "Gruntfile.js" tasks...ERROR
>> Error: Unable to read "<%= filename %>" file (Error code: ENOENT).
هل كانت مفيدة؟

المحلول

Grunt templates only work within the Grunt config itself. grunt.file.readJSON is a function that takes a single argument and does not process template patterns.

But rather just reads a JSON file from the specified file path and feeds that object to the Grunt config (which means the JSON file can contain Grunt template patterns).

Try the following instead:

grunt.initConfig({
  filename: grunt.option('filename'),
  config: grunt.file.readJSON(grunt.option('filename')),
  // ... ... ... 
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top