@@ -3,59 +3,74 @@ const path = require('path');
33const { info, success, error } = require ( './logger' ) ;
44
55function parseMDC ( content ) {
6- const [ frontmatter , ...sections ] = content . split ( '---' ) ;
6+ const parts = content . split ( '---' ) ;
7+ // Remove empty strings and trim each part
8+ const cleanParts = parts . filter ( Boolean ) . map ( part => part . trim ( ) ) ;
9+
10+ // First part is frontmatter, rest is content
711 return {
8- frontmatter : frontmatter . trim ( ) ,
9- content : sections . join ( '---' ) . trim ( )
12+ frontmatter : cleanParts [ 0 ] || '' ,
13+ content : cleanParts . slice ( 1 ) . join ( '\n\n' )
1014 } ;
1115}
1216
13- function mergeMDCFiles ( baseContent , roleContent , role ) {
17+ function mergeMDCFiles ( baseContent , roleContent , roles ) {
1418 const base = parseMDC ( baseContent ) ;
1519 const roleMDC = parseMDC ( roleContent ) ;
1620
1721 // Merge frontmatter
1822 const mergedFrontmatter = `---
19- description: Merged rules for ${ role }
23+ description: Merged rules for ${ roles . join ( ' and ' ) }
2024globs: ["**/*.{js,jsx,ts,tsx}"]
2125alwaysApply: false
2226---` ;
2327
2428 // Merge content
25- const mergedContent = `${ base . content } \n\n## ${ role } Rules\n${ roleMDC . content } ` ;
29+ const mergedContent = `${ base . content } \n\n## ${ roles [ 1 ] } Rules\n${ roleMDC . content } ` ;
2630
27- return `${ mergedFrontmatter } \n${ mergedContent } ` ;
31+ return `${ mergedFrontmatter } \n\n ${ mergedContent } ` ;
2832}
2933
3034function mergeConfigs ( role ) {
3135 const projectRoot = process . cwd ( ) ;
3236 const baseRulesPath = path . join ( projectRoot , '.cursor/rules' ) ;
3337 const roleRulesPath = path . join ( baseRulesPath , `${ role } .mdc` ) ;
3438
35- // Get all existing MDC files
36- const existingFiles = fs . readdirSync ( baseRulesPath )
37- . filter ( file => file . endsWith ( '.mdc' ) && ! file . includes ( 'merged' ) )
38- . map ( file => path . basename ( file , '.mdc' ) ) ;
39-
40- // Create merged filename
41- const mergedFileName = [ ...existingFiles , role ] . sort ( ) . join ( '-' ) + '-merged.mdc' ;
42- const mergedRulesPath = path . join ( baseRulesPath , mergedFileName ) ;
43-
4439 try {
45- // Read base MDC file (first existing file)
46- const baseFile = existingFiles [ 0 ] ;
47- const baseContent = fs . readFileSync ( path . join ( baseRulesPath , `${ baseFile } .mdc` ) , 'utf8' ) ;
40+ // Get all existing MDC files
41+ const existingFiles = fs . readdirSync ( baseRulesPath )
42+ . filter ( file => file . endsWith ( '.mdc' ) && ! file . includes ( 'merged' ) )
43+ . map ( file => path . basename ( file , '.mdc' ) ) ;
44+
45+ // Remove the current role if it's in the list
46+ const otherRoles = existingFiles . filter ( r => r !== role ) ;
4847
49- // Read role MDC file
50- const roleContent = fs . readFileSync ( roleRulesPath , 'utf8' ) ;
48+ if ( otherRoles . length === 0 ) {
49+ error ( 'No other roles found to merge with' ) ;
50+ return false ;
51+ }
5152
52- // Merge MDC files
53- const mergedContent = mergeMDCFiles ( baseContent , roleContent , role ) ;
54- fs . writeFileSync ( mergedRulesPath , mergedContent ) ;
53+ // For each other role, create a merged file
54+ for ( const otherRole of otherRoles ) {
55+ // Create merged filename (alphabetically ordered)
56+ const roles = [ otherRole , role ] . sort ( ) ;
57+ const mergedFileName = `${ roles . join ( '-' ) } -merged.mdc` ;
58+ const mergedRulesPath = path . join ( baseRulesPath , mergedFileName ) ;
5559
56- success ( 'Merged configurations successfully!' ) ;
57- info ( `\nCreated merged file: ${ path . relative ( projectRoot , mergedRulesPath ) } ` ) ;
60+ // Read base MDC file
61+ const baseContent = fs . readFileSync ( path . join ( baseRulesPath , `${ otherRole } .mdc` ) , 'utf8' ) ;
62+
63+ // Read role MDC file
64+ const roleContent = fs . readFileSync ( roleRulesPath , 'utf8' ) ;
65+
66+ // Merge MDC files
67+ const mergedContent = mergeMDCFiles ( baseContent , roleContent , roles ) ;
68+ fs . writeFileSync ( mergedRulesPath , mergedContent ) ;
5869
70+ info ( `Created merged file: ${ path . relative ( projectRoot , mergedRulesPath ) } ` ) ;
71+ }
72+
73+ success ( 'Merged configurations successfully!' ) ;
5974 return true ;
6075 } catch ( err ) {
6176 error ( `Failed to merge configurations: ${ err . message } ` ) ;
0 commit comments