|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + * |
| 5 | + * Proactive performance-based refactor tool using Cascade CLI. |
| 6 | + * Scans entire repo by default, or limited via --include=path |
| 7 | + */ |
| 8 | + |
| 9 | +const fs = require('node:fs'); |
| 10 | +const path = require('node:path'); |
| 11 | +const glob = require('fast-glob'); |
| 12 | +const { spawnSync } = require('node:child_process'); |
| 13 | + |
| 14 | +// ---------------------------- |
| 15 | +// CLI Options |
| 16 | +// ---------------------------- |
| 17 | +const args = process.argv.slice(2); |
| 18 | +const targetDir = getArgValue('--include') || '.'; // scan entire repo by default |
| 19 | +const strategy = getArgValue('--strategy') || 'performance'; |
| 20 | +const shouldApply = args.includes('--apply'); |
| 21 | +const shouldDry = args.includes('--dry'); |
| 22 | + |
| 23 | +function getArgValue(flag) { |
| 24 | + const i = args.indexOf(flag); |
| 25 | + return i >= 0 && args[i + 1] ? args[i + 1] : null; |
| 26 | +} |
| 27 | + |
| 28 | +function checkCascade() { |
| 29 | + try { |
| 30 | + const result = spawnSync('cascade', ['--version'], { encoding: 'utf-8' }); |
| 31 | + return result.status === 0; |
| 32 | + } catch { |
| 33 | + return false; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// ---------------------------- |
| 38 | +// Pattern matchers (basic) |
| 39 | +// ---------------------------- |
| 40 | +function findSuspiciousLines(content) { |
| 41 | + const lines = content.split('\n'); |
| 42 | + const matches = []; |
| 43 | + |
| 44 | + lines.forEach((line, i) => { |
| 45 | + if (line.includes('.map(') && (line.includes('.filter(') || line.includes('.reduce('))) { |
| 46 | + matches.push({ line: i + 1, content: line.trim() }); |
| 47 | + } |
| 48 | + |
| 49 | + if (line.match(/for\s*\(\s*let\s+[a-z]+\s*=\s*0;/)) { |
| 50 | + matches.push({ line: i + 1, content: line.trim() }); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + return matches; |
| 55 | +} |
| 56 | + |
| 57 | +function runCascade({ filePath, line, codeBlock }) { |
| 58 | + const prompt = `Refactor the following code to optimize runtime performance. |
| 59 | +Avoid redundant array operations or nested loops. |
| 60 | +Ensure the logic remains the same and behavior is preserved. |
| 61 | +
|
| 62 | +\`\`\`ts |
| 63 | +${codeBlock} |
| 64 | +\`\`\` |
| 65 | +`; |
| 66 | + |
| 67 | + const result = spawnSync( |
| 68 | + 'cascade', |
| 69 | + ['--prompt', prompt, '--file', filePath, '--line', String(line)], |
| 70 | + { encoding: 'utf-8' } |
| 71 | + ); |
| 72 | + |
| 73 | + return result.stdout; |
| 74 | +} |
| 75 | + |
| 76 | +// ---------------------------- |
| 77 | +// Main |
| 78 | +// ---------------------------- |
| 79 | +async function run() { |
| 80 | + const cascadeAvailable = checkCascade(); |
| 81 | + if (!cascadeAvailable) { |
| 82 | + console.error('❌ Cascade CLI not found.'); |
| 83 | + process.exit(1); |
| 84 | + } |
| 85 | + |
| 86 | + const files = await glob([`${targetDir}/**/*.ts`, `${targetDir}/**/*.tsx`], { |
| 87 | + ignore: [ |
| 88 | + '**/node_modules/**', |
| 89 | + '**/.next/**', |
| 90 | + '**/dist/**', |
| 91 | + '**/.turbo/**', |
| 92 | + '**/.git/**', |
| 93 | + '**/build/**', |
| 94 | + '**/out/**', |
| 95 | + '**/coverage/**', |
| 96 | + ], |
| 97 | + }); |
| 98 | + |
| 99 | + for (const file of files) { |
| 100 | + const fullPath = path.resolve(file); |
| 101 | + const content = fs.readFileSync(fullPath, 'utf-8'); |
| 102 | + const suspicious = findSuspiciousLines(content); |
| 103 | + |
| 104 | + if (suspicious.length === 0) continue; |
| 105 | + |
| 106 | + console.log(`🔍 Found ${suspicious.length} performance candidate(s) in ${file}`); |
| 107 | + |
| 108 | + for (const match of suspicious) { |
| 109 | + const { line, content: codeLine } = match; |
| 110 | + console.log(`⚡ Refactoring ${file}:${line}`); |
| 111 | + |
| 112 | + if (shouldDry) { |
| 113 | + console.log(`📝 Suspicious code: ${codeLine}`); |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + if (shouldApply) { |
| 118 | + const result = runCascade({ |
| 119 | + filePath: fullPath, |
| 120 | + line, |
| 121 | + codeBlock: codeLine, |
| 122 | + }); |
| 123 | + |
| 124 | + console.log(result.trim()); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + console.log('✅ Cascade performance refactor complete.'); |
| 130 | +} |
| 131 | + |
| 132 | +run(); |
0 commit comments