refactor: migrate fix-all script from bash to TypeScript

This commit is contained in:
2026-06-21 01:16:42 +02:00
committed by Milas Holsting
parent 85d986039b
commit 95db00f389
2 changed files with 68 additions and 29 deletions

View File

@@ -1,29 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
get_hash() {
find . -name "*.go" -type f ! -path "./vendor/*" -exec sha256sum {} + | sha256sum | cut -d' ' -f1
}
echo "running go fix recursively until no changes..."
prev_hash=$(get_hash)
iteration=0
while true; do
iteration=$((iteration + 1))
echo "iteration $iteration..."
go fix ./... >/dev/null 2>&1 || true
curr_hash=$(get_hash)
if [[ "$prev_hash" == "$curr_hash" ]]; then
echo "no more changes after $iteration iteration(s)"
break
fi
prev_hash=$curr_hash
done
echo "done"

68
scripts/fix-all.ts Normal file
View File

@@ -0,0 +1,68 @@
import { createHash } from "node:crypto";
import { readdirSync, readFileSync, statSync } from "node:fs";
import { join, relative } from "node:path";
import { spawnSync } from "node:child_process";
const ignoredDirectories = new Set([".git", "dist", "node_modules", "vendor"]);
const goFiles = (root: string): string[] => {
const files: string[] = [];
const walk = (dir: string): void => {
for (const entry of readdirSync(dir)) {
if (ignoredDirectories.has(entry)) {
continue;
}
const path = join(dir, entry);
const stat = statSync(path);
if (stat.isDirectory()) {
walk(path);
continue;
}
if (stat.isFile() && path.endsWith(".go")) {
files.push(path);
}
}
};
walk(root);
return files.sort();
};
const sourceHash = (): string => {
const hash = createHash("sha256");
for (const file of goFiles(process.cwd())) {
hash.update(relative(process.cwd(), file));
hash.update("\0");
hash.update(readFileSync(file));
hash.update("\0");
}
return hash.digest("hex");
};
const runGoFix = (): void => {
spawnSync("go", ["fix", "./..."], { stdio: "ignore" });
};
console.log("running go fix recursively until no changes...");
let previousHash = sourceHash();
let iteration = 0;
while (true) {
iteration += 1;
console.log(`iteration ${iteration}...`);
runGoFix();
const currentHash = sourceHash();
if (previousHash === currentHash) {
console.log(`no more changes after ${iteration} iteration(s)`);
break;
}
previousHash = currentHash;
}
console.log("done");