@apistudio/apim-cli
Version:
CLI for API Management Products
129 lines (103 loc) • 3.75 kB
JavaScript
// Script to update smith-ruleset.ts after generating recursive rulesets
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-ruleset.ts file
const smithRulesetPath = path.resolve(__dirname, '../wmgw-smith-ruleset.ts');
// Function to get all ruleset files in the spectral-rulesets directory
function getRulesetFiles() {
const rulesetDir = __dirname;
const files = fs.readdirSync(rulesetDir);
return files
.filter(file => file.endsWith('.ruleset.js'))
.map(file => file.replace('.js', ''));
}
// Function to generate the import statements
function generateImports(rulesetFiles) {
return rulesetFiles
.map(file => {
// Convert filename to a valid variable name by replacing dots with underscores
const varName = file.replace(/\./g, '_') + 'Ruleset';
return `import ${varName} from './spectral-rulesets/${file}.js';`;
})
.join('\n');
}
// Function to generate the combined ruleset object
function generateCombinedRuleset(rulesetFiles) {
return rulesetFiles
.map(file => {
// Convert filename to a valid variable name by replacing dots with underscores
const varName = file.replace(/\./g, '_') + 'Ruleset';
return ` "api.ibm.com_v1_${file}.yaml": toPlainObject(${varName})`;
})
.join(',\n');
}
// Function to generate the complete smith-ruleset.ts content
function generateSmithRulesetContent(imports, combinedRuleset) {
return `// Combined ruleset data for all schema components (TypeScript version)
// Import all ruleset files statically
${imports}
// Define types for ruleset structure
export interface RuleDefinition {
description: string;
severity: string; // Changed from enum to string to match the actual data
given: string;
resolved?: boolean;
then: {
field?: string;
function: string;
functionOptions?: {
schema?: any;
match?: string;
};
};
}
export interface Ruleset {
rules: Record<string, RuleDefinition>;
}
export interface CombinedRuleset {
[]: Ruleset | Record<string, any>;
}
// Helper function to convert imported module to plain JSON object
function toPlainObject(obj: any): Record<string, any> {
return JSON.parse(JSON.stringify(obj));
}
// Create the combined ruleset object with plain JSON objects
const combinedRuleset: CombinedRuleset = {
${combinedRuleset}
};
// Function to get the combined ruleset
export function getCombinedRuleset(): CombinedRuleset {
return combinedRuleset;
}
// Export the combined ruleset directly
export default combinedRuleset;
// Auto-generated by update-smith-ruleset.js on ${new Date().toISOString()}
`;
}
// Main function to update the smith-ruleset.ts file
function updateSmithRuleset() {
try {
// Get all ruleset files
const rulesetFiles = getRulesetFiles();
console.log(`Found ${rulesetFiles.length} ruleset files`);
// Generate the imports and combined ruleset
const imports = generateImports(rulesetFiles);
const combinedRuleset = generateCombinedRuleset(rulesetFiles);
// Generate the complete content
const content = generateSmithRulesetContent(imports, combinedRuleset);
// Write the content to the smith-ruleset.ts file
fs.writeFileSync(smithRulesetPath, content);
console.log(`Updated ${smithRulesetPath} with plain JSON objects`);
} catch (error) {
console.error('Error updating smith-ruleset.ts:', error);
}
}
// Run the update function
updateSmithRuleset();
// Export the update function for use in other scripts
export default updateSmithRuleset;
// Made with Bob