diff --git a/index.js b/index.js index 7ac74fb..75184f5 100644 --- a/index.js +++ b/index.js @@ -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), diff --git a/package.json b/package.json index 2c3b6d9..39c42d2 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "url": "https://github.com/kevva" }, "engines": { - "node": ">=4" + "node": ">=7.6.0" }, "scripts": { "test": "xo && ava" @@ -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" + ] } } diff --git a/test.js b/test.js index 67c0b92..d445246 100644 --- a/test.js +++ b/test.js @@ -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; @@ -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])); });