Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 11, 2016
1 parent 147c5ab commit b0cefab
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
16 changes: 9 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ const stripIndent = require('strip-indent');

const cli = meow(`
Usage
$ imagemin <path|glob> ... --out-dir build
$ imagemin <path|glob> ... --out-dir=build [--plugin=<name> ...]
$ imagemin <file> > <output>
$ cat <file> | imagemin > <output>
$ imagemin [--plugin <plugin-name>...] ...
Options
-p, --plugin Override the default plugins
-o, --out-dir Output directory
Examples
$ imagemin images/* --out-dir build
$ imagemin images/* --out-dir=build
$ imagemin foo.png > foo-optimized.png
$ cat foo.png | imagemin > foo-optimized.png
$ imagemin --plugin pngquant foo.png > foo-optimized.png
$ imagemin --plugin=pngquant foo.png > foo-optimized.png
`, {
string: [
'plugin',
Expand Down Expand Up @@ -59,13 +58,16 @@ const requirePlugins = plugins => plugins.map(x => {
});

const run = (input, opts) => {
const plugins = opts.plugin ? arrify(opts.plugin) : DEFAULT_PLUGINS;
opts = Object.assign({plugin: DEFAULT_PLUGINS}, opts);

const use = requirePlugins(arrify(opts.plugin));

if (Buffer.isBuffer(input)) {
return imagemin.buffer(input, {use: requirePlugins(plugins)}).then(buf => process.stdout.write(buf));
imagemin.buffer(input, {use}).then(buf => process.stdout.write(buf));
return;
}

imagemin(input, opts.outDir, {use: requirePlugins(plugins)}).then(files => {
imagemin(input, opts.outDir, {use}).then(files => {
if (!opts.outDir) {
files.forEach(x => process.stdout.write(x.data));
}
Expand Down
7 changes: 3 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ $ npm install --global imagemin-cli
$ imagemin --help
Usage
$ imagemin <path|glob> ... --out-dir build
$ imagemin <path|glob> ... --out-dir=build [--plugin=<name> ...]
$ imagemin <file> > <output>
$ cat <file> | imagemin > <output>
$ imagemin [--plugin <plugin-name>...] ...
Options
-p, --plugin Override the default plugins
-o, --out-dir Output directory
Examples
$ imagemin images/* --out-dir build
$ imagemin images/* --out-dir=build
$ imagemin foo.png > foo-optimized.png
$ cat foo.png | imagemin > foo-optimized.png
$ imagemin --plugin pngquant foo.png > foo-optimized.png
$ imagemin --plugin=pngquant foo.png > foo-optimized.png
```


Expand Down
18 changes: 9 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import fs from 'fs';
import path from 'path';
import execa from 'execa';
import pify from 'pify';
import test from 'ava';

process.chdir(__dirname);

const fsP = pify(fs);

test('show help screen', async t => {
Expand All @@ -15,33 +16,32 @@ test('show version', async t => {
});

test('optimize a GIF', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixtures', 'test.gif'));
const buf = await fsP.readFile('fixtures/test.gif');
t.true((await execa.stdout('./cli.js', {input: buf})).length < buf.length);
});

test('optimize a JPG', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixtures', 'test.jpg'));
const buf = await fsP.readFile('fixtures/test.jpg');
t.true((await execa.stdout('./cli.js', {input: buf})).length < buf.length);
});

test('optimize a PNG', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixtures', 'test.png'));
const buf = await fsP.readFile('fixtures/test.png');
t.true((await execa.stdout('./cli.js', {input: buf})).length < buf.length);
});

test('optimize a SVG', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixtures', 'test.svg'));
const buf = await fsP.readFile('fixtures/test.svg');
t.true((await execa.stdout('./cli.js', {input: buf})).length < buf.length);
});

test('output error on corrupt images', async t => {
t.throws(execa('./cli.js', [path.join(__dirname, 'fixtures', 'test-corrupt.jpg')]));
t.throws(execa('./cli.js', ['fixtures/test-corrupt.jpg']));
});

test('support plugins', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixtures', 'test.png'));
const data = await execa.stdout('./cli.js', ['--plugin', 'pngquant'], {input: buf});
const buf = await fsP.readFile('fixtures/test.png');
const data = await execa.stdout('./cli.js', ['--plugin=pngquant'], {input: buf});
const compareData = await execa.stdout('./cli.js', {input: buf});

t.true(data.length < compareData.length);
});

0 comments on commit b0cefab

Please sign in to comment.