diff --git a/cli.js b/cli.js index 4d50e38..0672ce4 100755 --- a/cli.js +++ b/cli.js @@ -4,6 +4,7 @@ const fs = require('fs'); const meow = require('meow'); const prettyBytes = require('pretty-bytes'); const gzipSize = require('gzip-size'); +const chalk = require('chalk'); const cli = meow(` Usage @@ -11,14 +12,17 @@ const cli = meow(` $ cat | gzip-size Options - --level Compression level [0-9] (Default: 9) - --raw Display value in bytes + --level Compression level [0-9] (Default: 9) + --raw Display value in bytes + --include-original Include original size Examples $ gzip-size unicorn.png 192 kB $ gzip-size unicorn.png --raw 192256 + $ gzip-size unicorn.png --include-original + 392 kB → 192 kB `, { flags: { level: { @@ -26,6 +30,9 @@ const cli = meow(` }, raw: { type: 'boolean' + }, + includeOriginal: { + type: 'boolean' } } }); @@ -45,5 +52,11 @@ if (cli.flags.level) { } source.pipe(gzipSize.stream(options)).on('gzip-size', size => { - console.log(cli.flags.raw ? size : prettyBytes(size)); + let output = cli.flags.raw ? size : prettyBytes(size); + if (cli.flags.includeOriginal) { + const {size: originalSize} = fs.statSync(input); + output = (cli.flags.raw ? originalSize : prettyBytes(originalSize)) + chalk.dim(' → ') + output; + } + + console.log(output); }); diff --git a/package.json b/package.json index 7865db9..8e0e4cf 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "stdin" ], "dependencies": { + "chalk": "^4.1.1", "gzip-size": "^6.0.0", "meow": "^9.0.0", "pretty-bytes": "^5.6.0" diff --git a/readme.md b/readme.md index ac47f52..0877b88 100644 --- a/readme.md +++ b/readme.md @@ -18,14 +18,17 @@ $ gzip-size --help $ cat | gzip-size Options - --level Compression level [0-9] (Default: 9) - --raw Display value in bytes + --level Compression level [0-9] (Default: 9) + --raw Display value in bytes + --include-original Include original size Examples $ gzip-size unicorn.png 192 kB $ gzip-size unicorn.png --raw 192256 + $ gzip-size unicorn.png --include-original + 392 kB → 192 kB ``` ## Related diff --git a/test.js b/test.js index f04f9be..c06bb00 100644 --- a/test.js +++ b/test.js @@ -22,3 +22,9 @@ test('stdin', async t => { }); t.is(Number.parseInt(stdout, 10), gzipSize.sync(fixture)); }); + +test('include original', async t => { + const {stdout} = await execa('./cli.js', ['test.js', '--raw', '--include-original']); + const {size} = fs.statSync('test.js'); + t.is(Number.parseInt(stdout, 10), size); +});