@@ -8,6 +8,9 @@ interface ModelData {
8
8
[ key : string ] : any
9
9
}
10
10
11
+ /**
12
+ * Generates the Prisma schema file based on the given models and options.
13
+ */
11
14
function generatePrismaSchema ( models : ModelOptions [ ] , path : string , options : SchemaOptions ) : void {
12
15
let schema = `datasource db {
13
16
provider = "${ options . database } "
@@ -46,43 +49,45 @@ generator client {
46
49
schema += '}\n\n'
47
50
}
48
51
49
- if ( ! fs . existsSync ( `${ projectPath ( ) } /.stacks/database` ) )
50
- fs . mkdirSync ( `${ projectPath ( ) } /.stacks/database` )
52
+ if ( ! fs ?. mkdirSync ( `${ projectPath ( ) } /.stacks/database` , { recursive : true } ) ) {
53
+ console . error ( `Error creating directory: ${ projectPath ( ) } /.stacks/database` )
54
+ return
55
+ }
51
56
52
57
fs . writeFile ( path , schema , ( err ) => {
53
58
if ( err )
54
59
console . error ( `Error writing schema file: ${ err . message } ` )
55
-
56
- // console.log(`Schema file generated successfully at path: ${path}`)
60
+ else
61
+ console . log ( `Schema file generated successfully at path: ${ path } ` )
57
62
} )
58
63
}
59
64
60
- function readModelsFromFolder ( folderPath : string ) : Promise < ModelData [ ] > {
61
- return new Promise ( ( resolve , reject ) => {
62
- const models : ModelData [ ] = [ ]
63
-
64
- fs . readdir ( folderPath , ( err , files ) => {
65
- if ( err )
66
- reject ( err )
67
-
68
- const promises = files
69
- . filter ( file => file . endsWith ( '.ts' ) )
70
- . map ( ( file ) => {
71
- const filePath = `${ folderPath } /${ file } `
72
-
73
- return import ( filePath ) . then ( ( data ) => {
74
- models . push ( {
75
- name : data . default . name ,
76
- columns : data . default . fields ,
77
- } )
78
- } )
65
+ /**
66
+ * Reads the model data from the given folder path.
67
+ */
68
+ async function readModelsFromFolder ( folderPath : string ) : Promise < ModelData [ ] > {
69
+ const models : ModelData [ ] = [ ]
70
+
71
+ try {
72
+ const files = await fs . promises . readdir ( folderPath )
73
+ const promises = files
74
+ . filter ( file => file . endsWith ( '.ts' ) )
75
+ . map ( async ( file ) => {
76
+ const filePath = `${ folderPath } /${ file } `
77
+ const data = await import ( filePath )
78
+ models . push ( {
79
+ name : data . default . name ,
80
+ columns : data . default . fields ,
79
81
} )
82
+ } )
80
83
81
- Promise . all ( promises )
82
- . then ( ( ) => resolve ( models ) )
83
- . catch ( err => reject ( err ) )
84
- } )
85
- } )
84
+ await Promise . all ( promises )
85
+ }
86
+ catch ( err ) {
87
+ console . error ( `Error reading models from folder: ${ folderPath } ` , err )
88
+ }
89
+
90
+ return models
86
91
}
87
92
88
93
export {
0 commit comments