Files
ecom-dmb/ecom-license/tui/src/index.ts
T

84 lines
2.1 KiB
JavaScript

#!/usr/bin/env node
import { input, select } from '@inquirer/prompts';
import { customerMenu } from './commands/customer.js';
import { licenseMenu } from './commands/license.js';
import { loginCommand } from './commands/login.js';
import { logoutCommand } from './commands/logout.js';
import { statsCommand } from './commands/stats.js';
import { whoamiCommand } from './commands/whoami.js';
import { loadConfig } from './config.js';
import { printBanner } from './ui/render.js';
async function mainMenu() {
console.clear();
printBanner();
const config = await loadConfig();
const isLoggedIn = Boolean(config?.token);
const choices: { name: string; value: string }[] = [];
if (!isLoggedIn) {
choices.push({ name: '登录', value: 'login' });
} else {
choices.push(
{ name: '当前会话', value: 'whoami' },
{ name: '客户管理', value: 'customer' },
{ name: '许可证管理', value: 'license' },
{ name: '平台统计', value: 'stats' },
{ name: '退出登录', value: 'logout' },
);
}
choices.push({ name: '退出', value: 'exit' });
const action = await select({
message: '主菜单',
choices,
pageSize: 10,
});
switch (action) {
case 'login':
console.clear();
await loginCommand({});
await input({ message: '按 Enter 返回' });
break;
case 'whoami':
console.clear();
await whoamiCommand();
await input({ message: '按 Enter 返回' });
break;
case 'customer':
await customerMenu();
break;
case 'license':
await licenseMenu();
break;
case 'stats':
console.clear();
await statsCommand();
await input({ message: '按 Enter 返回' });
break;
case 'logout':
console.clear();
await logoutCommand();
await input({ message: '按 Enter 返回' });
break;
case 'exit':
process.exit(0);
}
}
async function main() {
while (true) {
try {
await mainMenu();
} catch (e: any) {
if (e?.name === 'ExitPromptError') process.exit(0);
console.error(e?.message || e);
}
}
}
main();