refactor: migrate new-data-fix script from bash to TypeScript
This commit is contained in:
@@ -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 <name>" >&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" <<EOF
|
|
||||||
package fixes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
Register(Fix{
|
|
||||||
ID: "${id}",
|
|
||||||
Apply: func(ctx context.Context, sqlDB *sql.DB) error {
|
|
||||||
// TODO: implement fix
|
|
||||||
// _, err := sqlDB.ExecContext(ctx, \`UPDATE ...\`)
|
|
||||||
// if err != nil { return fmt.Errorf("...: %w", err) }
|
|
||||||
return fmt.Errorf("unimplemented data fix: ${id}")
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
EOF
|
|
||||||
|
|
||||||
echo "$file_path"
|
|
||||||
}
|
|
||||||
|
|
||||||
main "$@"
|
|
||||||
74
scripts/new-data-fix.ts
Normal file
74
scripts/new-data-fix.ts
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import { existsSync } from "node:fs";
|
||||||
|
import { mkdir, writeFile } from "node:fs/promises";
|
||||||
|
import { join } from "node:path";
|
||||||
|
import { spawnSync } from "node:child_process";
|
||||||
|
|
||||||
|
const toSlug = (raw: string): string =>
|
||||||
|
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<void> => {
|
||||||
|
const rawName = process.argv[2] ?? "";
|
||||||
|
|
||||||
|
if (rawName === "--help" || rawName === "-h") {
|
||||||
|
console.log("usage: bun run ./scripts/new-data-fix.ts <name>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const slug = toSlug(rawName);
|
||||||
|
|
||||||
|
if (!slug) {
|
||||||
|
console.error("usage: bun run ./scripts/new-data-fix.ts <name>");
|
||||||
|
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;
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user