forked from Shared/esg
73 lines
2.2 KiB
TypeScript
73 lines
2.2 KiB
TypeScript
/**
|
|
* CLI entry point — creates the initial SUPER_ADMIN user.
|
|
* Run: npm run cli:create-super-admin
|
|
*/
|
|
import { NestFactory } from '@nestjs/core';
|
|
import * as readline from 'readline';
|
|
import { CliModule } from './cli.module';
|
|
import { CreateSuperAdminService } from './create-super-admin.service';
|
|
|
|
function prompt(rl: readline.Interface, question: string, hidden = false): Promise<string> {
|
|
return new Promise((resolve) => {
|
|
if (!hidden) {
|
|
rl.question(question, resolve);
|
|
return;
|
|
}
|
|
const stdin = process.stdin;
|
|
const onData = (char: Buffer): void => {
|
|
const c = char.toString('utf8');
|
|
switch (c) {
|
|
case '\n':
|
|
case '\r':
|
|
case '\u0004':
|
|
stdin.pause();
|
|
break;
|
|
default:
|
|
process.stdout.write('\x1B[2K\x1B[200D' + question + '*'.repeat(rl.line.length));
|
|
break;
|
|
}
|
|
};
|
|
stdin.on('data', onData);
|
|
rl.question(question, (answer) => {
|
|
stdin.removeListener('data', onData);
|
|
process.stdout.write('\n');
|
|
resolve(answer);
|
|
});
|
|
});
|
|
}
|
|
|
|
async function bootstrap(): Promise<void> {
|
|
const app = await NestFactory.createApplicationContext(CliModule, {
|
|
logger: ['error', 'warn', 'log'],
|
|
});
|
|
|
|
const service = app.get(CreateSuperAdminService);
|
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
|
|
try {
|
|
console.log('\n=== Create Super Admin ===\n');
|
|
|
|
const fullName = (await prompt(rl, 'Full name: ')).trim();
|
|
const username = (await prompt(rl, 'Username: ')).trim();
|
|
const email = (await prompt(rl, 'Email: ')).trim();
|
|
const password = (await prompt(rl, 'Password (min 8 chars): ', true)).trim();
|
|
|
|
rl.close();
|
|
|
|
const user = await service.create({ fullName, username, email, password });
|
|
console.log('\nSuper admin created successfully.');
|
|
console.log(` ID: ${user.id}`);
|
|
console.log(` Username: ${user.username}`);
|
|
console.log(` Email: ${user.email}`);
|
|
console.log(` Role: ${user.role}\n`);
|
|
} catch (error) {
|
|
rl.close();
|
|
console.error('\nFailed:', error instanceof Error ? error.message : error);
|
|
process.exitCode = 1;
|
|
} finally {
|
|
await app.close();
|
|
}
|
|
}
|
|
|
|
bootstrap();
|