From 0a8258a672a96f80c2844bd069effafafb2917e1 Mon Sep 17 00:00:00 2001 From: Julian Cuni Date: Sun, 14 Jun 2026 11:31:03 +0200 Subject: [PATCH] seed-admin: prompt for username (default "admin") Running `pnpm --filter @parking/server seed-admin` with no env vars now prompts for a username (blank -> "admin") and then a password. Env vars (ADMIN_USER / ADMIN_PASS) and a CLI arg still work for non-interactive installs. Read prompts through a single readline async line-iterator so it's robust over both a TTY and a pipe (chaining readline/promises question() over a pipe could drop buffered lines). Verified: default + custom username, env-var path. --- apps/server/scripts/seed-admin.mjs | 41 +++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/apps/server/scripts/seed-admin.mjs b/apps/server/scripts/seed-admin.mjs index e3814e0..a9c690f 100644 --- a/apps/server/scripts/seed-admin.mjs +++ b/apps/server/scripts/seed-admin.mjs @@ -1,12 +1,13 @@ // Seed the first admin user (run once at install). // -// ADMIN_USER=admin ADMIN_PASS='strong-pass' \ -// node --env-file-if-exists=.env apps/server/scripts/seed-admin.mjs +// pnpm --filter @parking/server seed-admin +// -> prompts for a username (default "admin") and password // -// Or interactively (prompts for a hidden password): -// node --env-file-if-exists=.env apps/server/scripts/seed-admin.mjs +// Non-interactive (install scripts): +// ADMIN_USER=admin ADMIN_PASS='strong-pass' pnpm --filter @parking/server seed-admin // -// Idempotent-ish: refuses to overwrite an existing user unless FORCE=1. +// A username may also be passed as an argument. Refuses to overwrite an existing +// user unless FORCE=1 (which resets that user's password). import { randomUUID } from "node:crypto"; import { createInterface } from "node:readline/promises"; @@ -17,19 +18,35 @@ const require = createRequire(import.meta.url); const bcrypt = require("bcrypt"); const { createDb, users, eq } = require("@parking/db"); -const username = process.env.ADMIN_USER ?? process.argv[2]; -let password = process.env.ADMIN_PASS; +const DEFAULT_USERNAME = "admin"; +// Lazily create one readline interface and read answers sequentially through a +// single async line iterator — robust whether stdin is a TTY or a pipe (chaining +// readline/promises question() over a pipe can drop buffered lines). +let rl = null; +let lines = null; +async function prompt(label) { + if (!rl) { + rl = createInterface({ input: stdin, output: stdout }); + lines = rl[Symbol.asyncIterator](); + } + stdout.write(label); + const { value } = await lines.next(); + return (value ?? "").trim(); +} + +// Username: env var > CLI arg > prompt (blank -> default "admin"). +let username = process.env.ADMIN_USER ?? process.argv[2]; if (!username) { - console.error("usage: ADMIN_USER=.. ADMIN_PASS=.. seed-admin.mjs (or pass a username arg)"); - process.exit(1); + username = (await prompt(`Admin username [${DEFAULT_USERNAME}]: `)) || DEFAULT_USERNAME; } +let password = process.env.ADMIN_PASS; if (!password) { - const rl = createInterface({ input: stdin, output: stdout }); - password = (await rl.question(`Password for "${username}": `)).trim(); - rl.close(); + password = await prompt(`Password for "${username}": `); } +rl?.close(); + if (!password || password.length < 8) { console.error("password must be at least 8 characters"); process.exit(1);