Last active
March 6, 2020 16:26
-
-
Save aduh95/600f97d2cf52c97a310991e680e3e095 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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