|
| 1 | +#!/usr/bin/env node |
| 2 | +/* |
| 3 | + * Copyright (c) 2021-2023 Datalayer, Inc. |
| 4 | + * |
| 5 | + * MIT License |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Validation script to ensure tool definitions and operations stay in sync. |
| 10 | + * |
| 11 | + * Checks: |
| 12 | + * 1. Every definition has a corresponding operation |
| 13 | + * 2. Every operation has a corresponding definition |
| 14 | + * 3. Tool names match between definition and operation |
| 15 | + * 4. No orphaned files |
| 16 | + * |
| 17 | + * Usage: npm run validate:tools |
| 18 | + */ |
| 19 | + |
| 20 | +const fs = require('fs'); |
| 21 | +const path = require('path'); |
| 22 | + |
| 23 | +const TOOLS_DIR = path.join(__dirname, '../src/tools'); |
| 24 | +const DEFINITIONS_DIR = path.join(TOOLS_DIR, 'definitions'); |
| 25 | +const OPERATIONS_DIR = path.join(TOOLS_DIR, 'operations'); |
| 26 | + |
| 27 | +function getToolFiles(dir) { |
| 28 | + const files = fs.readdirSync(dir); |
| 29 | + return files |
| 30 | + .filter(f => f.endsWith('.ts') && f !== 'index.ts') |
| 31 | + .map(f => ({ |
| 32 | + name: f.replace('.ts', ''), |
| 33 | + path: path.join(dir, f), |
| 34 | + })); |
| 35 | +} |
| 36 | + |
| 37 | +function extractToolName(filePath) { |
| 38 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 39 | + |
| 40 | + // For definitions: export const xxxTool |
| 41 | + const defMatch = content.match(/export const (\w+)Tool:/); |
| 42 | + if (defMatch) { |
| 43 | + return defMatch[1]; // e.g., "insertBlock" from "insertBlockTool" |
| 44 | + } |
| 45 | + |
| 46 | + // For operations: export const xxxOperation |
| 47 | + const opMatch = content.match(/export const (\w+)Operation:/); |
| 48 | + if (opMatch) { |
| 49 | + return opMatch[1]; // e.g., "insertBlock" from "insertBlockOperation" |
| 50 | + } |
| 51 | + |
| 52 | + return null; |
| 53 | +} |
| 54 | + |
| 55 | +function main() { |
| 56 | + console.log( |
| 57 | + '🔍 Validating lexical tool definitions and operations sync...\n', |
| 58 | + ); |
| 59 | + |
| 60 | + const definitions = getToolFiles(DEFINITIONS_DIR); |
| 61 | + const operations = getToolFiles(OPERATIONS_DIR); |
| 62 | + |
| 63 | + console.log(`Found ${definitions.length} definitions:`); |
| 64 | + definitions.forEach(d => console.log(` - ${d.name}`)); |
| 65 | + console.log(); |
| 66 | + |
| 67 | + console.log(`Found ${operations.length} operations:`); |
| 68 | + operations.forEach(o => console.log(` - ${o.name}`)); |
| 69 | + console.log(); |
| 70 | + |
| 71 | + let hasErrors = false; |
| 72 | + |
| 73 | + // Check 1: File name alignment |
| 74 | + const defNames = new Set(definitions.map(d => d.name)); |
| 75 | + const opNames = new Set(operations.map(o => o.name)); |
| 76 | + |
| 77 | + // Find definitions without operations |
| 78 | + const missingOps = definitions.filter(d => !opNames.has(d.name)); |
| 79 | + if (missingOps.length > 0) { |
| 80 | + console.error('❌ Definitions without matching operations:'); |
| 81 | + missingOps.forEach(d => console.error(` ${d.name}`)); |
| 82 | + hasErrors = true; |
| 83 | + } |
| 84 | + |
| 85 | + // Find operations without definitions |
| 86 | + const missingDefs = operations.filter(o => !defNames.has(o.name)); |
| 87 | + if (missingDefs.length > 0) { |
| 88 | + console.error('❌ Operations without matching definitions:'); |
| 89 | + missingDefs.forEach(o => console.error(` ${o.name}`)); |
| 90 | + hasErrors = true; |
| 91 | + } |
| 92 | + |
| 93 | + // Check 2: Tool name extraction from file content |
| 94 | + console.log('\n📝 Checking exported names...'); |
| 95 | + for (const def of definitions) { |
| 96 | + const toolName = extractToolName(def.path); |
| 97 | + if (!toolName) { |
| 98 | + console.error(`❌ Could not extract tool name from ${def.name}`); |
| 99 | + hasErrors = true; |
| 100 | + } else if (toolName !== def.name) { |
| 101 | + console.error( |
| 102 | + `❌ File name mismatch: ${def.name}.ts exports ${toolName}Tool`, |
| 103 | + ); |
| 104 | + hasErrors = true; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + for (const op of operations) { |
| 109 | + const toolName = extractToolName(op.path); |
| 110 | + if (!toolName) { |
| 111 | + console.error(`❌ Could not extract tool name from ${op.name}`); |
| 112 | + hasErrors = true; |
| 113 | + } else if (toolName !== op.name) { |
| 114 | + console.error( |
| 115 | + `❌ File name mismatch: ${op.name}.ts exports ${toolName}Operation`, |
| 116 | + ); |
| 117 | + hasErrors = true; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Summary |
| 122 | + console.log('\n' + '='.repeat(60)); |
| 123 | + if (hasErrors) { |
| 124 | + console.error('❌ Validation FAILED - lexical tools are out of sync!'); |
| 125 | + process.exit(1); |
| 126 | + } else { |
| 127 | + console.log('✅ Validation PASSED - all lexical tools are in sync!'); |
| 128 | + console.log(`\n ${definitions.length} definition(s)`); |
| 129 | + console.log(` ${operations.length} operation(s)`); |
| 130 | + console.log(' Perfect 1:1 alignment! 🎉'); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +main(); |
0 commit comments