├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── cli.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | - 14 15 | - 12 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v2 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | import process from 'node:process'; 3 | import fs from 'node:fs'; 4 | import meow from 'meow'; 5 | import prettyBytes from 'pretty-bytes'; 6 | import {gzipSizeSync} from 'gzip-size'; 7 | import chalk from 'chalk'; 8 | import getStdin from 'get-stdin'; 9 | 10 | const cli = meow(` 11 | Usage 12 | $ gzip-size 13 | $ cat | gzip-size 14 | 15 | Options 16 | --level Compression level [0-9] (Default: 9) 17 | --raw Display value in bytes 18 | --include-original Include original size 19 | 20 | Examples 21 | $ gzip-size unicorn.png 22 | 192 kB 23 | $ gzip-size unicorn.png --raw 24 | 192256 25 | $ gzip-size unicorn.png --include-original 26 | 392 kB → 192 kB 27 | `, { 28 | importMeta: import.meta, 29 | flags: { 30 | level: { 31 | type: 'number', 32 | }, 33 | raw: { 34 | type: 'boolean', 35 | }, 36 | includeOriginal: { 37 | type: 'boolean', 38 | }, 39 | }, 40 | }); 41 | 42 | const [input] = cli.input; 43 | 44 | if (!input && process.stdin.isTTY) { 45 | console.error('Specify a file path'); 46 | process.exit(1); 47 | } 48 | 49 | const options = {}; 50 | if (cli.flags.level) { 51 | options.level = cli.flags.level; 52 | } 53 | 54 | function output(data) { 55 | const originalSize = data.length; 56 | const gzippedSize = gzipSizeSync(data); 57 | 58 | let output = cli.flags.raw ? gzippedSize : prettyBytes(gzippedSize); 59 | if (cli.flags.includeOriginal) { 60 | output = (cli.flags.raw ? originalSize : prettyBytes(originalSize)) + chalk.dim(' → ') + output; 61 | } 62 | 63 | console.log(output); 64 | } 65 | 66 | (async () => { 67 | if (input) { 68 | output(fs.readFileSync(input)); 69 | } else { 70 | output(await getStdin.buffer()); 71 | } 72 | })(); 73 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gzip-size-cli", 3 | "version": "5.1.0", 4 | "description": "Get the gzipped size of a file or stdin", 5 | "license": "MIT", 6 | "repository": "sindresorhus/gzip-size-cli", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "bin": { 15 | "gzip-size": "./cli.js" 16 | }, 17 | "engines": { 18 | "node": ">=12" 19 | }, 20 | "scripts": { 21 | "test": "xo && ava" 22 | }, 23 | "files": [ 24 | "cli.js" 25 | ], 26 | "keywords": [ 27 | "cli-app", 28 | "cli", 29 | "zlib", 30 | "gzip", 31 | "compressed", 32 | "size", 33 | "file", 34 | "stdin" 35 | ], 36 | "dependencies": { 37 | "chalk": "^4.1.2", 38 | "get-stdin": "^9.0.0", 39 | "gzip-size": "^7.0.0", 40 | "meow": "^10.1.2", 41 | "pretty-bytes": "^5.6.0" 42 | }, 43 | "devDependencies": { 44 | "ava": "^3.15.0", 45 | "execa": "^6.0.0", 46 | "xo": "^0.46.4" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # gzip-size-cli 2 | 3 | > Get the gzipped size of a file or stdin 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install --global gzip-size-cli 9 | ``` 10 | 11 | ## Usage 12 | 13 | ``` 14 | $ gzip-size --help 15 | 16 | Usage 17 | $ gzip-size 18 | $ cat | gzip-size 19 | 20 | Options 21 | --level Compression level [0-9] (Default: 9) 22 | --raw Display value in bytes 23 | --include-original Include original size 24 | 25 | Examples 26 | $ gzip-size unicorn.png 27 | 192 kB 28 | $ gzip-size unicorn.png --raw 29 | 192256 30 | $ gzip-size unicorn.png --include-original 31 | 392 kB → 192 kB 32 | ``` 33 | 34 | ## Related 35 | 36 | - [gzip-size](https://github.com/sindresorhus/gzip-size) - API for this module 37 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | import {execa} from 'execa'; 3 | import test from 'ava'; 4 | import {gzipSizeSync} from 'gzip-size'; 5 | 6 | const fixture = fs.readFileSync('test.js', 'utf8'); 7 | 8 | test('main', async t => { 9 | const {stdout} = await execa('./cli.js', ['test.js']); 10 | t.regex(stdout, /^\d+ B$/); 11 | }); 12 | 13 | test('file', async t => { 14 | const {stdout} = await execa('./cli.js', ['test.js', '--raw']); 15 | t.is(Number.parseInt(stdout, 10), gzipSizeSync(fixture)); 16 | }); 17 | 18 | test('stdin', async t => { 19 | const {stdout} = await execa('./cli.js', ['test.js', '--raw'], { 20 | input: fs.createReadStream('test.js'), 21 | }); 22 | t.is(Number.parseInt(stdout, 10), gzipSizeSync(fixture)); 23 | }); 24 | 25 | test('include original', async t => { 26 | const {stdout} = await execa('./cli.js', ['test.js', '--raw', '--include-original']); 27 | const {size} = fs.statSync('test.js'); 28 | t.is(Number.parseInt(stdout, 10), size); 29 | }); 30 | 31 | test('include original - stdin', async t => { 32 | const {stdout} = await execa('./cli.js', ['--raw', '--include-original'], { 33 | input: fs.createReadStream('test.js'), 34 | }); 35 | const {size} = fs.statSync('test.js'); 36 | t.is(Number.parseInt(stdout, 10), size); 37 | }); 38 | --------------------------------------------------------------------------------