forked from asyncapi/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateTypesForGenerateCommand.js
More file actions
40 lines (30 loc) · 1.23 KB
/
Copy pathgenerateTypesForGenerateCommand.js
File metadata and controls
40 lines (30 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const { writeFile } = require('fs/promises');
const path = require('path');
const { listBakedInTemplates } = require('@asyncapi/generator');
async function generateClientLanguages() {
const bakedInClients = listBakedInTemplates({ type: 'client' });
const targets = Array.from(new Set(bakedInClients.map(t => t.target))).sort();
const enumEntries = targets
.map(t => ` ${capitalize(t)} = '${t}',`)
.join('\n');
const fileContent = `// Auto-generated. Do not edit manually.
export enum AvailableLanguage {
${enumEntries}
}
export const availableLanguages = [${targets.map(t => `'${t}'`).join(', ')}] as const;
export type AvailableLanguageType = typeof availableLanguages[number];
/**
* Returns the first available language as the default option.
*/
export const getDefaultLanguage = (): AvailableLanguageType => availableLanguages[0];
`;
const outputPath = path.join(__dirname, '../src/domains/models/generate/ClientLanguages.ts');
await writeFile(outputPath, fileContent);
console.log('✅ ClientLanguages.ts generated');
}
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
generateClientLanguages().catch(err => {
throw new Error(`Failed to generate ClientLanguages.ts: ${err.message}`);
});