import { spawnSync } from 'node:child_process'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'

const root = join(dirname(fileURLToPath(import.meta.url)), '..')
const major = parseInt(process.versions.node.split('.')[0], 10)

if (major >= 20) {
  const r = spawnSync(process.execPath, [join(root, 'scripts', 'run-build-inner.mjs')], {
    stdio: 'inherit',
    cwd: root,
  })
  process.exit(r.status ?? 1)
}

const r = spawnSync(
  'npx',
  ['-y', '-p', 'node@20', 'node', join(root, 'scripts', 'run-build-inner.mjs')],
  { stdio: 'inherit', cwd: root, shell: false },
)
process.exit(r.status ?? 1)
