@@ -17,31 +17,61 @@ export async function generateResourceFile({
17
17
dataSource = "maindb" ,
18
18
resourcesDir = "resources"
19
19
} ) {
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 ) ;
22
22
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
+ }
26
41
}
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
+ } ) {
27
57
const __filename = fileURLToPath ( import . meta. url ) ;
28
58
const __dirname = path . dirname ( __filename ) ;
29
59
const templatePath = path . resolve ( __dirname , "templates/resource.ts.hbs" ) ;
30
60
console . log ( chalk . dim ( `Using template: ${ templatePath } ` ) ) ;
61
+
31
62
const context = {
32
63
table,
33
64
dataSource,
34
- resourceId : table ,
65
+ resourceId,
35
66
label : table . charAt ( 0 ) . toUpperCase ( ) + table . slice ( 1 ) ,
36
67
columns,
37
68
} ;
38
69
39
70
const content = await renderHBSTemplate ( templatePath , context ) ;
40
-
41
71
await fs . mkdir ( path . dirname ( filePath ) , { recursive : true } ) ;
42
72
await fs . writeFile ( filePath , content , "utf-8" ) ;
43
73
44
74
console . log ( chalk . green ( `✅ Generated resource file: ${ filePath } ` ) ) ;
45
75
46
- return { alreadyExists : false , path : filePath } ;
76
+ return { alreadyExists : false , path : filePath , fileName , resourceId } ;
47
77
}
0 commit comments