2 const fs = require('fs');
3 const execa = require('execa');
4 const pFinally = require('p-finally');
5 const pify = require('pify');
6 const rimraf = require('rimraf');
7 const tempfile = require('tempfile');
10 const rmP = pify(rimraf);
11 const input = Symbol('inputPath');
12 const output = Symbol('outputPath');
14 module.exports = opts => {
15 opts = Object.assign({}, opts);
17 if (!Buffer.isBuffer(opts.input)) {
18 return Promise.reject(new Error('Input is required'));
21 if (typeof opts.bin !== 'string') {
22 return Promise.reject(new Error('Binary is required'));
25 if (!Array.isArray(opts.args)) {
26 return Promise.reject(new Error('Arguments are required'));
29 const inputPath = tempfile();
30 const outputPath = tempfile();
32 opts.args = opts.args.map(x => x === input ? inputPath : x === output ? outputPath : x);
34 const promise = fsP.writeFile(inputPath, opts.input)
35 .then(() => execa(opts.bin, opts.args))
36 .then(() => fsP.readFile(outputPath));
38 return pFinally(promise, () => Promise.all([
44 module.exports.input = input;
45 module.exports.output = output;