Javascript 带有自定义json配置的grunt任务

Javascript 带有自定义json配置的grunt任务,javascript,json,coffeescript,gruntjs,Javascript,Json,Coffeescript,Gruntjs,我想创建某种任务,在这里我可以从不同的json文件中读取自定义配置,用json文件的内容替换咖啡源文件中的内容,并连接源文件 我的项目设置: /src 文件1.咖啡 文件2.咖啡 /配置 /折叠1 development.json(包含:{“key”:“value1”} production.json(包含:{“key”:“value2”} /折叠2 development.json(包含:{“key”:“value3”} production.json(包含:{“key”:“va

我想创建某种任务,在这里我可以从不同的json文件中读取自定义配置,用json文件的内容替换咖啡源文件中的内容,并连接源文件

我的项目设置:

  • /src

    • 文件1.咖啡
    • 文件2.咖啡
  • /配置

    • /折叠1

      • development.json(包含:{“key”:“value1”}
      • production.json(包含:{“key”:“value2”}
    • /折叠2

      • development.json(包含:{“key”:“value3”}
      • production.json(包含:{“key”:“value4”}
  • /区

    • 包装名称:咖啡
    • package-name.js
file1.coffee包含

myVar = '@@putkeyhere'
version = '@@version'
...
我已经为自己配置并运行了grunt concat任务:

concat: {
  dist: {
    src: ['<banner>', './src/*.coffee'],
    dest: './dist/<%= pkg.name %>.coffee'
  }
},
多任务是一个选项吗?但是如何动态读取config.json文件呢


感谢您的帮助!

我发现了一个插件,可以帮助您实现这一点-

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']
    }
  }
},
coffee: {
  compile: {
    files: {
      './dist/<%= pkg.name %>.js': ['./dist/*.coffee']        
    }
  }
}
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
  }

});