Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --include-original flag #5

Merged
merged 6 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@ 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
$ gzip-size <file>
$ cat <file> | 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 Display original size
vltansky marked this conversation as resolved.
Show resolved Hide resolved

Examples
$ gzip-size unicorn.png
192 kB
$ gzip-size unicorn.png --raw
192256
$ gzip-size unicorn.png --include-original
1920 kB → 192 kB
vltansky marked this conversation as resolved.
Show resolved Hide resolved
`, {
flags: {
level: {
type: 'number'
},
raw: {
type: 'boolean'
},
includeOriginal: {
type: 'boolean'
}
}
});
Expand All @@ -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);
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"stdin"
],
"dependencies": {
"chalk": "^4.1.1",
"gzip-size": "^6.0.0",
"meow": "^9.0.0",
"pretty-bytes": "^5.6.0"
Expand Down
7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ $ gzip-size --help
$ cat <file> | 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 Display original size

Examples
$ gzip-size unicorn.png
192 kB
$ gzip-size unicorn.png --raw
192256
$ gzip-size unicorn.png --include-original
1920 kB → 192 kB
```

## Related
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});