Files
publisher-dashboard/apps/cli/src/routes/bootstrap.ts

87 lines
2.3 KiB
TypeScript

import type { LocalContext } from "../context.js";
import { createDb, executeBootstrap } from "@reviq/db";
import { buildCommand } from "@stricli/core";
import { writeConfig } from "../utils/config.js";
interface BootstrapFlags {
email: string;
password: string;
dangerouslyOverwriteExisting: boolean;
}
async function bootstrap(
this: LocalContext,
flags: BootstrapFlags,
): Promise<void> {
console.log("RevIQ Bootstrap - Create Superuser");
console.log("===================================\n");
const databaseUrl = Bun.env.DATABASE_URL;
if (!databaseUrl) {
console.error("Error: DATABASE_URL environment variable is required");
this.process.exit(1);
}
const db = createDb(databaseUrl);
try {
// Execute the bootstrap operation
const result = await executeBootstrap(db, {
email: flags.email,
password: flags.password,
dangerouslyOverwriteExisting: flags.dangerouslyOverwriteExisting,
});
console.log(`Created superuser: ${result.user.email}`);
console.log(`Created org: ${result.org.slug}`);
// Save to config
await writeConfig({
apiUrl: Bun.env.API_URL ?? "http://localhost:9861",
token: result.token,
email: result.user.email,
});
console.log("Saved credentials to ~/.config/reviq/credentials.json");
console.log("\nBootstrap complete! You can now use the CLI.");
await db.destroy();
} catch (error) {
console.error(
"Error:",
error instanceof Error ? error.message : String(error),
);
await db.destroy();
this.process.exit(1);
}
}
export const bootstrapCommand = buildCommand({
func: bootstrap,
parameters: {
flags: {
email: {
kind: "parsed",
parse: String,
brief: "Email address for the superuser",
},
password: {
kind: "parsed",
parse: String,
brief: "Password for the superuser",
},
dangerouslyOverwriteExisting: {
kind: "boolean",
brief: "Delete existing user and reviq org if they exist",
default: false,
},
},
},
docs: {
brief: "Create a superuser account and initial organization",
fullDescription:
"Creates a superuser with the 'reviq' organization. " +
"Requires DATABASE_URL environment variable to be set.",
},
});