Skip to content

Instantly share code, notes, and snippets.

@developit
Last active July 15, 2019 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developit/3ccdb60e478771188c22e510bb524455 to your computer and use it in GitHub Desktop.
Save developit/3ccdb60e478771188c22e510bb524455 to your computer and use it in GitHub Desktop.

gzip-sizes

A tiny CLI tool to print the gzipped sizes of files matching a glob pattern.

Usage

gzip-sizes [...pattern] [options]

Options:

-d, --dir        Find within a directory
-s, --sort       Sort by size or name  (default: size)

Examples:

# show the gzipped size of all JS files:
gzip-sizes **/*.js

# show the size of all CSS files within ./build, in descending order of size:
gzip-sizes *.css --dir build --sort size-

# find large JS files in your npm dependencies:
gzip-sizes **/node_modules/**/*.js --sort size-
node_modules
package-lock.json
#! /usr/bin/env node
/**
* Copyright 2019 Google LLC.
* SPDX-License-Identifier: Apache-2.0
*/
const path = require('path');
const fs = require('fs');
const sade = require('sade');
const gzipSize = require('gzip-size');
const glob = require('glob');
const kleur = require('kleur');
const pkg = require('./package.json');
sade(`${pkg.name} [...pattern]`, true)
.describe(pkg.description)
.version(pkg.version)
.example(kleur.cyan('**/*.js'))
.example(kleur.cyan('*.css ') + kleur.magenta('--sort size- ') + kleur.dim(' # CSS files in descending order of size'))
.example(kleur.cyan('**/node_modules/**/*.js ') + kleur.magenta('--sort size- ') + kleur.dim(' # find large JS files in npm deps'))
.option('--dir -d, --cwd', 'Within a directory')
.option('--sort -s', 'Sort by size or name', 'size')
.action((pattern, opts) => {
const dir = path.resolve(process.cwd(), opts.dir || opts.cwd || '.');
if (opts._.length) pattern = `{${pattern},${opts._.join(',')}}`;
sizes(dir, pattern, opts.sort);
})
.parse(process.argv);
function sizes(dir, pattern, sort) {
glob(pattern, { cwd: dir }, (err, files) => {
if (err) return process.stderr.write(`\n${err.message}`);
const output = [];
let total = 0, count = 0, maxSize = 0;
for (const filename of files) {
const contents = fs.readFileSync(path.join(dir, filename));
let size = gzipSize.sync(contents);
maxSize = Math.max(maxSize, String(size).length);
output.push({ size, filename });
count++;
total += size;
}
let direction = 1;
if (sort.indexOf('-')!==-1) {
sort = sort.replace('-', '');
direction = -1;
}
output.sort((a, b) => {
if (sort==='name' || sort==='filename') return direction * a.filename.localeCompare(b.filename);
return direction * (a[sort] - b[sort]);
});
const text = output.reduce((text, item) => {
let sizeText = item.size + '';
while (sizeText.length<maxSize) sizeText = ' ' + sizeText;
let color = 'yellow';
if (item.size > (total/count*2)) color = 'red';
else if (item.size < (total/count/2)) color = 'green';
return text + '\n ' + kleur[color](sizeText) + kleur.grey('b') + ' ' + item.filename;
}, '');
process.stdout.write(kleur.bold(`${count} files, total size: ${total}b`) + text + '\n');
});
}
{
"name": "gzip-sizes",
"version": "1.0.1",
"description": "A tiny CLI to print the gzipped size of all files matching a pattern.",
"main": "bin.js",
"bin": "bin.js",
"author": "Jason Miller <jason@developit.ca>",
"license": "Apache-2.0",
"scripts": {
"prepack": "mv \"*gzip-sizes cli.md\" README.md",
"postpack": "mv README.md \"*gzip-sizes cli.md\""
},
"dependencies": {
"glob": "^7.1.4",
"gzip-size": "^5.1.1",
"kleur": "^3.0.3",
"sade": "^1.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment