54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import { closePool } from './db.js';
|
|
import { loadConfig } from './config.js';
|
|
import { AuthService } from './auth.js';
|
|
import { writeEmergencyDiagnostic } from './diagnostics.js';
|
|
|
|
function readFlag(name: string): string {
|
|
const index = process.argv.indexOf(name);
|
|
return index >= 0 ? String(process.argv[index + 1] || '') : '';
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
const command = process.argv[2] || '';
|
|
const config = loadConfig();
|
|
const auth = new AuthService(config);
|
|
if (command === 'bootstrap') {
|
|
const user = await auth.bootstrapAdmin(
|
|
process.env.ADMIN_USERNAME || readFlag('--username'),
|
|
process.env.ADMIN_PASSWORD || readFlag('--password'),
|
|
{ force: process.argv.includes('--force') }
|
|
);
|
|
console.log(JSON.stringify({
|
|
ok: true,
|
|
diagnostic_event: 'admin.bootstrap.completed',
|
|
diagnostic_stage: 'admin_cli',
|
|
action: 'bootstrap',
|
|
administrator_created: Boolean(user.id)
|
|
}));
|
|
return;
|
|
}
|
|
if (command === 'reset-password') {
|
|
await auth.resetPassword(
|
|
process.env.ADMIN_USERNAME || readFlag('--username'),
|
|
process.env.ADMIN_PASSWORD || readFlag('--password')
|
|
);
|
|
console.log(JSON.stringify({
|
|
ok: true,
|
|
diagnostic_event: 'admin.password_reset.completed',
|
|
diagnostic_stage: 'admin_cli',
|
|
action: 'reset-password'
|
|
}));
|
|
return;
|
|
}
|
|
throw new Error('Usage: npm run admin -- bootstrap|reset-password (use ADMIN_USERNAME and ADMIN_PASSWORD)');
|
|
}
|
|
|
|
main()
|
|
.catch((error) => {
|
|
writeEmergencyDiagnostic('admin.command.failed', error, {
|
|
diagnostic_stage: 'admin_cli'
|
|
});
|
|
process.exitCode = 1;
|
|
})
|
|
.finally(() => closePool());
|