bbnn bbnn - 2 months ago 6
Javascript Question

grunt.js console.log in callback doesn't output anything

What is wrong with my codes, the console.logs that is inside a call back of file related API's doesn't show anything in 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);
});
});
});
};

Answer

The exec() method is async so you need to tell grunt that and execute the async callback when it's done. It's in the docs: Why doesn't my asynchronous task complete?