簡體   English   中英

使用自定義json配置的grunt任務

[英]grunt task with custom json config

我想創建某種任務,我可以從不同的json文件中讀取自定義配置,並用我的咖啡源文件中的內容替換json文件的內容,並連接源文件。

我的projekt設置:

  • ./src

    • file1.coffee
    • file2.coffee
  • ./config中

    • /資料夾

      • development.json(包含:{“key”:“value1”}
      • production.json(包含:{“key”:“value2”}
    • /資料夾

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

    • package-name.coffee
    • 包name.js

file1.coffee包含

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

我有自己配置和工作的grunt concat任務:

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

我有grunt-replace任務(當我在已經連接的文件上運行“grunt replace”時,替換版本等已經工作了)

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']        
    }
  }
}

所有任務都適合自己,但我需要從config-json文件中讀取內容替換為連接的咖啡文件,然后將所有文件編譯為js。

我試過這樣的事,但感覺不對:

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是一個選項嗎? 但如何從config.json文件中動態讀取?

感謝您的幫助!

我找到了一個可以幫助你實現這個目標的插件 - https://github.com/outaTiME/grunt-replace

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM