From a7ace2dcddf7a8ed48850112e862b475dff63e58 Mon Sep 17 00:00:00 2001 From: Alex Khomenko Date: Mon, 10 Nov 2025 13:43:00 +0200 Subject: [PATCH] API clients: Use open_snapshots for possibleOpenAPISpecs (#113587) * API clients: Use open_snapshots for possibleOpenAPISpecs * Tweak logic to throw error if we can't load openapi specs --------- Co-authored-by: Tom Ratcliffe --- .../src/generator/plopfile.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/grafana-api-clients/src/generator/plopfile.ts b/packages/grafana-api-clients/src/generator/plopfile.ts index ed3953bfdc7..3f40c4adc42 100644 --- a/packages/grafana-api-clients/src/generator/plopfile.ts +++ b/packages/grafana-api-clients/src/generator/plopfile.ts @@ -107,15 +107,20 @@ export default function plopGenerator(plop: NodePlopAPI) { return actions; }; - // Read files from data/openapi directory and use as an array of API clients that the user can select + const getOpenAPISpecs = (isEnterprise: boolean): string[] => { + const openapiDir = isEnterprise + ? path.join(basePath, 'pkg/extensions/apiserver/tests/openapi_snapshots') + : path.join(basePath, 'pkg/tests/apis/openapi_snapshots'); - const openapiDir = path.join(basePath, 'data/openapi'); - let possibleOpenAPISpecs: string[] = []; - try { - possibleOpenAPISpecs = fs.readdirSync(openapiDir).filter((file: string) => file.endsWith('.json')); - } catch (e) { - possibleOpenAPISpecs = []; - } + try { + const files = fs.readdirSync(openapiDir).filter((file: string) => file.endsWith('.json')); + return files; + } catch (e) { + throw new Error( + "No OpenAPI specs found! Are you trying to generate an API client for enterprise but haven't linked your local environment?" + ); + } + }; const generator: PlopGeneratorConfig = { description: 'Generate RTK Query API client for a Grafana API group', @@ -129,7 +134,9 @@ export default function plopGenerator(plop: NodePlopAPI) { { type: 'list', loop: false, - choices: possibleOpenAPISpecs, + choices: (answers: { isEnterprise?: boolean }) => { + return getOpenAPISpecs(answers.isEnterprise ?? false); + }, pageSize: 50, name: 'apiInfo', message: 'OpenAPI spec:',