Repository: sindresorhus/gzip-size-cli Branch: main Commit: 73c47861635f Files: 10 Total size: 5.7 KB Directory structure: gitextract_l8aaff6o/ ├── .editorconfig ├── .gitattributes ├── .github/ │ └── workflows/ │ └── main.yml ├── .gitignore ├── .npmrc ├── cli.js ├── license ├── package.json ├── readme.md └── test.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 ================================================ FILE: .gitattributes ================================================ * text=auto eol=lf ================================================ FILE: .github/workflows/main.yml ================================================ name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 16 - 14 - 12 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test ================================================ FILE: .gitignore ================================================ node_modules yarn.lock ================================================ FILE: .npmrc ================================================ package-lock=false ================================================ FILE: cli.js ================================================ #!/usr/bin/env node import process from 'node:process'; import fs from 'node:fs'; import meow from 'meow'; import prettyBytes from 'pretty-bytes'; import {gzipSizeSync} from 'gzip-size'; import chalk from 'chalk'; import getStdin from 'get-stdin'; const cli = meow(` Usage $ gzip-size $ cat | gzip-size Options --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 `, { importMeta: import.meta, flags: { level: { type: 'number', }, raw: { type: 'boolean', }, includeOriginal: { type: 'boolean', }, }, }); const [input] = cli.input; if (!input && process.stdin.isTTY) { console.error('Specify a file path'); process.exit(1); } const options = {}; if (cli.flags.level) { options.level = cli.flags.level; } function output(data) { const originalSize = data.length; const gzippedSize = gzipSizeSync(data); let output = cli.flags.raw ? gzippedSize : prettyBytes(gzippedSize); if (cli.flags.includeOriginal) { output = (cli.flags.raw ? originalSize : prettyBytes(originalSize)) + chalk.dim(' → ') + output; } console.log(output); } (async () => { if (input) { output(fs.readFileSync(input)); } else { output(await getStdin.buffer()); } })(); ================================================ FILE: license ================================================ MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. ================================================ FILE: package.json ================================================ { "name": "gzip-size-cli", "version": "5.1.0", "description": "Get the gzipped size of a file or stdin", "license": "MIT", "repository": "sindresorhus/gzip-size-cli", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "bin": { "gzip-size": "./cli.js" }, "engines": { "node": ">=12" }, "scripts": { "test": "xo && ava" }, "files": [ "cli.js" ], "keywords": [ "cli-app", "cli", "zlib", "gzip", "compressed", "size", "file", "stdin" ], "dependencies": { "chalk": "^4.1.2", "get-stdin": "^9.0.0", "gzip-size": "^7.0.0", "meow": "^10.1.2", "pretty-bytes": "^5.6.0" }, "devDependencies": { "ava": "^3.15.0", "execa": "^6.0.0", "xo": "^0.46.4" } } ================================================ FILE: readme.md ================================================ # gzip-size-cli > Get the gzipped size of a file or stdin ## Install ```sh npm install --global gzip-size-cli ``` ## Usage ``` $ gzip-size --help Usage $ gzip-size $ cat | gzip-size Options --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 - [gzip-size](https://github.com/sindresorhus/gzip-size) - API for this module ================================================ FILE: test.js ================================================ import fs from 'node:fs'; import {execa} from 'execa'; import test from 'ava'; import {gzipSizeSync} from 'gzip-size'; const fixture = fs.readFileSync('test.js', 'utf8'); test('main', async t => { const {stdout} = await execa('./cli.js', ['test.js']); t.regex(stdout, /^\d+ B$/); }); test('file', async t => { const {stdout} = await execa('./cli.js', ['test.js', '--raw']); t.is(Number.parseInt(stdout, 10), gzipSizeSync(fixture)); }); test('stdin', async t => { const {stdout} = await execa('./cli.js', ['test.js', '--raw'], { input: fs.createReadStream('test.js'), }); t.is(Number.parseInt(stdout, 10), gzipSizeSync(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); }); test('include original - stdin', async t => { const {stdout} = await execa('./cli.js', ['--raw', '--include-original'], { input: fs.createReadStream('test.js'), }); const {size} = fs.statSync('test.js'); t.is(Number.parseInt(stdout, 10), size); });