← back to Rentv Adintel

scripts/export-all.js

50 lines

#!/usr/bin/env node
'use strict';
/**
 * CLI: node scripts/export-all.js [outDir]
 *
 * Runs buildDownloadEverything and prints the ZIP path + row counts to stdout.
 * outDir defaults to ./data/exports (relative to project root).
 *
 * Usage:
 *   node scripts/export-all.js
 *   node scripts/export-all.js /tmp/adintel-export-test
 *
 * @module scripts/export-all
 */

require('../lib/env');
const path = require('path');
const { buildDownloadEverything } = require('../src/export/build');
const { pool } = require('../db');

const outDir = process.argv[2]
  ? path.resolve(process.argv[2])
  : path.join(__dirname, '..', 'data', 'exports');

console.log('[export-all] Starting Download Everything export...');
console.log(`[export-all] Output directory: ${outDir}`);

(async () => {
  let exitCode = 0;
  try {
    const { zipPath, rowCounts } = await buildDownloadEverything({ outDir });

    console.log('\n[export-all] Export complete.');
    console.log('[export-all] ZIP path:', zipPath);
    console.log('[export-all] Row counts:');
    for (const [k, v] of Object.entries(rowCounts)) {
      console.log(`  ${k.padEnd(30)} ${v}`);
    }
    console.log('[export-all] Done.');
  } catch (err) {
    console.error('[export-all] FAILED:', err.message);
    console.error(err.stack);
    exitCode = 1;
  } finally {
    // Cleanly drain the pg pool so Node exits
    try { await pool.end(); } catch (_) {}
    process.exit(exitCode);
  }
})();