#!/usr/bin/env node
// CJS shim that runs before the ESM bundle to fail fast on unsupported Node.
// Kept in CJS so the parse step uses only Node ≥0.x APIs — this file must
// load cleanly even on the Node versions we are about to reject.
'use strict';

const { checkNodeVersion, formatBanner } = require('./check-node.cjs');

const result = checkNodeVersion(process.versions.node);

if (!result.ok) {
  process.stderr.write(formatBanner(result.detected));
  // 64 = EX_USAGE per BSD sysexits — "the command was used incorrectly".
  // Distinct from 1 (generic), 2 (pleri-not-configured), 3 (pull failed),
  // 4 (protocol mismatch), 5 (auth); see packages/cli/src/exit-codes.ts.
  process.exit(64);
}

const { join } = require('node:path');
const { pathToFileURL } = require('node:url');
const bundlePath = join(__dirname, '..', 'dist', 'index.js');

import(pathToFileURL(bundlePath).href).catch((err) => {
  process.stderr.write(`olam: failed to load CLI bundle (${bundlePath})\n  ${err && err.message ? err.message : err}\n`);
  process.exit(1);
});
