Galerie und tage
This commit is contained in:
168
node_modules/exec-buffer/index.js
generated
vendored
168
node_modules/exec-buffer/index.js
generated
vendored
@@ -1,45 +1,145 @@
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const execa = require('execa');
|
||||
const pFinally = require('p-finally');
|
||||
const pify = require('pify');
|
||||
const rimraf = require('rimraf');
|
||||
const tempfile = require('tempfile');
|
||||
|
||||
const fsP = pify(fs);
|
||||
const rmP = pify(rimraf);
|
||||
const input = Symbol('inputPath');
|
||||
const output = Symbol('outputPath');
|
||||
var execFile = require('child_process').execFile;
|
||||
var fs = require('fs');
|
||||
var rm = require('rimraf');
|
||||
var tempfile = require('tempfile');
|
||||
|
||||
module.exports = opts => {
|
||||
opts = Object.assign({}, opts);
|
||||
/**
|
||||
* Run a buffer through a child process
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
if (!Buffer.isBuffer(opts.input)) {
|
||||
return Promise.reject(new Error('Input is required'));
|
||||
function ExecBuffer() {
|
||||
if (!(this instanceof ExecBuffer)) {
|
||||
return new ExecBuffer();
|
||||
}
|
||||
|
||||
if (typeof opts.bin !== 'string') {
|
||||
return Promise.reject(new Error('Binary is required'));
|
||||
this.src(tempfile());
|
||||
this.dest(tempfile());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a child process to use
|
||||
*
|
||||
* @param {String} bin
|
||||
* @param {Array} args
|
||||
* @api public
|
||||
*/
|
||||
|
||||
ExecBuffer.prototype.use = function (bin, args) {
|
||||
if (!arguments.length) {
|
||||
return this._use;
|
||||
}
|
||||
|
||||
if (!Array.isArray(opts.args)) {
|
||||
return Promise.reject(new Error('Arguments are required'));
|
||||
}
|
||||
this._use = {
|
||||
args: args,
|
||||
bin: bin
|
||||
};
|
||||
|
||||
const inputPath = opts.inputPath || tempfile();
|
||||
const outputPath = opts.outputPath || tempfile();
|
||||
|
||||
opts.args = opts.args.map(x => x === input ? inputPath : x === output ? outputPath : x);
|
||||
|
||||
const promise = fsP.writeFile(inputPath, opts.input)
|
||||
.then(() => execa(opts.bin, opts.args))
|
||||
.then(() => fsP.readFile(outputPath));
|
||||
|
||||
return pFinally(promise, () => Promise.all([
|
||||
rmP(inputPath),
|
||||
rmP(outputPath)
|
||||
]));
|
||||
return this;
|
||||
};
|
||||
|
||||
module.exports.input = input;
|
||||
module.exports.output = output;
|
||||
/**
|
||||
* Get or set the temp source path
|
||||
*
|
||||
* @param {String} path
|
||||
* @api public
|
||||
*/
|
||||
|
||||
ExecBuffer.prototype.src = function (path) {
|
||||
if (!arguments.length) {
|
||||
return this._src;
|
||||
}
|
||||
|
||||
this._src = path;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get or set the temp destination path
|
||||
*
|
||||
* @param {String} path
|
||||
* @api public
|
||||
*/
|
||||
|
||||
ExecBuffer.prototype.dest = function (path) {
|
||||
if (!arguments.length) {
|
||||
return this._dest;
|
||||
}
|
||||
|
||||
this._dest = path;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Run buffer through the child process
|
||||
*
|
||||
* @param {Buffer} buf
|
||||
* @param {Function} cb
|
||||
* @api public
|
||||
*/
|
||||
|
||||
ExecBuffer.prototype.run = function (buf, cb) {
|
||||
fs.writeFile(this.src(), buf, function (err) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
return;
|
||||
}
|
||||
|
||||
execFile(this.use().bin, this.use().args, function (err, stdout, stderr) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
return;
|
||||
}
|
||||
|
||||
fs.readFile(this.dest(), function (err, data) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
return;
|
||||
}
|
||||
|
||||
this.cleanup(function (err) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
return;
|
||||
}
|
||||
|
||||
cb(null, data, stderr);
|
||||
});
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
/**
|
||||
* Cleanup temporary files
|
||||
*
|
||||
* @param {Function} cb
|
||||
* @api private
|
||||
*/
|
||||
|
||||
ExecBuffer.prototype.cleanup = function (cb) {
|
||||
rm(this.src(), function (err) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
return;
|
||||
}
|
||||
|
||||
rm(this.dest(), function (err) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
return;
|
||||
}
|
||||
|
||||
cb();
|
||||
});
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
/**
|
||||
* Module exports
|
||||
*/
|
||||
|
||||
module.exports = ExecBuffer;
|
||||
|
||||
Reference in New Issue
Block a user