@neurolint/cli
Version:
NeuroLint CLI for React/Next.js modernization with advanced 6-layer orchestration and intelligent AST transformations
290 lines (250 loc) • 8.07 kB
JavaScript
/**
* Orchestration Engine Integration for CLI
* Bridges the robust Phase 1 orchestration system with the existing CLI
*/
const path = require('path');
const fs = require('fs-extra');
/**
* Enhanced orchestration engine that integrates with the existing CLI
*/
class OrchestrationEngine {
constructor(options = {}) {
this.verbose = options.verbose || false;
this.dryRun = options.dryRun || false;
this.useAdvancedOrchestration = options.useAdvancedOrchestration !== false;
}
/**
* Initialize orchestration system by loading the fix-master.js
*/
async initialize() {
try {
// Try to load the orchestration system from parent directory
const fixMasterPath = path.resolve(__dirname, '../../fix-master.js');
if (await fs.pathExists(fixMasterPath)) {
this.orchestrationSystem = require(fixMasterPath);
this.hasOrchestration = true;
if (this.verbose) {
console.log('Advanced orchestration system loaded');
}
} else {
this.hasOrchestration = false;
if (this.verbose) {
console.log('Using legacy engine (orchestration system not found)');
}
}
} catch (error) {
this.hasOrchestration = false;
if (this.verbose) {
console.log('Fallback to legacy engine:', error.message);
}
}
}
/**
* Run enhanced analysis with orchestration system
*/
async runAnalysis(targetPath, options = {}) {
if (!this.hasOrchestration) {
return this.runLegacyAnalysis(targetPath, options);
}
try {
const { runNeuroLint } = this.orchestrationSystem;
const orchestrationOptions = {
dryRun: true, // Analysis is always dry-run
verbose: this.verbose,
layers: options.layers || [1, 2, 3, 4, 5, 6],
useCache: options.useCache !== false,
skipUnnecessary: options.skipUnnecessary !== false
};
const result = await runNeuroLint(targetPath, orchestrationOptions);
return {
success: result.success,
results: result.results || [],
summary: result.summary || {},
analysisResults: result.analysisResults || [],
orchestrationUsed: true,
performanceStats: result.summary?.performanceStats
};
} catch (error) {
if (this.verbose) {
console.warn('Orchestration analysis failed, falling back to legacy:', error.message);
}
return this.runLegacyAnalysis(targetPath, options);
}
}
/**
* Run enhanced fixes with orchestration system
*/
async runFixes(targetPath, options = {}) {
if (!this.hasOrchestration) {
return this.runLegacyFixes(targetPath, options);
}
try {
const { runNeuroLint } = this.orchestrationSystem;
const orchestrationOptions = {
dryRun: this.dryRun,
verbose: this.verbose,
layers: options.layers || [1, 2, 3, 4, 5, 6],
useCache: options.useCache !== false,
skipUnnecessary: options.skipUnnecessary !== false
};
const result = await runNeuroLint(targetPath, orchestrationOptions);
return {
success: result.success,
results: result.results || [],
summary: result.summary || {},
orchestrationUsed: true,
performanceStats: result.summary?.performanceStats,
fixesApplied: !this.dryRun
};
} catch (error) {
if (this.verbose) {
console.warn('Orchestration fixes failed, falling back to legacy:', error.message);
}
return this.runLegacyFixes(targetPath, options);
}
}
/**
* Get orchestration capabilities and status
*/
getCapabilities() {
return {
hasOrchestration: this.hasOrchestration,
features: this.hasOrchestration ? [
'Advanced 6-layer architecture',
'Algorithm-based layer selection',
'Performance optimization with caching',
'Comprehensive error recovery',
'Real-time progress tracking',
'Incremental validation',
'Pipeline state tracking'
] : [
'Basic analysis and fixes',
'Legacy transformation engine'
],
version: this.hasOrchestration ? '2.0.0' : '1.x.x'
};
}
/**
* Legacy analysis fallback
*/
async runLegacyAnalysis(targetPath, options) {
// Fallback to existing NeuroLint engine
const NeuroLintProEnhanced = require('../neurolint-pro-enhanced.js');
const files = await this.getFiles(targetPath, options);
const results = [];
for (const filePath of files) {
try {
const content = await fs.readFile(filePath, 'utf-8');
const result = await NeuroLintProEnhanced(
content,
filePath,
true, // Analysis mode
options.layers || [1, 2, 3, 4, 5, 6],
{ verbose: this.verbose }
);
if (result.analysis) {
results.push({
filePath,
...result.analysis
});
}
} catch (error) {
if (this.verbose) {
console.warn(`Failed to analyze ${filePath}:`, error.message);
}
}
}
return {
success: true,
results,
summary: {
totalFiles: results.length,
successfulFiles: results.length
},
orchestrationUsed: false
};
}
/**
* Legacy fixes fallback
*/
async runLegacyFixes(targetPath, options) {
const NeuroLintProEnhanced = require('../neurolint-pro-enhanced.js');
const files = await this.getFiles(targetPath, options);
const results = [];
for (const filePath of files) {
try {
const content = await fs.readFile(filePath, 'utf-8');
const result = await NeuroLintProEnhanced(
content,
filePath,
this.dryRun,
options.layers || [1, 2, 3, 4, 5, 6],
{ verbose: this.verbose }
);
if (result.transformedCode && result.transformedCode !== content && !this.dryRun) {
await fs.writeFile(filePath, result.transformedCode);
}
results.push({
filePath,
success: true,
fixesApplied: result.transformedCode !== content ? 1 : 0
});
} catch (error) {
results.push({
filePath,
success: false,
error: error.message
});
}
}
return {
success: results.every(r => r.success),
results,
summary: {
totalFiles: results.length,
successfulFiles: results.filter(r => r.success).length
},
orchestrationUsed: false,
fixesApplied: !this.dryRun
};
}
/**
* Get files to process
*/
async getFiles(targetPath, options) {
const { glob } = require('fast-glob');
const includePatterns = options.include ?
options.include.split(',').map(p => p.trim()) :
['**/*.{ts,tsx,js,jsx}'];
const excludePatterns = options.exclude ?
options.exclude.split(',').map(p => p.trim()) :
['**/node_modules/**', '**/dist/**', '**/.next/**'];
const files = [];
for (const pattern of includePatterns) {
const globPattern = path.join(targetPath, pattern);
const matches = await glob(globPattern, {
ignore: excludePatterns.map(p => path.join(targetPath, p))
});
files.push(...matches);
}
return [...new Set(files)];
}
/**
* Display orchestration status
*/
displayStatus() {
const capabilities = this.getCapabilities();
console.log(`\nNeuroLint Engine: ${capabilities.hasOrchestration ? 'Advanced Orchestration' : 'Legacy'}`);
console.log(`Version: ${capabilities.version}`);
console.log('Features:');
capabilities.features.forEach(feature => {
console.log(` - ${feature}`);
});
if (capabilities.hasOrchestration) {
console.log('Enhanced capabilities available!');
} else {
console.log('Upgrade to orchestration system for enhanced features');
}
}
}
module.exports = { OrchestrationEngine };