From 41b5246111273c5c484a9eb762f3fb5e93a98475 Mon Sep 17 00:00:00 2001 From: mkelvers Date: Sun, 21 Jun 2026 01:16:48 +0200 Subject: [PATCH] refactor: migrate new-data-fix script from bash to TypeScript --- scripts/new-data-fix.sh | 62 ---------------------------------- scripts/new-data-fix.ts | 74 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 62 deletions(-) delete mode 100755 scripts/new-data-fix.sh create mode 100644 scripts/new-data-fix.ts diff --git a/scripts/new-data-fix.sh b/scripts/new-data-fix.sh deleted file mode 100755 index a54b571..0000000 --- a/scripts/new-data-fix.sh +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -to_slug() { - local raw="$1" - echo "$raw" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/_/g' | sed -E 's/^_+|_+$//g' -} - -format_yyyymmdd() { - date '+%Y%m%d' -} - -main() { - local raw_name="${1:-}" - local slug - slug=$(to_slug "$raw_name") - - if [[ -z "$slug" ]]; then - echo "usage: $0 " >&2 - exit 1 - fi - - local id - id="$(format_yyyymmdd)_${slug}" - local dir - dir="$(pwd)/internal/database/fixes" - local file_path - file_path="${dir}/${id}.go" - - mkdir -p "$dir" - - if [[ -f "$file_path" ]]; then - echo "data fix already exists: ${file_path}" >&2 - exit 1 - fi - - cat > "$file_path" < + raw + .toLowerCase() + .replace(/[^a-z0-9]+/g, "_") + .replace(/^_+|_+$/g, ""); + +const formatYyyymmdd = (date = new Date()): string => { + const year = date.getFullYear(); + const month = String(date.getMonth() + 1).padStart(2, "0"); + const day = String(date.getDate()).padStart(2, "0"); + return `${year}${month}${day}`; +}; + +const sourceFor = (id: string): string => `package fixes + +import ( +\t"context" +\t"database/sql" +\t"fmt" +) + +func init() { +\tRegister(Fix{ +\t\tID: "${id}", +\t\tApply: func(ctx context.Context, sqlDB *sql.DB) error { +\t\t\t// TODO: implement fix +\t\t\t// _, err := sqlDB.ExecContext(ctx, \`UPDATE ...\`) +\t\t\t// if err != nil { return fmt.Errorf("...: %w", err) } +\t\t\treturn fmt.Errorf("unimplemented data fix: ${id}") +\t\t}, +\t}) +} +`; + +const main = async (): Promise => { + const rawName = process.argv[2] ?? ""; + + if (rawName === "--help" || rawName === "-h") { + console.log("usage: bun run ./scripts/new-data-fix.ts "); + return; + } + + const slug = toSlug(rawName); + + if (!slug) { + console.error("usage: bun run ./scripts/new-data-fix.ts "); + process.exit(1); + } + + const id = `${formatYyyymmdd()}_${slug}`; + const dir = join(process.cwd(), "internal", "database", "fixes"); + const filePath = join(dir, `${id}.go`); + + if (existsSync(filePath)) { + console.error(`data fix already exists: ${filePath}`); + process.exit(1); + } + + await mkdir(dir, { recursive: true }); + await writeFile(filePath, sourceFor(id)); + spawnSync("gofmt", ["-w", filePath], { stdio: "ignore" }); + + console.log(filePath); +}; + +main().catch((error: unknown) => { + console.error(error instanceof Error ? error.message : String(error)); + process.exitCode = 1; +});