@@ -327,6 +327,7 @@ interface Input {
327
327
scalarsModulePath ?: string
328
328
schemaSource : string
329
329
options ?: {
330
+ formatter ?: Formatter
330
331
TSDoc ?: {
331
332
noDocPolicy ?: 'message' | 'ignore'
332
333
}
@@ -393,11 +394,6 @@ export const generateCode = (input: Input) => {
393
394
objects : Code . objectFromEntries (
394
395
typeMapByKind . GraphQLObjectType . map ( _ => [ _ . name , Code . propertyAccess ( `Object` , _ . name ) ] ) ,
395
396
) ,
396
- // unionMemberNames: Code.objectFromEntries(
397
- // typeMapByKind.GraphQLUnionType.map(
398
- // (_) => [_.name, Code.unionItems(_.getTypes().map(_ => Code.quote(_.name)))],
399
- // ),
400
- // ),
401
397
unions : {
402
398
type : Code . objectFrom (
403
399
{
@@ -419,7 +415,6 @@ export const generateCode = (input: Input) => {
419
415
) ,
420
416
) ,
421
417
)
422
- // console.log(typeMapByKind.GraphQLScalarType)
423
418
424
419
for ( const [ name , types ] of entries ( typeMapByKind ) ) {
425
420
if ( name === `GraphQLScalarType` ) continue
@@ -441,45 +436,64 @@ export const generateCode = (input: Input) => {
441
436
442
437
let scalarsCode = ``
443
438
444
- scalarsCode += `import type * as Scalar from ${ Code . quote ( scalarsModulePath ) }
439
+ scalarsCode += `
440
+ import * as Scalar from ${ Code . quote ( scalarsModulePath ) }
445
441
446
- declare global {
447
- interface SchemaCustomScalars {
448
- Date: Date
449
- }
450
- }
442
+ declare global {
443
+ interface SchemaCustomScalars {
444
+ Date: Date
445
+ }
446
+ }
451
447
452
- ${
448
+ ${
453
449
typeMapByKind . GraphQLCustomScalarType
454
450
. map ( ( _ ) => {
455
451
return `
456
- export const ${ _ . name } = Scalar.scalar('${ _ . name } ', Scalar.nativeScalarConstructors.String)
457
- export type ${ _ . name } = typeof ${ _ . name }
452
+ export const ${ _ . name } = Scalar.scalar('${ _ . name } ', Scalar.nativeScalarConstructors.String)
453
+ export type ${ _ . name } = typeof ${ _ . name }
458
454
`
459
455
} ) . join ( `\n` )
460
456
}
461
457
458
+ export * from ${ Code . quote ( scalarsModulePath ) }
459
+ `
462
460
463
-
464
-
465
- export * from ${ Code . quote ( scalarsModulePath ) }
466
- `
461
+ const defaultDprintConfig = {
462
+ quoteStyle : `preferSingle` ,
463
+ semiColons : `asi` ,
464
+ }
467
465
468
466
return {
469
- scalars : scalarsCode ,
470
- schema : schemaCode ,
467
+ scalars : input . options ?. formatter ?. formatText ( `memory.ts` , scalarsCode , defaultDprintConfig )
468
+ ?? scalarsCode ,
469
+ schema : input . options ?. formatter ?. formatText ( `memory.ts` , schemaCode , defaultDprintConfig ) ?? schemaCode ,
471
470
}
472
471
}
473
472
473
+ import type { Formatter } from '@dprint/formatter'
474
+ import { createFromBuffer } from '@dprint/formatter'
475
+ import { getPath } from '@dprint/typescript'
474
476
export const generateFiles = async ( params : {
475
477
schemaPath : string
476
478
outputDirPath : string
477
479
schemaModulePath ?: string
478
480
scalarsModulePath ?: string
481
+ /**
482
+ * @defaultValue `true`
483
+ */
484
+ format ?: boolean
479
485
} ) => {
480
- // todo use @dprint /formatter
481
486
const schemaSource = await fs . readFile ( params . schemaPath , `utf8` )
482
- const code = generateCode ( { schemaSource, ...params } )
487
+ const options = ( params . format ?? true )
488
+ ? {
489
+ formatter : createFromBuffer ( await fs . readFile ( getPath ( ) ) ) ,
490
+ }
491
+ : undefined
492
+ const code = generateCode ( {
493
+ schemaSource,
494
+ ...params ,
495
+ options,
496
+ } )
483
497
await fs . mkdir ( params . outputDirPath , { recursive : true } )
484
498
await fs . writeFile ( `${ params . outputDirPath } /Schema.ts` , code . schema , { encoding : `utf8` } )
485
499
await fs . writeFile ( `${ params . outputDirPath } /Scalar.ts` , code . scalars , { encoding : `utf8` } )
0 commit comments