#!/usr/bin/env tsx /** * Create a local user account. * Usage: npm run create-user -- --email you@lab.local --password 'secret' [--name "You"] */ import { hash } from "bcryptjs"; import { eq } from "drizzle-orm"; import { getDb, resetDbConnection } from "../src/db/client"; import { users } from "../src/db/schema"; function arg(name: string): string | undefined { const idx = process.argv.indexOf(`--${name}`); if (idx === -1) return undefined; return process.argv[idx + 1]; } async function main() { const email = arg("email")?.trim().toLowerCase(); const password = arg("password"); const name = arg("name") || email?.split("@")[0] || "User"; if (!email || !password) { console.error( "Usage: npm run create-user -- --email you@lab.local --password 'secret' [--name Name]" ); process.exit(1); } const db = getDb(); const existing = db.select().from(users).where(eq(users.email, email)).get(); if (existing) { console.error(`User already exists: ${email}`); process.exit(1); } const passwordHash = await hash(password, 12); const id = crypto.randomUUID(); db.insert(users) .values({ id, email, name, passwordHash, createdAt: new Date(), }) .run(); console.log(`Created user ${email} (${id})`); resetDbConnection(); } main().catch((err) => { console.error(err); process.exit(1); });