Grunt task with custom json setting

I want to create some kind of task where I can read custom configs from different json files and replace files inside my source files with the contents of json files and concatenate the source files.

my projekt-setup:

  • ./CSI

    • file1.coffee
    • file2.coffee
  • ./configurations

    • / folder1

      • development.json (contains: {"key": "value1"}
      • production.json (contains: {"key": "value2"}
    • / folder2

      • development.json (contains: {"key": "value3"}
      • production.json (contains: {"key": "value4"}
  • ./distance

    • package-name.coffee
    • Package-name.js

file1.coffee contains

myVar = '@@putkeyhere'
version = '@@version'
...

      

I have a grunt concet task set up and working:

concat: {
  dist: {
    src: ['<banner>', './src/*.coffee'],
    dest: './dist/<%= pkg.name %>.coffee'
  }
},

      

I have a task to replace grunt (version replacement etc already works when I run "grunt replace" on already concatenated files)

replace: {
  dist: {
    options: {
      variables: {
        'created': '<%= grunt.template.today("dd.mm.yyyy HH:MM:ss") %>',
        'environment': 'dev',
        'version': '<%= pkg.version %>'
      },
      prefix: '@@'
    },
    files: {
      'dist/': ['./dist/<%= pkg.name %>.coffee']
    }
  }
},

      

and finally the task of compiling coffee:

coffee: {
  compile: {
    files: {
      './dist/<%= pkg.name %>.js': ['./dist/*.coffee']        
    }
  }
}

      

all the tasks work on their own, but I need to read from the config-json files that replace the content in the concatenated coffee files and then compile all the files to js.

I tried something like this, but it doesn't seem right:

grunt.registerTask('mytask', '', function (env) {

  env = env || 'development';
  if (env !== 'development' && env !== 'production') {
    grunt.log.error("'" + env + "' is not valid environment");
    return false;
  }

  var c = grunt.option('c');
  if(c) {

    // if i run the task "grunt mytask:production -c folder2 it should read
    // ./config/folder2/development.json
    // that works that way, but i dont think this is a good solution
    var config = grunt.file.readJSON('./config/' + c + '/' + env + '.json')


  } else {

     // here i need to iterate for all folders in ./config, and do stuff for all
  }

});

      

- multiTask option? but how to read dynamically from config.json files?

Appreciate your help!

+3


source to share


1 answer


I found a plugin to help you do this - https://github.com/outaTiME/grunt-replace



0


source







All Articles