1
1
import { DMMF } from '@prisma/generator-helper' ;
2
- import { PluginError , PluginOptions , createProject , getDataModels , saveProject } from '@zenstackhq/sdk' ;
2
+ import {
3
+ PluginError ,
4
+ PluginOptions ,
5
+ createProject ,
6
+ getDataModels ,
7
+ requireOption ,
8
+ resolvePath ,
9
+ saveProject ,
10
+ } from '@zenstackhq/sdk' ;
3
11
import { DataModel , Model } from '@zenstackhq/sdk/ast' ;
4
12
import { paramCase } from 'change-case' ;
5
13
import fs from 'fs' ;
6
14
import { lowerCaseFirst } from 'lower-case-first' ;
7
15
import path from 'path' ;
8
16
import { Project , SourceFile , VariableDeclarationKind } from 'ts-morph' ;
9
17
import { upperCaseFirst } from 'upper-case-first' ;
18
+ import { name } from '.' ;
10
19
11
20
const supportedTargets = [ 'react' , 'svelte' ] ;
12
21
type TargetFramework = ( typeof supportedTargets ) [ number ] ;
13
22
14
23
export async function generate ( model : Model , options : PluginOptions , dmmf : DMMF . Document ) {
15
- let outDir = options . output as string ;
16
- if ( ! outDir ) {
17
- throw new PluginError ( '"output" option is required' ) ;
18
- }
19
-
20
- if ( ! path . isAbsolute ( outDir ) ) {
21
- // output dir is resolved relative to the schema file path
22
- outDir = path . join ( path . dirname ( options . schemaPath ) , outDir ) ;
23
- }
24
+ let outDir = requireOption < string > ( options , 'output' ) ;
25
+ outDir = resolvePath ( outDir , options ) ;
24
26
25
27
const project = createProject ( ) ;
26
28
const warnings : string [ ] = [ ] ;
27
29
const models = getDataModels ( model ) ;
28
30
29
- const target = options . target as string ;
30
- if ( ! target ) {
31
- throw new PluginError ( `"target" option is required, supported values: ${ supportedTargets . join ( ', ' ) } ` ) ;
32
- }
31
+ const target = requireOption < string > ( options , 'target' ) ;
33
32
if ( ! supportedTargets . includes ( target ) ) {
34
- throw new PluginError ( `Unsupported target "${ target } ", supported values: ${ supportedTargets . join ( ', ' ) } ` ) ;
33
+ throw new PluginError (
34
+ options . name ,
35
+ `Unsupported target "${ target } ", supported values: ${ supportedTargets . join ( ', ' ) } `
36
+ ) ;
35
37
}
36
38
37
39
generateIndex ( project , outDir , models ) ;
@@ -198,7 +200,7 @@ function generateMutationHook(
198
200
break ;
199
201
200
202
default :
201
- throw new PluginError ( `Unsupported target "${ target } "` ) ;
203
+ throw new PluginError ( name , `Unsupported target "${ target } "` ) ;
202
204
}
203
205
204
206
func . addStatements ( 'return mutation;' ) ;
@@ -395,7 +397,7 @@ function generateHelper(target: TargetFramework, project: Project, outDir: strin
395
397
srcFile = path . join ( __dirname , './res/svelte/helper.ts' ) ;
396
398
break ;
397
399
default :
398
- throw new PluginError ( `Unsupported target: ${ target } ` ) ;
400
+ throw new PluginError ( name , `Unsupported target: ${ target } ` ) ;
399
401
}
400
402
401
403
// merge content of `shared.ts`, `helper.ts` and `marshal-?.ts`
@@ -418,7 +420,7 @@ function makeGetContext(target: TargetFramework) {
418
420
case 'svelte' :
419
421
return `const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);` ;
420
422
default :
421
- throw new PluginError ( `Unsupported target "${ target } "` ) ;
423
+ throw new PluginError ( name , `Unsupported target "${ target } "` ) ;
422
424
}
423
425
}
424
426
@@ -441,7 +443,7 @@ function makeBaseImports(target: TargetFramework) {
441
443
...shared ,
442
444
] ;
443
445
default :
444
- throw new PluginError ( `Unsupported target: ${ target } ` ) ;
446
+ throw new PluginError ( name , `Unsupported target: ${ target } ` ) ;
445
447
}
446
448
}
447
449
@@ -452,7 +454,7 @@ function makeQueryOptions(target: string, returnType: string) {
452
454
case 'svelte' :
453
455
return `QueryOptions<${ returnType } >` ;
454
456
default :
455
- throw new PluginError ( `Unsupported target: ${ target } ` ) ;
457
+ throw new PluginError ( name , `Unsupported target: ${ target } ` ) ;
456
458
}
457
459
}
458
460
@@ -463,6 +465,6 @@ function makeMutationOptions(target: string, returnType: string, argsType: strin
463
465
case 'svelte' :
464
466
return `MutationOptions<${ returnType } , unknown, ${ argsType } >` ;
465
467
default :
466
- throw new PluginError ( `Unsupported target: ${ target } ` ) ;
468
+ throw new PluginError ( name , `Unsupported target: ${ target } ` ) ;
467
469
}
468
470
}
0 commit comments