cwebp
Version:
node.js wrapper for cwebp and dwebp binaries from WebP image processing utility
72 lines (62 loc) • 1.75 kB
JavaScript
// Generated by CoffeeScript 1.9.1
var When, Wrapper, mixin, spawn;
When = require('when');
spawn = require('child_process').spawn;
mixin = require('./utils').mixin;
module.exports = Wrapper = (function() {
mixin(Wrapper, require('./args'));
mixin(Wrapper, require('./io'));
function Wrapper(source, bin) {
this._args = {
_: []
};
this.source = source;
this.bin = bin || this.constructor.bin;
}
Wrapper.prototype._spawn = function(args, stdin, stdout) {
var onClose, onErr, proc, promise, ref, reject, res, resolve, stderr, stdio;
if (stdin == null) {
stdin = false;
}
if (stdout == null) {
stdout = false;
}
ref = When.defer(), resolve = ref.resolve, reject = ref.reject, promise = ref.promise;
stderr = '';
stdio = [stdin ? 'pipe' : 'ignore', stdout ? 'pipe' : 'ignore', 'pipe'];
proc = spawn(this.bin, args, {
stdio: stdio
});
proc.once('error', reject);
proc.once('close', onClose = function(code, signal) {
var err;
if (code !== 0 || signal !== null) {
err = new Error("Command failed: " + stderr);
err.code = code;
err.signal = signal;
return reject(err);
} else {
return resolve();
}
});
proc.stderr.on('data', onErr = function(data) {
return stderr += data;
});
promise = promise.ensure(function() {
proc.removeListener('error', reject);
proc.removeListener('close', onClose);
return proc.stderr.removeListener('close', onErr);
});
res = {
promise: promise
};
if (stdin) {
res.stdin = proc.stdin;
}
if (stdout) {
res.stdout = proc.stdout;
}
return res;
};
return Wrapper;
})();