回调中的grunt.js console.log不输出任何内容

时间:2012-09-04 04:13:03

标签: javascript node.js gruntjs

我的代码出了什么问题,在文件相关API的回调中的console.logs在CLI中没有显示任何内容。

module.exports = function (grunt) {

    var path = require("path"),
        fs = require("fs"),
        gm = require("gm");
    grunt.registerMultiTask("imagesizeslist", "Generate image sizes list", function () {
        var files = grunt.file.expandFiles(this.file.src);
        grunt.helper('imagesizeslist', files);
    });

    //  grunt.log.write(contents);


    grunt.registerHelper('imagesizeslist', function (files) {
        files.forEach(function (file) {
            basename = path.resolve(file);
            var exec = require('child_process').exec,
            child;
            //using sips
            console.log('sips "'+basename+'" -g pixelHeight');
            child = exec('sips "'+basename+'" -g pixelHeight',
              function (error, stdout, stderr) {
                  console.log(error);
                console.log('stdout: ' + stdout);
            });
            // using gm
            gm(basename).size(function (err, size) {
                if (!err)
                    console.log(size.width > size.height ? 'wider' : 'taller than you');
                else
                    console.log("test");
                console.log(size);
            });
        });
    });
};

1 个答案:

答案 0 :(得分:1)

exec()方法是异步的,所以你需要告诉grunt并在完成后执行异步回调。它位于文档中:Why doesn't my asynchronous task complete?