sync-works-for-account.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { initDatabase, AppDataSource, PlatformAccount } from '../models/index.js';
  2. import { logger } from '../utils/logger.js';
  3. import { WorkService } from '../services/WorkService.js';
  4. async function main() {
  5. const arg = process.argv[2];
  6. if (!arg) {
  7. logger.error('请提供账号(platform_accounts.id 或 account_id 字符串)');
  8. logger.info('用法: tsx src/scripts/sync-works-for-account.ts <6|dy_1742363409>');
  9. process.exit(1);
  10. }
  11. await initDatabase();
  12. const accountRepo = AppDataSource.getRepository(PlatformAccount);
  13. const maybeId = Number(arg);
  14. let account: PlatformAccount | null = null;
  15. if (!Number.isNaN(maybeId) && Number.isInteger(maybeId) && maybeId > 0) {
  16. account = await accountRepo.findOne({ where: { id: maybeId } });
  17. }
  18. if (!account) {
  19. account = await accountRepo.findOne({ where: { accountId: arg } });
  20. }
  21. if (!account) {
  22. logger.error(`未找到账号: ${arg}`);
  23. process.exit(1);
  24. }
  25. logger.info(`准备同步账号: id=${account.id}, userId=${account.userId}, platform=${account.platform}, accountId=${account.accountId}`);
  26. const workService = new WorkService();
  27. const result = await workService.syncWorks(
  28. account.userId,
  29. account.id,
  30. undefined,
  31. (p, step) => logger.info(`[进度] ${p}% ${step}`)
  32. );
  33. logger.info(`同步完成: synced=${result.synced}, accounts=${result.accounts}`);
  34. logger.info(`账号摘要: ${JSON.stringify(result.accountSummaries, null, 2)}`);
  35. process.exit(0);
  36. }
  37. void main().catch((e) => {
  38. logger.error('执行失败:', e);
  39. process.exit(1);
  40. });