How can I perform an asynchronous operation before grunt.initConfig()?

浪子不回头ぞ 提交于 2019-12-08 16:13:01

问题


Now I have my Gruntfile setup to perform some automatic detection magic like parsing sourcefiles to parse some PHP sources in roder to dynamically figure out filenames and paths I need to know before running grunt.initConfig().

Unfortunately grunt.initConfig() doesn't seem to be meant to be run asynchronously, so I see no way to have my asynchronous code executed before I can call it. Is there a trick to accomplish this or do I have to rewrite my detection routines synchronously? Is there any easy way to block execution before my callback has arrived?

Inside grunt tasks there is of course this.async(), but for initConfig() that doesn't work.

Here's a stripped down example:

function findSomeFilesAndPaths(callback) {
  // async tasks that detect and parse
  // and execute callback(results) when done
}

module.exports = function (grunt) {
  var config = {
    pkg: grunt.file.readJSON('package.json'),
  }

  findSomeFilesAndPaths(function (results) {
    config.watch = {
      coffee: {
        files: results.coffeeDir + "**/*.coffee",
        tasks: ["coffee"]
         // ...
      }
    };

    grunt.initConfig(config);

    grunt.loadNpmTasks "grunt-contrib-coffee"
    // grunt.loadNpmTasks(...);
  });
};

Any good ideas how to get this done?

Thanks a lot!


回答1:


I would do it as a task since Grunt is sync or if you can make findSomeFilesAndPaths sync.

grunt.initConfig({
  initData: {},
  watch: {
    coffee: {
      files: ['<%= initData.coffeeDir %>/**/*.coffee'],
      tasks: ['coffee'],
    },
  },
});

grunt.registerTask('init', function() {
  var done = this.async();
  findSomeFilesAndPaths(function(results) {
    // Set our initData in our config
    grunt.config(['initData'], results);
    done();
  });
});

// This is optional but if you want it to
// always run the init task first do this
grunt.renameTask('watch', 'actualWatch');
grunt.registerTask('watch', ['init', 'actualWatch']);



回答2:


Solved by rewriting, synchronous style. ShellJS came in handy, especially for synchronously executing shell commands.




回答3:


Example of how you could use ShellJS in Grunt:

grunt.initConfig({
    paths: {
        bootstrap: exec('bundle show bootstrap-sass').output.replace(/(\r\n|\n|\r)/gm, '')
    },
    uglify: {
        vendor: {
            files: { 'vendor.js': ['<%= paths.bootstrap %>/vendor/assets/javascripts/bootstrap/alert.js']
        }
    }
});


来源:https://stackoverflow.com/questions/16547528/how-can-i-perform-an-asynchronous-operation-before-grunt-initconfig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!