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 <tom.ratcliffe@grafana.com>
This commit is contained in:
co-authored by
Tom Ratcliffe
parent
51c18b57a2
commit
a7ace2dcdd
@@ -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:',
|
||||
|
||||
Reference in New Issue
Block a user