在Grunt任务中从节点脚本中检索输出 [英] Retrieving output from Node script in a Grunt task

查看:146
本文介绍了在Grunt任务中从节点脚本中检索输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Grunt相当陌生,所以这可能是一个相当基本的问题。我有一个看起来像这样的Gruntfile.js:

$ $ p $ / *全局模块:false * /
模块.exports = function(grunt){

grunt.initConfig({

});

grunt.registerTask('default','measureText');

grunt.registerTask('measureText','度量文本大小',function(){
grunt.log.writeln('============ ================================================== =='=');
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.log。 writeln('=============================================== =========================');
return;
});
};

正如您所看到的,它调用 node-imagemagick 方法标识以获取图像的宽度和高度(build / text.png)。当我运行上面的grunt脚本时,identify()回调没有输出。 (这是上面的console.log行)。然后,如果我创建一个节点脚本(例如test-node-imagemagick.js)来测试相同的代码,它可以工作很好,例如

 #!/ usr / bin / env节点

var im = require( 节点的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的目录。



谢谢!

解决方案

我做了一些更多的研究,首先,我将切换到imagemagick开发人员推荐的这个库。

rel =nofollow> https://github.com/aheckmann/gm



但是,如果您想要使用无人看管的服务,这取决于您

无论如何这里是:

pre $ g $ c $ grunt.registerTask 'measureText',function(done){
//省略代码

var done = this.async()//创建异步任务

im.identify '/ path / to / image',函数(err,output){
//做异步的东西
console.log('dimension:%s',输出)
done()
})

//或使用gm
gm('/ path / to / image')
.size(function(err,size){
console.log('gm size:%s',size)
done()
})
})


I'm fairly new to Grunt, so this might be quite a basic question. I've got a Gruntfile.js that looks like this:

/*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; 
  });    
}; 

As you can see, it's calling the node-imagemagick method identify to get the width and height of an image (build/text.png). When I run my grunt script above, there is no output from the identify() callback. (That's the console.log line above).

Yet if I create a Node script (e.g. test-node-imagemagick.js) to test that same code, it works just fine, e.g.

#!/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);
});

So how do I call Node packages from within Grunt tasks, and get returned values?

By the way, I did install the package by doing:

$ npm install node-imagemagick

... from the directory that contains the Gruntfile.js.

Thanks!

解决方案

Doing some more research I was able to get this to work. First off, I would switch to this library which was recommended by the imagemagick developer README.

https://github.com/aheckmann/gm

But, that is up to you if you want to use an unmaintained library.

Anyway here it is:

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()
   })
})

这篇关于在Grunt任务中从节点脚本中检索输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆