|
| 1 | +#!/usr/bin/env ts-node |
| 2 | + |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as path from 'path'; |
| 5 | +import { findMatchingFiles } from './utils'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Add x-mint configuration to OpenAPI schema |
| 9 | + */ |
| 10 | +function addMintConfig(schema: any): boolean { |
| 11 | + // Check if x-mint already exists |
| 12 | + if (schema['x-mint']) { |
| 13 | + console.warn(' ⚠️ x-mint configuration already exists, overwriting'); |
| 14 | + } |
| 15 | + |
| 16 | + // Add x-mint configuration |
| 17 | + schema['x-mint'] = { |
| 18 | + mcp: { |
| 19 | + enabled: true |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + return true; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Main execution function |
| 28 | + */ |
| 29 | +export async function main(directoryPath?: string, pattern?: string): Promise<number> { |
| 30 | + // Get command line arguments if not provided |
| 31 | + let dir = directoryPath; |
| 32 | + let pat = pattern; |
| 33 | + |
| 34 | + if (!dir || !pat) { |
| 35 | + const args = process.argv.slice(2); |
| 36 | + |
| 37 | + if (args.length < 2) { |
| 38 | + console.error('❌ Error: Missing required arguments'); |
| 39 | + console.error(''); |
| 40 | + console.error('Usage: cd .github/scripts && npm run add-mint-config -- <directory> <pattern>'); |
| 41 | + console.error(''); |
| 42 | + console.error('Arguments:'); |
| 43 | + console.error(' <directory> - Path to directory containing OpenAPI JSON files'); |
| 44 | + console.error(' <pattern> - Regex pattern to match filenames'); |
| 45 | + console.error(''); |
| 46 | + console.error('Examples:'); |
| 47 | + console.error(' cd .github/scripts && npm run add-mint-config -- "../../openapi" "openapi.*\\.json"'); |
| 48 | + return 1; |
| 49 | + } |
| 50 | + |
| 51 | + dir = args[0]; |
| 52 | + pat = args[1]; |
| 53 | + } |
| 54 | + |
| 55 | + console.log('🚀 Adding x-mint configuration to OpenAPI schemas...\n'); |
| 56 | + console.log(`Searching in directory: ${dir}`); |
| 57 | + console.log(`Pattern: ${pat}\n`); |
| 58 | + |
| 59 | + // Find matching OpenAPI files |
| 60 | + const openapiFiles = findMatchingFiles(dir, pat); |
| 61 | + |
| 62 | + if (openapiFiles.length === 0) { |
| 63 | + console.log('⚠️ No files found matching the pattern'); |
| 64 | + return 1; |
| 65 | + } |
| 66 | + |
| 67 | + console.log(`Found ${openapiFiles.length} matching file(s):\n`); |
| 68 | + openapiFiles.forEach(file => console.log(` - ${file}`)); |
| 69 | + console.log(''); |
| 70 | + |
| 71 | + const startTime = Date.now(); |
| 72 | + let filesUpdated = 0; |
| 73 | + |
| 74 | + try { |
| 75 | + for (const filePath of openapiFiles) { |
| 76 | + console.log(`📄 Processing ${path.basename(filePath)}...`); |
| 77 | + |
| 78 | + // Read and parse the schema |
| 79 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 80 | + const schema = JSON.parse(content); |
| 81 | + |
| 82 | + // Add x-mint configuration |
| 83 | + const updated = addMintConfig(schema); |
| 84 | + |
| 85 | + if (updated) { |
| 86 | + // Write back to file |
| 87 | + const updatedContent = JSON.stringify(schema, null, 2); |
| 88 | + fs.writeFileSync(filePath, updatedContent, 'utf-8'); |
| 89 | + console.log(' ✅ Added x-mint configuration'); |
| 90 | + filesUpdated++; |
| 91 | + } |
| 92 | + |
| 93 | + console.log(); |
| 94 | + } |
| 95 | + |
| 96 | + // Summary |
| 97 | + const duration = ((Date.now() - startTime) / 1000).toFixed(2); |
| 98 | + console.log('📊 Summary:'); |
| 99 | + console.log(` • Files processed: ${openapiFiles.length}`); |
| 100 | + console.log(` • Files updated: ${filesUpdated}`); |
| 101 | + console.log(` • Duration: ${duration}s`); |
| 102 | + console.log('\n✨ Done!'); |
| 103 | + |
| 104 | + return 0; |
| 105 | + } catch (error) { |
| 106 | + console.error('\n❌ Error:', error); |
| 107 | + return 1; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Run if executed directly |
| 112 | +if (require.main === module) { |
| 113 | + main() |
| 114 | + .then((exitCode) => process.exit(exitCode)) |
| 115 | + .catch((error) => { |
| 116 | + console.error('Fatal error:', error); |
| 117 | + process.exit(1); |
| 118 | + }); |
| 119 | +} |
| 120 | + |
0 commit comments