srs-api/scripts/user.ts
Aswin Biju 6851d66b38 feat: SRS backend: orders intake, results pipeline, user management
Fastify + TypeScript + SQLite backend for a single Kerala lottery agency.
2026-11-03 10:09:46 +05:30

87 lines
2.1 KiB
TypeScript

/**
* npm run user create a short wizard: choose Admin or User
* npm run user list show every account
*
*/
import { createInterface } from 'node:readline';
import { Cancelled, createWizard, listLines, type Io } from '../src/cli/userWizard';
import { loadConfig } from '../src/config';
import { openDb } from '../src/db';
import { runMigrations } from '../src/db/migrate';
function makeIo(): { io: Io; close: () => void } {
const rl = createInterface({ input: process.stdin, output: process.stdout });
const queue: string[] = [];
let waiting: ((line: string | null) => void) | null = null;
let ended = false;
rl.on('line', (line) => {
if (waiting) {
const w = waiting;
waiting = null;
w(line);
} else {
queue.push(line);
}
});
rl.on('close', () => {
ended = true;
if (waiting) {
const w = waiting;
waiting = null;
w(null);
}
});
const io: Io = {
async ask(question) {
rl.setPrompt(question);
rl.prompt();
const line = queue.length > 0 ? queue.shift()! : ended ? null : await new Promise<string | null>((r) => (waiting = r));
if (line === null) throw new Cancelled();
return line.trim();
},
say(line = '') {
console.log(line);
},
};
return { io, close: () => rl.close() };
}
async function main(): Promise<number> {
const command = process.argv[2];
if (command !== 'create' && command !== 'list') {
console.log('usage:');
console.log(' npm run user create create an admin or a user (a short wizard)');
console.log(' npm run user list show every account');
return command ? 1 : 0;
}
const config = loadConfig();
const db = openDb(config.dbPath);
runMigrations(db);
try {
if (command === 'list') {
for (const line of listLines(db)) console.log(line);
return 0;
}
const { io, close } = makeIo();
try {
return await createWizard(db, io, config.defaultCountryCode);
} finally {
close();
}
} finally {
db.close();
}
}
main().then(
(code) => process.exit(code),
(err) => {
console.error(String(err));
process.exit(1);
},
);