Skip to content

Instantly share code, notes, and snippets.

@aduh95
Last active March 6, 2020 16:26
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 aduh95/600f97d2cf52c97a310991e680e3e095 to your computer and use it in GitHub Desktop.
Save aduh95/600f97d2cf52c97a310991e680e3e095 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
import { promises as fs, createReadStream } from "fs";
import { createBrotliCompress } from "zlib";
import { resolve, join } from "path";
const DIR = resolve(process.argv[2]);
fs.readdir(DIR)
.then(files =>
Promise.all(
files.map(
fileName =>
new Promise(async (resolve, reject) => {
const filePath = join(DIR, fileName);
const stats = await fs.stat(filePath);
if (stats.isFile()) {
const stream = createReadStream(filePath).pipe(
createBrotliCompress()
);
let counter = 0;
stream.once("error", reject);
stream.on("data", chunk => {
counter += chunk.length;
});
stream.on("end", () =>
resolve([
fileName,
{
uncompressedSize: stats.size,
brotliSize: counter,
ratio: counter / stats.size,
},
])
);
} else {
resolve([fileName, null]);
}
})
)
)
)
.then(Object.fromEntries)
.then(console.log)
.catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment