11import { 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' ;
311import { DataModel , Model } from '@zenstackhq/sdk/ast' ;
412import { paramCase } from 'change-case' ;
513import fs from 'fs' ;
614import { lowerCaseFirst } from 'lower-case-first' ;
715import path from 'path' ;
816import { Project , SourceFile , VariableDeclarationKind } from 'ts-morph' ;
917import { upperCaseFirst } from 'upper-case-first' ;
18+ import { name } from '.' ;
1019
1120const supportedTargets = [ 'react' , 'svelte' ] ;
1221type TargetFramework = ( typeof supportedTargets ) [ number ] ;
1322
1423export 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 ) ;
2426
2527 const project = createProject ( ) ;
2628 const warnings : string [ ] = [ ] ;
2729 const models = getDataModels ( model ) ;
2830
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' ) ;
3332 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+ ) ;
3537 }
3638
3739 generateIndex ( project , outDir , models ) ;
@@ -198,7 +200,7 @@ function generateMutationHook(
198200 break ;
199201
200202 default :
201- throw new PluginError ( `Unsupported target "${ target } "` ) ;
203+ throw new PluginError ( name , `Unsupported target "${ target } "` ) ;
202204 }
203205
204206 func . addStatements ( 'return mutation;' ) ;
@@ -395,7 +397,7 @@ function generateHelper(target: TargetFramework, project: Project, outDir: strin
395397 srcFile = path . join ( __dirname , './res/svelte/helper.ts' ) ;
396398 break ;
397399 default :
398- throw new PluginError ( `Unsupported target: ${ target } ` ) ;
400+ throw new PluginError ( name , `Unsupported target: ${ target } ` ) ;
399401 }
400402
401403 // merge content of `shared.ts`, `helper.ts` and `marshal-?.ts`
@@ -418,7 +420,7 @@ function makeGetContext(target: TargetFramework) {
418420 case 'svelte' :
419421 return `const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);` ;
420422 default :
421- throw new PluginError ( `Unsupported target "${ target } "` ) ;
423+ throw new PluginError ( name , `Unsupported target "${ target } "` ) ;
422424 }
423425}
424426
@@ -441,7 +443,7 @@ function makeBaseImports(target: TargetFramework) {
441443 ...shared ,
442444 ] ;
443445 default :
444- throw new PluginError ( `Unsupported target: ${ target } ` ) ;
446+ throw new PluginError ( name , `Unsupported target: ${ target } ` ) ;
445447 }
446448}
447449
@@ -452,7 +454,7 @@ function makeQueryOptions(target: string, returnType: string) {
452454 case 'svelte' :
453455 return `QueryOptions<${ returnType } >` ;
454456 default :
455- throw new PluginError ( `Unsupported target: ${ target } ` ) ;
457+ throw new PluginError ( name , `Unsupported target: ${ target } ` ) ;
456458 }
457459}
458460
@@ -463,6 +465,6 @@ function makeMutationOptions(target: string, returnType: string, argsType: strin
463465 case 'svelte' :
464466 return `MutationOptions<${ returnType } , unknown, ${ argsType } >` ;
465467 default :
466- throw new PluginError ( `Unsupported target: ${ target } ` ) ;
468+ throw new PluginError ( name , `Unsupported target: ${ target } ` ) ;
467469 }
468470}
0 commit comments