@@ -11,7 +11,10 @@ export function generateSchemaFromJson(
11
11
prismaFilePath : string = "./prisma/schema.prisma"
12
12
) {
13
13
if ( ! verifyFilePath ( prismaFilePath ) ) {
14
- throw new Error ( "File cannot have '-' in between its name" ) ;
14
+ return {
15
+ status : false ,
16
+ message : "File cannot have '-' in between its name" ,
17
+ } ;
15
18
}
16
19
if ( fs . existsSync ( prismaFilePath ) ) {
17
20
console . log ( "File exists" ) ;
@@ -26,11 +29,13 @@ export function generateSchemaFromJson(
26
29
}
27
30
28
31
JsonChecks ( jsonData , models ) ;
29
- generateSchemaWhenFilePresent ( jsonData , prismaFilePath ) ;
32
+ const output = generateSchemaWhenFilePresent ( jsonData , prismaFilePath ) ;
33
+ return output ;
30
34
} else {
31
35
console . log ( "Applying Checks...." ) ;
32
36
JsonChecks ( jsonData , [ ] ) ;
33
- generateIfNoSchema ( jsonData ) ;
37
+ const output = generateIfNoSchema ( jsonData ) ;
38
+ return output ;
34
39
}
35
40
}
36
41
@@ -85,7 +90,9 @@ export function readJsonFile(filePath: string): Promise<Schema> {
85
90
} ) ;
86
91
}
87
92
88
- export async function generateIfNoSchema ( jsonData : Schema ) : Promise < void > {
93
+ export async function generateIfNoSchema (
94
+ jsonData : Schema
95
+ ) : Promise < { status : boolean ; message ?: string ; error ?: string } > {
89
96
const models : any [ ] = createModels ( jsonData . schema ) ;
90
97
console . log ( "Model generated" ) ;
91
98
// console.log(models);
@@ -138,7 +145,11 @@ export async function generateIfNoSchema(jsonData: Schema): Promise<void> {
138
145
fs . mkdirSync ( "./prisma" , { recursive : true } ) ;
139
146
fs . writeFile ( "./prisma/schema.prisma" , result , ( err ) => {
140
147
if ( err ) {
141
- console . error ( "Error writing Prisma schema:" , err ) ;
148
+ return {
149
+ status : false ,
150
+ message : "Error writing Prisma schema" ,
151
+ error : err ,
152
+ } ;
142
153
} else {
143
154
console . log ( "Prisma schema generated successfully!" ) ;
144
155
let migrateModels : string [ ] = jsonData . schema . map (
@@ -147,13 +158,21 @@ export async function generateIfNoSchema(jsonData: Schema): Promise<void> {
147
158
validateAndMigrate ( migrateModels ) ;
148
159
}
149
160
} ) ;
161
+
162
+ return {
163
+ status : true ,
164
+ message : "Prisma schema generated successfully!" ,
165
+ } ;
150
166
}
151
167
export async function generateSchemaWhenFilePresent (
152
168
jsonData : Schema ,
153
169
prismaFilePath : string
154
170
) {
155
171
if ( ! verifyFilePath ( prismaFilePath ) ) {
156
- throw new Error ( "File cannot have '-' in between its name" ) ;
172
+ return {
173
+ status : false ,
174
+ message : "File cannot have '-' in between its name" ,
175
+ } ;
157
176
}
158
177
const FileData = fs . readFileSync ( prismaFilePath , "utf8" ) ;
159
178
const models : any [ ] = createModels ( jsonData . schema ) ;
@@ -174,7 +193,11 @@ export async function generateSchemaWhenFilePresent(
174
193
fs . mkdirSync ( "./prisma" , { recursive : true } ) ;
175
194
fs . writeFile ( "./prisma/schema.prisma" , result , ( err ) => {
176
195
if ( err ) {
177
- console . error ( "Error writing Prisma schema:" , err ) ;
196
+ return {
197
+ status : false ,
198
+ message : "Error writing Prisma schema" ,
199
+ error : err ,
200
+ } ;
178
201
} else {
179
202
console . log ( "Prisma schema generated successfully!" ) ;
180
203
let migrateModels : string [ ] = jsonData . schema . map (
@@ -183,16 +206,24 @@ export async function generateSchemaWhenFilePresent(
183
206
validateAndMigrate ( migrateModels ) ;
184
207
}
185
208
} ) ;
209
+
210
+ return {
211
+ status : true ,
212
+ message : "Prisma schema generated successfully!" ,
213
+ } ;
186
214
}
187
215
188
216
function validateAndMigrate ( migrateModels : string [ ] ) {
189
217
// TODO: DONE RUN: npx prisma validate
190
218
exec ( "npx prisma validate" , ( error , stdout , stderr ) => {
191
219
if ( error ) {
192
- console . error ( "Error executing 'npx prisma validate':" , error ) ;
193
- return ;
220
+ return {
221
+ status : false ,
222
+ message : "Error executing 'npx prisma validate'" ,
223
+ error : error ,
224
+ } ;
194
225
}
195
- console . log ( "commands executed successfully" , stdout ) ;
226
+ return { status : true , message : "commands executed successfully" , stdout } ;
196
227
} ) ;
197
228
198
229
// TODO: DONE RUN: prisma migrate dev
@@ -203,7 +234,18 @@ function validateAndMigrate(migrateModels: string[]) {
203
234
stdio : "inherit" ,
204
235
}
205
236
) ;
237
+ shell . on ( "error" , ( err ) => {
238
+ return {
239
+ status : false ,
240
+ message : "Error executing 'npx prisma migrate dev'" ,
241
+ error : err ,
242
+ } ;
243
+ } ) ;
206
244
shell . on ( "close" , ( code ) => {
207
245
console . log ( "[shell] terminated:" , code ) ;
208
246
} ) ;
247
+ return {
248
+ status : true ,
249
+ message : "commands executed successfully" ,
250
+ } ;
209
251
}
0 commit comments