@apistudio/apim-cli
Version:
CLI for API Management Products
78 lines (59 loc) • 2.6 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// Get the directory name in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Path to the combined-source.json file
const combinedSourcePath = path.resolve(__dirname, './combined-source.json');
// Path for the output defaultVersion.json file
const outputFilePath = path.resolve(__dirname, './defaultVersion.json');
// Main function to generate the defaultVersion.json
async function generateDefaultVersions() {
try {
console.log(`Reading combined-source.json from: ${combinedSourcePath}`);
// Read the combined-source.json file
const combinedSourceContent = fs.readFileSync(combinedSourcePath, 'utf8');
// Parse the JSON content
const combinedSource = JSON.parse(combinedSourceContent);
// Create the result object for defaultVersion.json
const defaultVersions = {};
// Process each schema in combined-source.json
for (const [key, schema] of Object.entries(combinedSource)) {
// Skip special schemas like AssetDiscriminator and KindEnums
if (key.includes('-v1_')) {
continue;
}
// Extract apiVersion and kind from the key (format: apiVersion_kind.json)
const keyParts = key.split('_');
if (keyParts.length < 2) continue;
// Get the kind from the schema
let kind = '';
if (schema.properties && schema.properties.kind && schema.properties.kind.enum) {
kind = schema.properties.kind.enum[0];
}
// Skip if kind is empty
if (!kind) continue;
// Get the apiVersion from the schema
let apiVersion = '';
if (schema.properties && schema.properties.apiVersion && schema.properties.apiVersion.default) {
apiVersion = schema.properties.apiVersion.default;
} else {
// If no default apiVersion in schema, extract from the key
apiVersion = keyParts[0].replace(/_/g, '/');
}
// Skip if apiVersion is empty
if (!apiVersion) continue;
// Add to result object
defaultVersions[kind] = apiVersion;
}
// Write the defaultVersion.json file
fs.writeFileSync(outputFilePath, JSON.stringify(defaultVersions, null, 2));
console.log(`Successfully generated defaultVersion.json with ${Object.keys(defaultVersions).length} entries`);
} catch (error) {
console.error('Error generating defaultVersion.json:', error);
}
}
// Run the main function
generateDefaultVersions();
// Made with Bob