UNPKG

@apistudio/apim-cli

Version:

CLI for API Management Products

82 lines (64 loc) 2.71 kB
// Script to update smith-combinedSource.ts with values from combinedSource.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-combinedSource.ts file const smithcombinedSourcePath = path.resolve(__dirname, '../nano-smith-schemas-json.ts'); // Path to the combinedSource.json file const combinedSourceJsonPath = path.resolve(__dirname, '../../../generated/combined-source.json'); // Function to generate the smith-combinedSource.ts content function generateSmithSchemasContent(schemas) { // Convert the JSON object to a formatted string with proper indentation const schemasString = Object.entries(schemas) .map(([key, value]) => { // Pretty print the JSON with 2-space indentation, then add additional indentation for nesting const formattedJson = JSON.stringify(value, null, 2) .split('\n') .map((line, index) => index === 0 ? line : ' ' + line) .join('\n'); return ` "${key}": ${formattedJson}`; }) .join(',\n'); return `// Auto-generated file with default versions for all schema components // Generated from packages/inventory/generated/combined-source.json /** * Returns the default versions for all schema components * @returns Record of component names to their schema objects */ export function getCombinedSource(): Record<string, any> { return { ${schemasString} }; } // Export the schemas directly export default getCombinedSource(); `; } // Main function to update the smith-combinedSource.ts file function updateSmithSchemas() { try { // Check if the combinedSource.json file exists if (!fs.existsSync(combinedSourceJsonPath)) { console.error(`Error: combinedSource.json not found at ${combinedSourceJsonPath}`); process.exit(1); } // Read the combinedSource.json file const combinedSourcesContent = fs.readFileSync(combinedSourceJsonPath, 'utf8'); const combinedSources = JSON.parse(combinedSourcesContent); // Generate the new content const content = generateSmithSchemasContent(combinedSources); // Write the content to the smith-combinedSource.ts file fs.writeFileSync(smithcombinedSourcePath, content); console.log(`Updated ${smithcombinedSourcePath} with values from combinedSource.json`); } catch (error) { console.error('Error updating smith-combinedSource.ts:', error); } } // Run the update function updateSmithSchemas(); // Export the update function for use in other scripts export default updateSmithSchemas; // Made with Bob