Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0e150e4

Browse files
committed
feat(ts-client): format generated code with dprint
1 parentc0d85b6 commit0e150e4

File tree

6 files changed

+222
-176
lines changed

6 files changed

+222
-176
lines changed

‎package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
},
8080
"dependencies": {
8181
"@dprint/formatter":"^0.2.1",
82+
"@dprint/typescript":"^0.89.3",
8283
"@graphql-typed-document-node/core":"^3.2.0",
8384
"@molt/command":"^0.9.0",
8485
"dprint":"^0.45.0",

‎pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎src/cli/generate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const args = Command.create().description(`Generate a type safe GraphQL client.`
1212
`Directory path for where to output the generated TypeScript files.`,
1313
),
1414
)
15+
.parameter(`format`,z.boolean().describe(`Format the generated files using dprint.`).default(true))
1516
.settings({
1617
parameters:{
1718
environment:false,
@@ -22,4 +23,5 @@ const args = Command.create().description(`Generate a type safe GraphQL client.`
2223
awaitgenerateFiles({
2324
outputDirPath:args.output,
2425
schemaPath:args.schema,
26+
format:args.format,
2527
})

‎src/generator/generator.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ interface Input {
327327
scalarsModulePath?:string
328328
schemaSource:string
329329
options?:{
330+
formatter?:Formatter
330331
TSDoc?:{
331332
noDocPolicy?:'message'|'ignore'
332333
}
@@ -393,11 +394,6 @@ export const generateCode = (input: Input) => {
393394
objects:Code.objectFromEntries(
394395
typeMapByKind.GraphQLObjectType.map(_=>[_.name,Code.propertyAccess(`Object`,_.name)]),
395396
),
396-
// unionMemberNames: Code.objectFromEntries(
397-
// typeMapByKind.GraphQLUnionType.map(
398-
// (_) => [_.name, Code.unionItems(_.getTypes().map(_ => Code.quote(_.name)))],
399-
// ),
400-
// ),
401397
unions:{
402398
type:Code.objectFrom(
403399
{
@@ -419,7 +415,6 @@ export const generateCode = (input: Input) => {
419415
),
420416
),
421417
)
422-
// console.log(typeMapByKind.GraphQLScalarType)
423418

424419
for(const[name,types]ofentries(typeMapByKind)){
425420
if(name===`GraphQLScalarType`)continue
@@ -441,45 +436,64 @@ export const generateCode = (input: Input) => {
441436

442437
letscalarsCode=``
443438

444-
scalarsCode+=`import type * as Scalar from${Code.quote(scalarsModulePath)}
439+
scalarsCode+=`
440+
import * as Scalar from${Code.quote(scalarsModulePath)}
445441
446-
declare global {
447-
interface SchemaCustomScalars {
448-
Date: Date
449-
}
450-
}
442+
declare global {
443+
interface SchemaCustomScalars {
444+
Date: Date
445+
}
446+
}
451447
452-
${
448+
${
453449
typeMapByKind.GraphQLCustomScalarType
454450
.map((_)=>{
455451
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}
458454
`
459455
}).join(`\n`)
460456
}
461457
458+
export * from${Code.quote(scalarsModulePath)}
459+
`
462460

463-
464-
465-
export * from${Code.quote(scalarsModulePath)}
466-
`
461+
constdefaultDprintConfig={
462+
quoteStyle:`preferSingle`,
463+
semiColons:`asi`,
464+
}
467465

468466
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,
471470
}
472471
}
473472

473+
importtype{Formatter}from'@dprint/formatter'
474+
import{createFromBuffer}from'@dprint/formatter'
475+
import{getPath}from'@dprint/typescript'
474476
exportconstgenerateFiles=async(params:{
475477
schemaPath:string
476478
outputDirPath:string
477479
schemaModulePath?:string
478480
scalarsModulePath?:string
481+
/**
482+
*@defaultValue `true`
483+
*/
484+
format?:boolean
479485
})=>{
480-
// todo use@dprint/formatter
481486
constschemaSource=awaitfs.readFile(params.schemaPath,`utf8`)
482-
constcode=generateCode({ schemaSource, ...params})
487+
constoptions=(params.format??true)
488+
?{
489+
formatter:createFromBuffer(awaitfs.readFile(getPath())),
490+
}
491+
:undefined
492+
constcode=generateCode({
493+
schemaSource,
494+
...params,
495+
options,
496+
})
483497
awaitfs.mkdir(params.outputDirPath,{recursive:true})
484498
awaitfs.writeFile(`${params.outputDirPath}/Schema.ts`,code.schema,{encoding:`utf8`})
485499
awaitfs.writeFile(`${params.outputDirPath}/Scalar.ts`,code.scalars,{encoding:`utf8`})

‎tests/ts/_/schema/Scalar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ declare global {
66
}
77
}
88

9-
exportconstDate=Scalar.scalar(`Date`,Scalar.nativeScalarConstructors.String)
9+
exportconstDate=Scalar.scalar('Date',Scalar.nativeScalarConstructors.String)
1010
exporttypeDate=typeofDate
1111

1212
export*from'../../../../src/Schema/NamedType/Scalar/Scalar.js'

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp