import chalk from 'chalk'; import path from 'node:path'; import { checkValidProductModuleDirectory } from '../helpers/check-valid-product-module-directory'; import { RootConfigWrite } from '../domain/root-config'; import { DocsFileName, TopLevelDirectoryName } from '../domain/file-names'; import { writeFile } from '../helpers/write-file'; import { getApiDocsPrompt } from '../helpers/get-api-docs-prompt'; import { getSandboxHost } from '../helpers/get-sandbox-host'; import { runWithSpinner } from '../helpers/spinner'; export const generateApiDocsPrompt = async (params: { apiKey: string; productModuleConfig: RootConfigWrite }) => { const { apiKey, productModuleConfig } = params; const productModuleDirectory = path.join('./'); const docsDirPath = path.join(productModuleDirectory, TopLevelDirectoryName.Docs); checkValidProductModuleDirectory({ productModuleConfig }); const { host, productModuleKey } = productModuleConfig; // Fetch the Api Docs Prompt from Root let { prompt }: { prompt: string } = await runWithSpinner('Fetching API docs prompt from Root...', () => getApiDocsPrompt({ apiKey, host }), ); const sandboxHost = getSandboxHost(host); prompt = prompt.replaceAll('[productModuleKey]', productModuleKey); // The server prompt uses [host] in multiple places as the sandbox URL for example curl calls. // Use a global regex so every occurrence is substituted, not just the first. prompt = prompt.replaceAll('[host]', sandboxHost); // Write the API docs (preserve format and structure exactly as provided) const promptPath = path.join(docsDirPath, DocsFileName.ApiDocsPrompt); await writeFile(promptPath, prompt); console.log( [ '\n' + chalk.green(`The [${productModuleKey}] Open API Docs prompt has been written to '${promptPath}'.`), '', `Tip: Feed ${chalk.yellow(promptPath)} to your AI assistant to generate the swagger`, ` spec at ${chalk.yellow(path.join(docsDirPath, DocsFileName.ApiDocs))}.`, ].join('\n'), ); };