()
| 120 | } |
| 121 | |
| 122 | async function main(): Promise<void> { |
| 123 | const args = process.argv.slice(2); |
| 124 | const entryPath = args[0]; |
| 125 | if (!entryPath) { |
| 126 | console.error( |
| 127 | "Usage: generate-worker <entryPath> [exportName] [--json-schema] [--prompt-options <name>]", |
| 128 | ); |
| 129 | process.exit(1); |
| 130 | } |
| 131 | |
| 132 | const jsonSchema = args.includes("--json-schema"); |
| 133 | const promptOptionsIdx = args.indexOf("--prompt-options"); |
| 134 | const promptOptionsName = promptOptionsIdx !== -1 ? args[promptOptionsIdx + 1] : undefined; |
| 135 | const reserved = new Set(["--json-schema", "--prompt-options"]); |
| 136 | if (promptOptionsName) reserved.add(promptOptionsName); |
| 137 | const exportName = args.find( |
| 138 | (a, i) => a !== entryPath && !reserved.has(a) && !(i > 0 && args[i - 1] === "--prompt-options"), |
| 139 | ); |
| 140 | |
| 141 | const bundleDir = fs.mkdtempSync(path.join(os.tmpdir(), "openui-generate-")); |
| 142 | const bundlePath = path.join(bundleDir, "entry.cjs"); |
| 143 | |
| 144 | let mod: Record<string, unknown> | undefined; |
| 145 | let importError: unknown; |
| 146 | try { |
| 147 | await esbuild.build({ |
| 148 | absWorkingDir: process.cwd(), |
| 149 | bundle: true, |
| 150 | entryPoints: [entryPath], |
| 151 | format: "cjs", |
| 152 | outfile: bundlePath, |
| 153 | platform: "node", |
| 154 | plugins: [createAssetStubPlugin()], |
| 155 | sourcemap: "inline", |
| 156 | target: "node18", |
| 157 | write: true, |
| 158 | }); |
| 159 | |
| 160 | mod = require(bundlePath) as Record<string, unknown>; |
| 161 | } catch (err) { |
| 162 | importError = err; |
| 163 | } finally { |
| 164 | fs.rmSync(bundleDir, { force: true, recursive: true }); |
| 165 | } |
| 166 | |
| 167 | if (!mod) { |
| 168 | console.error(`Error: Failed to import ${entryPath}`); |
| 169 | console.error(importError instanceof Error ? importError.message : importError); |
| 170 | process.exit(1); |
| 171 | } |
| 172 | |
| 173 | const library = findLibrary(mod, exportName); |
| 174 | |
| 175 | if (!library) { |
| 176 | const exports = Object.keys(mod).join(", "); |
| 177 | console.error( |
| 178 | `Error: No Library export found.\n` + |
| 179 | `Found exports: ${exports || "(none)"}\n` + |
no test coverage detected