@apistudio/apim-cli
Version:
CLI for API Management Products
77 lines (57 loc) • 2.49 kB
JavaScript
// Script to update smith-defaultVersion.ts with values from defaultVersion.json
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// Get the directory name using ES modules approach
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Path to the smith-defaultVersion.ts file
const smithDefaultVersionPath = path.resolve(__dirname, '../wmgw-smith-defaultVersion.ts');
// Path to the defaultVersion.json file
const defaultVersionJsonPath = path.resolve(__dirname, '../../../generated/defaultVersion.json');
// Function to generate the smith-defaultVersion.ts content
function generateSmithDefaultVersionContent(defaultVersions) {
// Convert the JSON object to a formatted string with proper indentation
const versionsString = Object.entries(defaultVersions)
.map(([key, value]) => ` "${key}": "${value}"`)
.join(',\n');
return `// Auto-generated file with default versions for all schema components
// Generated from packages/inventory/generated/defaultVersion.json
/**
* Returns the default versions for all schema components
* @returns Record of component names to their default API versions
*/
export function getDefaultVersions(): Record<string, string> {
return {
${versionsString}
};
}
// Export the default versions directly
export default getDefaultVersions();
`;
}
// Main function to update the smith-defaultVersion.ts file
function updateSmithDefaultVersion() {
try {
// Check if the defaultVersion.json file exists
if (!fs.existsSync(defaultVersionJsonPath)) {
console.error(`Error: defaultVersion.json not found at ${defaultVersionJsonPath}`);
process.exit(1);
}
// Read the defaultVersion.json file
const defaultVersionsContent = fs.readFileSync(defaultVersionJsonPath, 'utf8');
const defaultVersions = JSON.parse(defaultVersionsContent);
// Generate the new content
const content = generateSmithDefaultVersionContent(defaultVersions);
// Write the content to the smith-defaultVersion.ts file
fs.writeFileSync(smithDefaultVersionPath, content);
console.log(`Updated ${smithDefaultVersionPath} with values from defaultVersion.json`);
} catch (error) {
console.error('Error updating smith-defaultVersion.ts:', error);
}
}
// Run the update function
updateSmithDefaultVersion();
// Export the update function for use in other scripts
export default updateSmithDefaultVersion;
// Made with Bob