Skip to content

Commit

Permalink
feat(tools): add clean-package script
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 22, 2024
1 parent ec53aba commit e8a84a1
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tools/src/clean-package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { readdirSync, rmSync, statSync, unlinkSync } from "node:fs";
import { basename, sep } from "node:path";

// utility functions adapted from thi.ng/file-io
// used here directly to be entirely self-contained...

const isDirectory = (path: string) => statSync(path).isDirectory();

const files = (dir: string, match: RegExp, maxDepth = Infinity) =>
__files(dir, match, maxDepth, 0);

function* __files(
dir: string,
match: RegExp,
maxDepth = Infinity,
depth = 0
): IterableIterator<string> {
if (depth >= maxDepth) return;
const pred = (x: string) => match.test(x);
for (let f of readdirSync(dir).sort()) {
const curr = dir + sep + f;
try {
if (isDirectory(curr)) {
yield* __files(curr, match, maxDepth, depth + 1);
} else if (pred(curr)) {
yield curr;
}
} catch (e) {
console.warn(`ignoring file: ${f} (${(<Error>e).message})`);
}
}
}

function* dirs(
dir: string,
pred: (x: string) => boolean,
maxDepth = Infinity,
depth = 0
): IterableIterator<string> {
if (depth >= maxDepth) return;
for (let f of readdirSync(dir).sort()) {
const curr = dir + sep + f;
try {
if (statSync(curr).isDirectory()) {
if (pred(curr)) yield curr;
yield* dirs(curr, pred, maxDepth, depth + 1);
}
} catch (e) {
console.warn(`ignoring file/dir: ${f} (${(<Error>e).message})`);
}
}
}

// accept & merge additional dirs as CLI args
const removeDirs = new Set([
"doc",
"api",
"generated",
"internal",
...process.argv.slice(2),
]);

for (let d of dirs(".", (x) => removeDirs.has(basename(x)), 1)) {
console.log("removing directory:", d);
rmSync(d, { recursive: true, force: true });
}

for (let f of files(".", /\.(map|js|d\.ts|tsbuildinfo)$/)) {
if (f.indexOf("/bin/") === -1) {
console.log("removing file:", f);
unlinkSync(f);
}
}

0 comments on commit e8a84a1

Please sign in to comment.