UNPKG

cwebp

Version:

node.js wrapper for cwebp and dwebp binaries from WebP image processing utility

92 lines (81 loc) 2.25 kB
// Generated by CoffeeScript 1.12.7 var Wrapper, defer, mixin, spawn; spawn = require('child_process').spawn; mixin = require('./utils').mixin; defer = function() { var res; res = {}; res.promise = new Promise(function(resolve, reject) { res.resolve = resolve; return res.reject = reject; }); return res; }; module.exports = Wrapper = (function() { mixin(Wrapper, require('./args')); mixin(Wrapper, require('./io')); function Wrapper(source, bin) { this._args = { _: [] }; this._opts = {}; this.source = source; this.bin = bin || this.constructor.bin; } Wrapper.prototype.spawnOptions = function(options) { var key, value; for (key in options) { value = options[key]; this._opts[key] = value; } return this; }; Wrapper.prototype._spawn = function(args, stdin, stdout) { var onClose, onErrData, onError, proc, promise, ref, reject, res, resolve, stderr; if (stdin == null) { stdin = false; } if (stdout == null) { stdout = false; } ref = defer(), resolve = ref.resolve, reject = ref.reject, promise = ref.promise; stderr = ''; this._opts.stdio = [stdin ? 'pipe' : 'ignore', stdout ? 'pipe' : 'ignore', 'pipe']; proc = spawn(this.bin, args, this._opts); proc.once('error', onError = function(err) { if ((err.code !== 'OK' || 'OK' !== err.errno)) { return reject(err); } }); 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', onErrData = function(data) { return stderr += data; }); promise = promise["finally"](function() { proc.removeListener('error', onError); proc.removeListener('close', onClose); return proc.stderr.removeListener('data', onErrData); }); res = { promise: promise }; if (stdin) { res.stdin = proc.stdin; } if (stdout) { res.stdout = proc.stdout; } return res; }; return Wrapper; })();