import process from 'node:process' import { serve } from '@hono/node-server' import { loadDevProxyConfig, parseDevProxyCliArgs, resolveDevProxyServerOptions } from './config' import { createDevProxyApp } from './server' function printUsage() { console.log(`Usage: dev-proxy --config [options] Options: --config, -c Path to a dev proxy config file. Defaults to dev-proxy.config.ts. --env-file Load environment variables before evaluating the config file. --host Override the configured host. --port Override the configured port. --help, -h Show this help message.`) } async function flushStandardStreams() { await Promise.all([ new Promise(resolve => process.stdout.write('', () => resolve())), new Promise(resolve => process.stderr.write('', () => resolve())), ]) } async function main() { const cliOptions = parseDevProxyCliArgs(process.argv.slice(2)) if (cliOptions.help) { printUsage() return } const config = await loadDevProxyConfig(cliOptions.config, process.cwd(), { envFile: cliOptions.envFile, }) const { host, port } = resolveDevProxyServerOptions(config.server, cliOptions) const app = createDevProxyApp(config) serve({ fetch: app.fetch, hostname: host, port, }) console.log(`[dev-proxy] listening on http://${host}:${port}`) } try { await main() await flushStandardStreams() } catch (error) { console.error(error instanceof Error ? error.message : error) await flushStandardStreams() process.exit(1) }