@@ -17,31 +17,61 @@ export async function generateResourceFile({
1717 dataSource = "maindb" ,
1818 resourcesDir = "resources"
1919} ) {
20- const fileName = `${ table } .ts` ;
21- const filePath = path . resolve ( process . cwd ( ) , resourcesDir , fileName ) ;
20+ const baseFileName = `${ table } .ts` ;
21+ const baseFilePath = path . resolve ( process . cwd ( ) , resourcesDir , baseFileName ) ;
2222
23- if ( fsSync . existsSync ( filePath ) ) {
24- console . log ( chalk . yellow ( `⚠️ File already exists: ${ filePath } ` ) ) ;
25- return { alreadyExists : true , path : filePath } ;
23+ if ( fsSync . existsSync ( baseFilePath ) ) {
24+ const content = await fs . readFile ( baseFilePath , "utf-8" ) ;
25+ const match = content . match ( / d a t a S o u r c e : \s * [ " ' ] ( .+ ?) [ " ' ] / ) ;
26+ const existingDataSource = match ?. [ 1 ] ;
27+ console . log ( existingDataSource , "123444444444" ) ;
28+ if ( existingDataSource === dataSource ) {
29+ console . log ( chalk . yellow ( `⚠️ File already exists with same dataSource: ${ baseFilePath } ` ) ) ;
30+ return { alreadyExists : true , path : baseFilePath , fileName : baseFileName , resourceId : table } ;
31+ } else {
32+ const suffixedFileName = `${ table } _${ dataSource } .ts` ;
33+ const suffixedFilePath = path . resolve ( process . cwd ( ) , resourcesDir , suffixedFileName ) ;
34+ return await writeResourceFile ( suffixedFilePath , suffixedFileName , {
35+ table,
36+ columns,
37+ dataSource,
38+ resourceId : `${ table } _${ dataSource } ` ,
39+ } ) ;
40+ }
2641 }
42+
43+ return await writeResourceFile ( baseFilePath , baseFileName , {
44+ table,
45+ columns,
46+ dataSource,
47+ resourceId : table ,
48+ } ) ;
49+ }
50+
51+ async function writeResourceFile ( filePath , fileName , {
52+ table,
53+ columns,
54+ dataSource,
55+ resourceId,
56+ } ) {
2757 const __filename = fileURLToPath ( import . meta. url ) ;
2858 const __dirname = path . dirname ( __filename ) ;
2959 const templatePath = path . resolve ( __dirname , "templates/resource.ts.hbs" ) ;
3060 console . log ( chalk . dim ( `Using template: ${ templatePath } ` ) ) ;
61+
3162 const context = {
3263 table,
3364 dataSource,
34- resourceId : table ,
65+ resourceId,
3566 label : table . charAt ( 0 ) . toUpperCase ( ) + table . slice ( 1 ) ,
3667 columns,
3768 } ;
3869
3970 const content = await renderHBSTemplate ( templatePath , context ) ;
40-
4171 await fs . mkdir ( path . dirname ( filePath ) , { recursive : true } ) ;
4272 await fs . writeFile ( filePath , content , "utf-8" ) ;
4373
4474 console . log ( chalk . green ( `✅ Generated resource file: ${ filePath } ` ) ) ;
4575
46- return { alreadyExists : false , path : filePath } ;
76+ return { alreadyExists : false , path : filePath , fileName , resourceId } ;
4777}
0 commit comments