【问题标题】:Grunt Task Will Not Execute Modules From Node Code Referenced With RequireGrunt 任务不会从 Require 引用的节点代码中执行模块
【发布时间】:2014-07-03 23:05:41
【问题描述】:

我有以下繁重的任务:

    module.exports = function(grunt) {
    var info = 'Syncs, updates English translations and downloads Chinese translations.';    

    grunt.registerTask('translations', info, function() {
        require('../../node/poeditor');
    });
};

我希望 require 执行该节点模块中的代码,其布局如下:

var querystring = require('querystring'),
    fs = require('fs'),
    https = require('https');

// Sync Terms
console.log('Syncing Terms...');
var requestConfig = {
    options: {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': 0
        },
        host: 'poeditor.com',
        method: 'POST',
        path: '/api/',
        port: '443'
    },
    data: {
        api_token: API_KEY,
        action: 'sync_terms',
        id: PROJECT_ID,
        data: ''
    }
};

fs.readFile('public/js/languages/en.json', 'utf8', function(error, data) { console.log(data);
});

问题是上面引用的模块根本没有被执行,我错过了什么?在 Grunt 中或者当需要一个需要其他模块来执行代码的模块时,我是否需要做一些特别的事情?

【问题讨论】:

    标签: javascript node.js gruntjs


    【解决方案1】:

    我想通了。我正在执行异步操作,所以我改为这样调用我的任务:

    grunt.registerTask('translations', info, function() {
            require('../../node/poeditor')(this);
        });
    

    通过模块传递该范围:

    module.exports = function(grunt) {...
    

    然后像这样分配:

    var done = grunt.async();
    

    在我上次这样调用的异步回调中:

    done();
    

    所以基本上我必须遵循这个: http://gruntjs.com/creating-tasks#why-doesn-t-my-asynchronous-task-complete

    【讨论】:

      猜你喜欢
      • 2015-07-25
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 2016-03-31
      • 2013-02-20
      • 2014-11-06
      • 1970-01-01
      相关资源
      最近更新 更多