Spaces:
Paused
Paused
File size: 4,552 Bytes
9de864e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #!/usr/bin/env node
import readline from 'readline';
import { loadTokens } from '../src/api/tokenManager.js';
import { addAccountInteractive, reloginAccountInteractive, removeAccountInteractive } from '../src/utils/accountSetup.js';
function prompt(question) {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
return new Promise(resolve => rl.question(question, answer => { rl.close(); resolve(answer.trim()); }));
}
function printDivider() {
console.log('======================================================');
}
const STATUS_CODES = {
INVALID: 0,
WAIT: 1,
OK: 2
};
function formatStatus(token) {
const now = Date.now();
if (token.invalid) {
return { code: STATUS_CODES.INVALID, label: '❌ Недействителен' };
}
if (token.resetAt && new Date(token.resetAt).getTime() > now) {
return { code: STATUS_CODES.WAIT, label: '⏳ Ожидание сброса' };
}
return { code: STATUS_CODES.OK, label: '✅ OK' };
}
function printAccounts(tokens) {
console.log('\nСписок аккаунтов:');
if (!tokens.length) {
console.log(' (пусто)');
return;
}
tokens.forEach((token, index) => {
const status = formatStatus(token);
console.log(`${String(index + 1).padStart(2, ' ')} | ${token.id} | ${status.label} (${status.code})`);
});
}
function handleList(tokens) {
printAccounts(tokens);
const active = tokens.filter(t => formatStatus(t).code === STATUS_CODES.OK);
console.log(`\nАктивных аккаунтов: ${active.length} из ${tokens.length}`);
}
function parseArgs(argv) {
const args = new Set(argv.slice(2));
if (args.has('--help') || args.has('-h')) return 'help';
if (args.has('--list')) return 'list';
if (args.has('--add')) return 'add';
if (args.has('--relogin')) return 'relogin';
if (args.has('--remove')) return 'remove';
return null;
}
function printHelp() {
printDivider();
console.log('Скрипт управления аккаунтами Qwen');
printDivider();
console.log('Опции:');
console.log(' --list Показать список аккаунтов и статусы');
console.log(' --add Добавить новый аккаунт');
console.log(' --relogin Перелогинить аккаунт с истекшим токеном');
console.log(' --remove Удалить аккаунт');
console.log('Без опций запускается интерактивное меню.');
printDivider();
}
async function runCliAction(action) {
if (action === 'help') {
printHelp();
return;
}
if (action === 'list') {
const tokens = loadTokens();
handleList(tokens);
return;
}
if (action === 'add') {
await addAccountInteractive();
return;
}
if (action === 'relogin') {
await reloginAccountInteractive();
return;
}
if (action === 'remove') {
await removeAccountInteractive();
return;
}
}
async function runInteractiveMenu() {
while (true) {
const tokens = loadTokens();
printDivider();
printAccounts(tokens);
printDivider();
console.log('Меню:');
console.log('1 - Добавить новый аккаунт');
console.log('2 - Перелогинить аккаунт с истекшим токеном');
console.log('3 - Удалить аккаунт');
console.log('4 - Показать список и статусы');
console.log('5 - Выход');
const choice = await prompt('Ваш выбор (Enter = 5): ');
const normalized = choice || '5';
if (normalized === '1') {
await addAccountInteractive();
} else if (normalized === '2') {
await reloginAccountInteractive();
} else if (normalized === '3') {
await removeAccountInteractive();
} else if (normalized === '4') {
handleList(tokens);
await prompt('\nНажмите Enter, чтобы вернуться в меню...');
} else if (normalized === '5') {
console.log('Выход из скрипта.');
break;
}
}
}
(async () => {
const action = parseArgs(process.argv);
if (action) {
await runCliAction(action);
return;
}
await runInteractiveMenu();
})();
|