diff --git a/scripts/fix-all.sh b/scripts/fix-all.sh deleted file mode 100755 index 00cc2bb..0000000 --- a/scripts/fix-all.sh +++ /dev/null @@ -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" \ No newline at end of file diff --git a/scripts/fix-all.ts b/scripts/fix-all.ts new file mode 100644 index 0000000..9d32b5f --- /dev/null +++ b/scripts/fix-all.ts @@ -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");