@kubb/cli
Version:
Command-line interface for Kubb, enabling easy generation of TypeScript, React-Query, Zod, and other code from OpenAPI specifications.
1 lines • 6.73 kB
Source Map (JSON)
{"version":3,"file":"start-lrn1-P52.cjs","names":["path","process"],"sources":["../src/commands/agent/start.ts"],"sourcesContent":["import { spawn } from 'node:child_process'\nimport path from 'node:path'\nimport * as process from 'node:process'\nimport { fileURLToPath } from 'node:url'\nimport { styleText } from 'node:util'\nimport * as clack from '@clack/prompts'\nimport type { ArgsDef } from 'citty'\nimport { defineCommand } from 'citty'\n\nconst args = {\n config: {\n type: 'string',\n description: 'Path to the Kubb config',\n alias: 'c',\n },\n port: {\n type: 'string',\n description: 'Port for the server. If not specified, an available port is automatically selected.',\n alias: 'p',\n },\n host: {\n type: 'string',\n description: 'Host for the server',\n default: 'localhost',\n },\n 'no-cache': {\n type: 'boolean',\n description: 'Disable session caching',\n default: false,\n },\n 'allow-write': {\n type: 'boolean',\n description: 'Allow writing generated files to the filesystem. When not set, no files are written and the config patch is not persisted.',\n default: false,\n },\n 'allow-all': {\n type: 'boolean',\n description: 'Grant all permissions (implies --allow-write).',\n default: false,\n },\n} as const satisfies ArgsDef\n\ntype StartServerProps = {\n port: number\n host: string\n configPath: string\n noCache: boolean\n allowWrite: boolean\n allowAll: boolean\n}\n\nasync function startServer({ port, host, configPath, noCache, allowWrite, allowAll }: StartServerProps): Promise<void> {\n try {\n // Load .env file into process.env using Node.js built-in (v20.12.0+)\n try {\n process.loadEnvFile()\n } catch {\n // .env file may not exist; ignore\n }\n\n // Resolve the @kubb/agent package path\n const agentPkgUrl = import.meta.resolve('@kubb/agent/package.json')\n const agentPkgPath = fileURLToPath(agentPkgUrl)\n const agentDir = path.dirname(agentPkgPath)\n const serverPath = path.join(agentDir, '.output', 'server', 'index.mjs')\n\n // nitro env\n const PORT = process.env.PORT || (port === 0 ? '3000' : String(port))\n const HOST = process.env.HOST || host || '0.0.0.0'\n\n // kubb env\n const KUBB_AGENT_ROOT = process.env.KUBB_AGENT_ROOT || process.cwd()\n const KUBB_AGENT_CONFIG = process.env.KUBB_AGENT_CONFIG || configPath || 'kubb.config.ts'\n const KUBB_AGENT_NO_CACHE = noCache ? 'true' : 'false'\n const KUBB_AGENT_ALLOW_WRITE = allowAll || allowWrite ? 'true' : (process.env.KUBB_AGENT_ALLOW_WRITE ?? 'false')\n const KUBB_AGENT_ALLOW_ALL = allowAll ? 'true' : (process.env.KUBB_AGENT_ALLOW_ALL ?? 'false')\n const KUBB_AGENT_TOKEN = process.env.KUBB_AGENT_TOKEN\n const KUBB_AGENT_RETRY_TIMEOUT = process.env.KUBB_AGENT_RETRY_TIMEOUT || '30000'\n const KUBB_STUDIO_URL = process.env.KUBB_STUDIO_URL || 'https://studio.kubb.dev'\n\n // Set environment variables\n const env = {\n PORT,\n HOST,\n KUBB_AGENT_ROOT,\n KUBB_AGENT_CONFIG,\n KUBB_AGENT_NO_CACHE,\n KUBB_AGENT_ALLOW_WRITE,\n KUBB_AGENT_ALLOW_ALL,\n KUBB_AGENT_TOKEN,\n KUBB_AGENT_RETRY_TIMEOUT,\n KUBB_STUDIO_URL,\n }\n\n clack.log.step(styleText('cyan', 'Starting agent server...'))\n clack.log.info(styleText('dim', `Config: ${KUBB_AGENT_CONFIG}`))\n clack.log.info(styleText('dim', `Host: ${HOST}`))\n clack.log.info(styleText('dim', `Port: ${PORT}`))\n if (noCache) {\n clack.log.info(styleText('dim', 'Session caching: disabled'))\n }\n if (!KUBB_AGENT_ALLOW_WRITE && !KUBB_AGENT_ALLOW_ALL) {\n clack.log.warn(styleText('yellow', 'Filesystem writes disabled. Use --allow-write or --allow-all to enable.'))\n }\n\n spawn('node', [serverPath], {\n env: { ...process.env, ...env },\n stdio: 'inherit',\n cwd: process.cwd(),\n })\n } catch (error) {\n console.error('Failed to start agent server:', error)\n process.exit(1)\n }\n}\n\nconst command = defineCommand({\n meta: {\n name: 'start',\n description: 'Start the Agent server',\n },\n args,\n async run(commandContext) {\n const { args } = commandContext\n\n try {\n const configPath = path.resolve(process.cwd(), args.config || 'kubb.config.ts')\n const port = args.port ? Number.parseInt(args.port, 10) : 0\n const host = args.host\n const noCache = args['no-cache']\n const allowWrite = args['allow-write']\n const allowAll = args['allow-all']\n\n await startServer({ port, host, configPath, noCache, allowWrite, allowAll })\n } catch (error) {\n clack.log.error(styleText('red', 'Failed to start agent server'))\n console.error(error)\n process.exit(1)\n }\n },\n})\n\nexport default command\n"],"mappings":";;;;;;;;;;;;;AASA,MAAM,OAAO;CACX,QAAQ;EACN,MAAM;EACN,aAAa;EACb,OAAO;EACR;CACD,MAAM;EACJ,MAAM;EACN,aAAa;EACb,OAAO;EACR;CACD,MAAM;EACJ,MAAM;EACN,aAAa;EACb,SAAS;EACV;CACD,YAAY;EACV,MAAM;EACN,aAAa;EACb,SAAS;EACV;CACD,eAAe;EACb,MAAM;EACN,aAAa;EACb,SAAS;EACV;CACD,aAAa;EACX,MAAM;EACN,aAAa;EACb,SAAS;EACV;CACF;AAWD,eAAe,YAAY,EAAE,MAAM,MAAM,YAAY,SAAS,YAAY,YAA6C;AACrH,KAAI;AAEF,MAAI;AACF,gBAAQ,aAAa;UACf;EAMR,MAAM,8CAD0B,QAAQ,2BAA2B,CACpB;EAC/C,MAAM,WAAWA,kBAAK,QAAQ,aAAa;EAC3C,MAAM,aAAaA,kBAAK,KAAK,UAAU,WAAW,UAAU,YAAY;EAGxE,MAAM,OAAOC,aAAQ,IAAI,SAAS,SAAS,IAAI,SAAS,OAAO,KAAK;EACpE,MAAM,OAAOA,aAAQ,IAAI,QAAQ,QAAQ;EAGzC,MAAM,kBAAkBA,aAAQ,IAAI,mBAAmBA,aAAQ,KAAK;EACpE,MAAM,oBAAoBA,aAAQ,IAAI,qBAAqB,cAAc;EACzE,MAAM,sBAAsB,UAAU,SAAS;EAC/C,MAAM,yBAAyB,YAAY,aAAa,SAAUA,aAAQ,IAAI,0BAA0B;EACxG,MAAM,uBAAuB,WAAW,SAAUA,aAAQ,IAAI,wBAAwB;EAMtF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