/**
 * Postinstall wrapper (CommonJS) — runs vendor copy and ripgrep download.
 * All scripts are CJS to avoid ESM loader issues during npm install -g.
 */
const { resolve } = require('path');

const root = resolve(__dirname, '..');

// Ensure CWD is valid (npm global installs can leave CWD dangling)
try { process.cwd(); } catch { process.chdir(root); }

// Run copy-vendor directly (same process, no child spawn needed)
try {
  require(resolve(root, 'scripts/copy-vendor.cjs'));
} catch (e) {
  console.warn('copy-vendor failed (non-fatal):', e.message);
}

// Run download-ripgrep in a child process (it's async)
try {
  require('child_process').execFileSync(
    process.execPath,
    [resolve(root, 'scripts/download-ripgrep.cjs')],
    { cwd: root, stdio: 'inherit', env: { ...process.env, NODE_OPTIONS: '' } },
  );
} catch (e) {
  console.warn('download-ripgrep failed (non-fatal):', e.message);
}
