remove-account.mjs 1.04 KB
// 删除账号。
// 用法:  pnpm --filter @hm/server remove:account <用户名>
import { existsSync, readFileSync, writeFileSync, renameSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

const here = path.dirname(fileURLToPath(import.meta.url));
const ACCOUNTS_PATH = path.resolve(here, '../data/accounts.json');
const username = process.argv[2];

if (!username) {
  console.error('用法: pnpm --filter @hm/server remove:account <用户名>');
  process.exit(1);
}
if (!existsSync(ACCOUNTS_PATH)) {
  console.error('账号文件不存在');
  process.exit(1);
}

const accounts = JSON.parse(readFileSync(ACCOUNTS_PATH, 'utf-8'));
const next = accounts.filter((a) => a.username !== username);
if (next.length === accounts.length) {
  console.error(`未找到账号 "${username}"`);
  process.exit(1);
}

const tmp = ACCOUNTS_PATH + '.tmp';
writeFileSync(tmp, JSON.stringify(next, null, 2) + '\n', 'utf-8');
renameSync(tmp, ACCOUNTS_PATH);

console.log(`已删除账号 "${username}"(剩余 ${next.length} 个)`);