#!/usr/bin/env -S node --enable-source-maps

import simpleUpdateNotifier from 'simple-update-notifier'
import * as commander from 'commander'
import * as packageJSON from '../package.json'
import { program } from './commands'
import { commands2md } from './utils/commands2md'

export const pkg = packageJSON

const updateCheck = simpleUpdateNotifier({
  pkg,
  updateCheckInterval: 1000 * 60 * 60 * 8, // 8 hours
}).catch((e) => {
  if (process.env.DEBUG) {
    console.error('Update check failed:', e)
  }
})

const prog = program.version(
  packageJSON.version,
  undefined,
  'display E2B CLI version'
)

if (process.env.NODE_ENV === 'development') {
  prog
    .addOption(new commander.Option('-cmd2md').hideHelp())
    .on('option:-cmd2md', () => {
      commands2md(program.commands as any)
      process.exit(0)
    })
}

async function main() {
  await prog.parseAsync()
  await updateCheck
}

main()
