Skip to content

Commit

Permalink
Remove globby dependency, use stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
sondr3 committed Dec 29, 2022
1 parent 079f852 commit ccae513
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
"format": "prettier --check src",
"format:fix": "prettier --write src"
},
"dependencies": {
"globby": "13.1.3"
},
"devDependencies": {
"@sondr3/eslint-config": "0.7.1",
"@sondr3/prettier": "0.6.0",
Expand Down
37 changes: 22 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 22 additions & 8 deletions src/compress.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { createReadStream, createWriteStream } from "node:fs";
import { readdir } from "node:fs/promises";
import { resolve } from "node:path";
import { hrtime } from "node:process";
import { promises as stream } from "node:stream";
import { createBrotliCompress, createGzip } from "node:zlib";

import { globby } from "globby";

import { Logger } from "./logger.js";

async function* walkDir(dir: string): AsyncGenerator<string> {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const name = resolve(dir, entry.name);
if (entry.isDirectory()) {
yield* walkDir(name);
} else if (filterFile(entry.name)) {
yield name;
}
}
}

const filterFile = (file: string): boolean => {
return [".css", ".js", ".html", ".xml", ".cjs", ".mjs", ".svg", ".txt"].some((ext) =>
file.endsWith(ext),
Expand All @@ -16,29 +28,31 @@ const filterFile = (file: string): boolean => {
export const gzip = async (dir: URL): Promise<void> => {
const start = hrtime.bigint();

const files = (await globby(`${dir.pathname}/**/*`)).filter(filterFile);
for (const file of files) {
let counter = 0;
for await (const file of walkDir(dir.pathname)) {
counter += 1;
const source = createReadStream(file);
const destination = createWriteStream(`${file}.gz`);
const gzip = createGzip({ level: 9 });
await stream.pipeline(source, gzip, destination);
}

const end = hrtime.bigint();
Logger.success(`finished gzip of ${files.length} files in ${(end - start) / 1000000n}m`);
Logger.success(`finished gzip of ${counter} files in ${(end - start) / 1000000n}m`);
};

export const brotli = async (dir: URL): Promise<void> => {
const start = hrtime.bigint();

const files = (await globby(`${dir.pathname}/**/*`)).filter(filterFile);
for (const file of files) {
let counter = 0;
for await (const file of walkDir(dir.pathname)) {
counter += 1;
const source = createReadStream(file);
const destination = createWriteStream(`${file}.br`);
const brotli = createBrotliCompress();
await stream.pipeline(source, brotli, destination);
}

const end = hrtime.bigint();
Logger.success(`finished brotli of ${files.length} files in ${(end - start) / 1000000n}m`);
Logger.success(`finished brotli of ${counter} files in ${(end - start) / 1000000n}m`);
};

0 comments on commit ccae513

Please sign in to comment.