This refactoring successfully implemented a comprehensive plugin-based architecture for OnlyRules that makes the system more extensible, organized, and maintainable while preserving full backward compatibility.
- Created
RuleFormatSpecinterface that standardizes how rules are structured - Defined clear categories:
DIRECTORY_BASED,ROOT_FILE, andMEMORY_BASED - Established consistent metadata and configuration patterns
- Implemented
BaseRuleFormatterabstract class as the foundation - Created dedicated formatter classes for each AI IDE:
- Cursor:
.cursor/rules/*.mdcwith YAML frontmatter - GitHub Copilot:
.github/instructions/*.instructions.mdwith frontmatter - Cline:
.clinerules/*.mdwith plain markdown - Claude:
CLAUDE.md(root) and.claude/memories/*.md - Gemini:
GEMINI.md(root) and.gemini/memories/*.md - Roo:
.roo/rules/*.mdwith description headers - Legacy formats: Agents, Junie, Windsurf, Trae, Augment, Lingma
- Cursor:
.cursor/rules/- Cursor IDE rules with MDC format.github/instructions/- GitHub Copilot instructions.clinerules/- Cline assistant rules.roo/rules/- Roo Code rules
CLAUDE.md- Claude global rulesGEMINI.md- Gemini global rules
.claude/memories/- Claude memory files.gemini/memories/- Gemini memory files
- Parsing:
DefaultRuleParserhandles.mdcfiles and metadata extraction - Transformation: Format-specific content transformation in each formatter
- File Writing: Consistent directory management and file writing
- Error Handling: Comprehensive error reporting and recovery
- Factory Pattern:
DefaultRuleFormatterFactorymanages all formatters - Clear Interfaces: Well-defined contracts for all components
- Type Safety: Full TypeScript interfaces and type checking
- Easy Registration: Simple process to add new AI assistants
src/
├── core/
│ ├── interfaces.ts # Core interfaces and base classes
│ ├── parser.ts # Rule parsing logic
│ ├── factory.ts # Formatter factory and registration
│ ├── pipeline.ts # Main generation pipeline
│ ├── generator-v2.ts # New generator with backward compatibility
│ └── generator.ts # Updated to use new system with fallback
├── formatters/
│ ├── cursor.ts # Cursor IDE formatter
│ ├── copilot.ts # GitHub Copilot formatter
│ ├── cline.ts # Cline formatter
│ ├── claude-root.ts # Claude root file formatter
│ ├── claude-memories.ts # Claude memories formatter
│ ├── gemini-root.ts # Gemini root file formatter
│ ├── gemini-memories.ts # Gemini memories formatter
│ ├── roo.ts # Roo Code formatter
│ └── legacy/ # Legacy formatters
│ ├── agents.ts
│ ├── junie.ts
│ ├── windsurf.ts
│ ├── trae.ts
│ ├── augment.ts
│ ├── augment-always.ts
│ └── lingma-project.ts
└── utils/ # Existing utilities (preserved)
✅ Fully Preserved: All existing code continues to work without changes
- The
generateRules()function maintains the same API - Legacy
RuleFormatenum values are automatically mapped - Environment variable
ONLYRULES_USE_LEGACY=trueforces old behavior - Automatic fallback if new system encounters issues
// Get available formats
import { getAvailableFormats, getFormatsByCategory } from 'onlyrules';
const formats = getAvailableFormats();
const byCategory = getFormatsByCategory();// Use new pipeline directly
import { DefaultRuleGenerationPipeline } from 'onlyrules';
const pipeline = new DefaultRuleGenerationPipeline();
const results = await pipeline.execute({
input: './rules.mdc',
outputDir: './output',
formats: ['cursor', 'copilot'],
force: true
});// Add custom formatter
import { BaseRuleFormatter, RuleFormatCategory } from 'onlyrules';
class MyAIFormatter extends BaseRuleFormatter {
readonly spec = {
id: 'my-ai',
name: 'My AI Assistant',
category: RuleFormatCategory.DIRECTORY_BASED,
// ... other properties
};
// Implement required methods...
}- 🔌 Extensible: Adding new AI assistants requires only creating a formatter class
- 📁 Organized: Clear separation of concerns and logical grouping
- 🔒 Type Safe: Full TypeScript support with comprehensive interfaces
- 🧪 Testable: Each formatter can be tested independently
- 🔄 Maintainable: Consistent patterns make the codebase easier to maintain
- ⚡ Future-Proof: Architecture supports advanced features like plugin discovery
The foundation is now in place for:
- Plugin Discovery: Automatic discovery of third-party formatters
- Advanced Configuration: Per-format configuration options
- Schema Validation: Format-specific rule validation
- Template System: AI-specific rule templates
- Performance Optimization: Lazy loading and caching
- ✅ Zero Breaking Changes: Existing implementations continue to work
- ✅ Gradual Adoption: Teams can migrate to new APIs at their own pace
- ✅ Enhanced Features: New capabilities available immediately
- ✅ Better DX: Improved developer experience with better types and documentation
The refactoring includes:
- Comprehensive interfaces and type checking
- Modular architecture enabling unit testing
- Clear separation of concerns
- Example test patterns in documentation
- ARCHITECTURE.md: Complete architecture guide
- Inline documentation: Extensive JSDoc comments
- TypeScript definitions: Full type safety
- Usage examples: Practical implementation examples
This refactoring establishes OnlyRules as a robust, extensible platform for AI IDE rule generation that will scale with the evolving AI development ecosystem.