-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"reporter": ["lcov", "json"], | ||
"reports-dir": "coverage", | ||
"exclude": ["tmp", "coverage", "node_modules", ".github"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
yarn.lock | ||
coverage |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,86 @@ | ||
'use strict'; | ||
const execa = require('execa'); | ||
const isPng = require('is-png'); | ||
const isStream = require('is-stream'); | ||
const pngquant = require('pngquant-bin'); | ||
const ow = require('ow'); | ||
|
||
const imageminPngquant = (options = {}) => input => { | ||
const isBuffer = Buffer.isBuffer(input); | ||
import {execa} from 'execa'; | ||
import isPng from 'is-png'; | ||
import {isStream} from 'is-stream'; | ||
import pngquant from 'pngquant-bin'; | ||
import ow from 'ow'; | ||
|
||
export function imageminPngquant(options = {}) { | ||
return input => { | ||
const isBuffer = Buffer.isBuffer(input); | ||
Check failure on line 9 in index.js GitHub Actions / macos (16.x)
Check failure on line 9 in index.js GitHub Actions / linux (18.x)
Check failure on line 9 in index.js GitHub Actions / linux (20.x)
|
||
|
||
if (!isBuffer && !isStream(input)) { | ||
return Promise.reject(new TypeError(`Expected a Buffer or Stream, got ${typeof input}`)); | ||
} | ||
|
||
if (!isBuffer && !isStream(input)) { | ||
return Promise.reject(new TypeError(`Expected a Buffer or Stream, got ${typeof input}`)); | ||
} | ||
if (isBuffer && !isPng(input)) { | ||
return Promise.resolve(input); | ||
} | ||
|
||
if (isBuffer && !isPng(input)) { | ||
return Promise.resolve(input); | ||
} | ||
const args = ['-']; | ||
|
||
const args = ['-']; | ||
if (options.speed !== undefined) { | ||
ow(options.speed, ow.number.integer.inRange(1, 11)); | ||
args.push('--speed', options.speed); | ||
} | ||
|
||
if (typeof options.speed !== 'undefined') { | ||
ow(options.speed, ow.number.integer.inRange(1, 11)); | ||
args.push('--speed', options.speed); | ||
} | ||
if (options.strip !== undefined) { | ||
ow(options.strip, ow.boolean); | ||
|
||
if (typeof options.strip !== 'undefined') { | ||
ow(options.strip, ow.boolean); | ||
if (options.strip) { | ||
args.push('--strip'); | ||
} | ||
} | ||
|
||
if (options.strip) { | ||
args.push('--strip'); | ||
if (options.quality !== undefined) { | ||
ow(options.quality, ow.array.length(2).ofType(ow.number.inRange(0, 1))); | ||
const [min, max] = options.quality; | ||
args.push('--quality', `${Math.round(min * 100)}-${Math.round(max * 100)}`); | ||
} | ||
} | ||
|
||
if (typeof options.quality !== 'undefined') { | ||
ow(options.quality, ow.array.length(2).ofType(ow.number.inRange(0, 1))); | ||
const [min, max] = options.quality; | ||
args.push('--quality', `${Math.round(min * 100)}-${Math.round(max * 100)}`); | ||
} | ||
if (options.dithering !== undefined) { | ||
ow(options.dithering, ow.any(ow.number.inRange(0, 1), ow.boolean.false)); | ||
|
||
if (typeof options.dithering !== 'undefined') { | ||
ow(options.dithering, ow.any(ow.number.inRange(0, 1), ow.boolean.false)); | ||
if (typeof options.dithering === 'number') { | ||
args.push(`--floyd=${options.dithering}`); | ||
} else if (options.dithering === false) { | ||
args.push('--ordered'); | ||
} | ||
} | ||
|
||
if (typeof options.dithering === 'number') { | ||
args.push(`--floyd=${options.dithering}`); | ||
} else if (options.dithering === false) { | ||
args.push('--ordered'); | ||
if (options.posterize !== undefined) { | ||
ow(options.posterize, ow.number); | ||
args.push('--posterize', options.posterize); | ||
} | ||
|
||
if (options.verbose !== undefined) { | ||
ow(options.verbose, ow.boolean); | ||
args.push('--verbose'); | ||
} | ||
} | ||
|
||
if (typeof options.posterize !== 'undefined') { | ||
ow(options.posterize, ow.number); | ||
args.push('--posterize', options.posterize); | ||
} | ||
|
||
if (typeof options.verbose !== 'undefined') { | ||
ow(options.verbose, ow.boolean); | ||
args.push('--verbose'); | ||
} | ||
|
||
const subprocess = execa(pngquant, args, { | ||
encoding: null, | ||
maxBuffer: Infinity, | ||
input | ||
}); | ||
|
||
const promise = subprocess | ||
.then(result => result.stdout) // eslint-disable-line promise/prefer-await-to-then | ||
.catch(error => { | ||
// We use `error.exitCode` to check for a special condition when running the pngquant binary. | ||
// See details on handling of "99" code at https://pngquant.org (search for "status code 99"). | ||
if (error.exitCode === 99) { | ||
return input; | ||
} | ||
|
||
error.message = error.stderr || error.message; | ||
throw error; | ||
const subprocess = execa(pngquant, args, { | ||
encoding: null, | ||
maxBuffer: Number.POSITIVE_INFINITY, | ||
input, | ||
}); | ||
|
||
subprocess.stdout.then = promise.then.bind(promise); // eslint-disable-line promise/prefer-await-to-then | ||
subprocess.stdout.catch = promise.catch.bind(promise); | ||
const promise = subprocess | ||
.then(result => result.stdout) | ||
.catch(error => { | ||
// We use `error.exitCode` to check for a special condition when running the pngquant binary. | ||
// See details on handling of "99" code at https://pngquant.org (search for "status code 99"). | ||
if (error.exitCode === 99) { | ||
return input; | ||
} | ||
|
||
error.message = error.stderr || error.message; | ||
throw error; | ||
}); | ||
|
||
subprocess.stdout.then = promise.then.bind(promise); // eslint-disable-line unicorn/no-thenable | ||
subprocess.stdout.catch = promise.catch.bind(promise); | ||
|
||
return subprocess.stdout; | ||
}; | ||
return subprocess.stdout; | ||
}; | ||
} | ||
|
||
module.exports = imageminPngquant; | ||
module.exports.default = imageminPngquant; | ||
export {imageminPngquant as default}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as fs from 'node:fs'; | ||
import * as path from 'node:path'; | ||
import * as url from 'node:url'; | ||
import {expectType} from 'tsd'; | ||
import imageminPngquant from '.'; | ||
import pngquant from './index.js'; | ||
|
||
const buffer = fs.readFileSync(path.join(__dirname, 'fixture.png')); | ||
const thisDirname = url.fileURLToPath(new URL('.', import.meta.url)); | ||
const buffer = fs.readFileSync(path.join(thisDirname, 'fixture.png')); | ||
|
||
(async () => { | ||
expectType<Buffer>(await imageminPngquant()(buffer)); | ||
expectType<Buffer>(await imageminPngquant({ | ||
async function test() { | ||
expectType<Buffer>(await pngquant()(buffer)); | ||
Check failure on line 11 in index.test-d.ts GitHub Actions / macos (16.x)
Check failure on line 11 in index.test-d.ts GitHub Actions / linux (18.x)
Check failure on line 11 in index.test-d.ts GitHub Actions / linux (20.x)
|
||
expectType<Buffer>(await pngquant({ | ||
Check failure on line 12 in index.test-d.ts GitHub Actions / macos (16.x)
Check failure on line 12 in index.test-d.ts GitHub Actions / linux (18.x)
Check failure on line 12 in index.test-d.ts GitHub Actions / linux (20.x)
|
||
speed: 10, | ||
quality: [0.8, 1] | ||
quality: [0.8, 1], | ||
})(buffer)); | ||
})(); | ||
} | ||
|
||
await test(); |