Skip to content

Commit

Permalink
Improve CLI output by adding number of minified files and spinner (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva authored and sindresorhus committed May 15, 2016
1 parent b0cefab commit efb9b39
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 22 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const arrify = require('arrify');
const meow = require('meow');
const getStdin = require('get-stdin');
const imagemin = require('imagemin');
const ora = require('ora');
const plur = require('plur');
const stripIndent = require('strip-indent');

const cli = meow(`
Expand Down Expand Up @@ -61,17 +63,32 @@ const run = (input, opts) => {
opts = Object.assign({plugin: DEFAULT_PLUGINS}, opts);

const use = requirePlugins(arrify(opts.plugin));
const spinner = ora('Minifying images');

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

imagemin(input, opts.outDir, {use}).then(files => {
if (!opts.outDir) {
files.forEach(x => process.stdout.write(x.data));
}
});
if (opts.outDir) {
spinner.start();
}

imagemin(input, opts.outDir, {use})
.then(files => {
if (!opts.outDir) {
files.forEach(x => process.stdout.write(x.data));
return;
}

spinner.stop();

console.log(`${files.length} ${plur('image', files.length)} minified`);
})
.catch(err => {
spinner.stop();
throw err;
});
};

if (!cli.input.length && process.stdin.isTTY) {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"get-stdin": "^5.0.1",
"imagemin": "^5.0.0",
"meow": "^3.6.0",
"ora": "^0.2.1",
"plur": "^2.1.2",
"strip-indent": "^2.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit efb9b39

Please sign in to comment.