正在Grunt任务中检索Node脚本的输出



我是Grunt的新手,所以这可能是一个非常基本的问题。我有一个Gruntfile.js,看起来像这样:

/*global module:false*/ 
module.exports = function (grunt) { 
  grunt.initConfig({ 
  }); 
  grunt.registerTask('default', 'measureText'); 
  grunt.registerTask('measureText', 'Measures size of text', function() { 
    grunt.log.writeln('========================================================================'); 
    grunt.log.writeln('= output of ImageMagick should be on next line:                        ='); 
    var im = require("node-imagemagick"); 
    im.identify(['-format', '%wx%h', 'build/text.png'], function(err, output){ 
      if (err) throw err; 
      console.log('dimension: '+output);  // <-- NOTHING WRITTEN!
    }); 
    grunt.log.writeln('========================================================================'); 
    return; 
  });    
}; 

正如你所看到的,它调用节点imagemagik方法identify来获取图像的宽度和高度(build/text.png)。当我运行上面的grunt脚本时,identify()回调没有输出。(这是上面的console.log行)。

然而,如果我创建一个Node脚本(例如testnodeimagemagick.js)来测试相同的代码,它运行得很好,例如

#!/usr/bin/env node
var im = require("node-imagemagick");
im.identify(['-format', '%wx%h', 'build/text.png'], function(err, output){
  if (err) throw err;
  console.log('dimension: '+output);
});

那么,我如何从Grunt任务中调用Node包,并获得返回值呢?

顺便说一句,我确实通过以下操作安装了软件包:

$ npm install node-imagemagick

从包含Gruntfile.js.的目录

谢谢!

编辑:

你的任务永远不会完成的原因是你在最后的模糊返回:

return; <---

您正在中断对的异步调用

im.identify(function(){
  //do async
})
return; //exit task

试试这个:

grunt.registerTask('measureText', function(done) {
    //omitting code
    im.identify(function(){
       //do async stuff
       done()
    })
})

函数参数done()告诉任务运行者,它应该在您执行异步操作之后完成,而不是在任何代码执行之前结束。

原件:

您的需求在每个脚本中看起来都非常不同。

require('imagemagick')
--- vs ---
require('node-imagemagick')

Grunt可能正在吞下错误

此外,我认为它只是:

npm install imagemagick

我刚刚通过更多的挖掘找到了答案,这里是:在Grunt Task中使用节点模块会使失败

基本上,如果要接收回调,我的Grunt任务必须是异步的:

/*global module:false*/ 
module.exports = function (grunt) { 
  var im = require("node-imagemagick"); 

  grunt.initConfig({ 
  }); 
  grunt.registerTask('default', 'measureText'); 
  grunt.registerTask('measureText', 'Measures size of text', function() { 
    var done = this.async(); // <-- Make the task asynchronous!
    grunt.log.writeln('========================================================================'); 
    grunt.log.writeln('= output of ImageMagick should be on next line:                        ='); 
    im.identify(['-format', '%wx%h', 'build/text.png'], function(err, output){ 
      if (err) throw err; 
      console.log('dimension: '+output); 
      done();
    });  
  });    
}; 

当然,这意味着如果我保留第二行,输出实际上不会出现在等号的两行之间。

做了更多的研究,我能够让它发挥作用。首先,我会切换到这个库,它是imagemagik开发人员README推荐的。

https://github.com/aheckmann/gm

但是,如果你想使用一个未维护的库,那就取决于你了。

不管怎样,它在这里:

grunt.registerTask('measureText', function(done) {
  //omitting code
  var done = this.async() //create async task
  im.identify('/path/to/image', function(err, output){
   //do async stuff
   console.log('dimension: %s', output)
   done()
  })
  //or use gm
  gm('/path/to/image')
   .size(function(err, size) {
      console.log('gm size: %s', size)
      done()
   })
})

最新更新