Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,39 @@ const rmP = pify(rimraf);
const input = Symbol('inputPath');
const output = Symbol('outputPath');

module.exports = opts => {
opts = Object.assign({}, opts);
module.exports = options => {
options = Object.assign({}, options);

if (!Buffer.isBuffer(opts.input)) {
if (!Buffer.isBuffer(options.input)) {
return Promise.reject(new Error('Input is required'));
}

if (typeof opts.bin !== 'string') {
if (typeof options.bin !== 'string') {
return Promise.reject(new Error('Binary is required'));
}

if (!Array.isArray(opts.args)) {
if (!Array.isArray(options.args)) {
return Promise.reject(new Error('Arguments are required'));
}

const inputPath = opts.inputPath || tempfile();
const outputPath = opts.outputPath || tempfile();
const inputPath = options.inputPath || tempfile();
const outputPath = options.outputPath || tempfile();

opts.args = opts.args.map(x => x === input ? inputPath : x === output ? outputPath : x);
options.args = options.args.map(x => {
if (x === input) {
return inputPath;
}

const promise = fsP.writeFile(inputPath, opts.input)
.then(() => execa(opts.bin, opts.args))
.then(() => fsP.readFile(outputPath));
if (x === output) {
return outputPath;
}

return x;
});

const promise = fsP.writeFile(inputPath, options.input)
.then(() => execa(options.bin, options.args)) // eslint-disable-line promise/prefer-await-to-then
.then(() => fsP.readFile(outputPath)); // eslint-disable-line promise/prefer-await-to-then

return pFinally(promise, () => Promise.all([
rmP(inputPath),
Expand Down
28 changes: 17 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "https://github.com/kevva"
},
"engines": {
"node": ">=4"
"node": ">=7.6.0"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -23,17 +23,23 @@
"exec"
],
"dependencies": {
"execa": "^0.7.0",
"p-finally": "^1.0.0",
"pify": "^3.0.0",
"rimraf": "^2.5.4",
"tempfile": "^2.0.0"
"execa": "^5.0.0",
"p-finally": "^2.0.1",
"pify": "^5.0.0",
"rimraf": "^3.0.2",
"tempfile": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"gifsicle": "^3.0.4",
"is-gif": "^1.0.0",
"path-exists": "^3.0.0",
"xo": "*"
"ava": "^3.15.0",
"esm": "^3.2.25",
"gifsicle": "^5.1.0",
"is-gif": "^3.0.0",
"path-exists": "^4.0.0",
"xo": "^0.37.1"
},
"ava": {
"require": [
"esm"
]
}
}
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import isGif from 'is-gif';
import pathExists from 'path-exists';
import pify from 'pify';
import test from 'ava';
import m from './';
import m from './index.js';

test('set temporary directories', t => {
const {input, output} = m;
Expand All @@ -27,13 +27,13 @@ test('return a optimized buffer', async t => {

test('remove temporary files', async t => {
const buf = await pify(fs.readFile)(path.join(__dirname, 'fixture.gif'));
const err = await t.throws(m({
const error = await t.throwsAsync(m({
input: buf,
bin: 'foobarunicorn',
args: [m.output, m.input]
}));

t.is(err.code, 'ENOENT');
t.false(await pathExists(err.spawnargs[0]));
t.false(await pathExists(err.spawnargs[1]));
t.is(error.code, 'ENOENT');
t.false(await pathExists(error.spawnargs[0]));
t.false(await pathExists(error.spawnargs[1]));
});