我对 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;
});
};
如您所见,它调用node-imagemagick方法 identify 来获取图像的宽度和高度(build/text.png)。当我在上面运行我的 grunt 脚本时, identify() 回调没有输出。(这是上面的 console.log 行)。
然而,如果我创建一个 Node 脚本(例如 test-node-imagemagick.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 的目录。
谢谢!