Skip to content

Commit d859d37

Browse files
authored
feat(generator): use prettier if present (#1222)
1 parent 5b91cf0 commit d859d37

File tree

4 files changed

+122
-6
lines changed

4 files changed

+122
-6
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@
132132
"@dprint/formatter": "^0.4.0",
133133
"@dprint/typescript": "^0.93.0",
134134
"@opentelemetry/api": "^1.9.0",
135-
"graphql": "14 - 16"
135+
"graphql": "14 - 16",
136+
"prettier": "^3.3.3"
136137
},
137138
"peerDependenciesMeta": {
138-
"dprint": {
139+
"prettier": {
139140
"optional": true
140141
},
141142
"@opentelemetry/api": {
@@ -168,6 +169,7 @@
168169
"@typescript-eslint/parser": "^8.11.0",
169170
"async-cleanup": "^1.0.0",
170171
"doctoc": "^2.2.1",
172+
"dprint": "^0.47.4",
171173
"dripip": "^0.10.0",
172174
"es-toolkit": "^1.26.1",
173175
"eslint": "^9.13.0",
@@ -188,6 +190,7 @@
188190
"graphql-upload-minimal": "^1.6.1",
189191
"graphql-yoga": "^5.7.0",
190192
"jsdom": "^25.0.1",
193+
"prettier": "^3.3.3",
191194
"publint": "^0.2.12",
192195
"strip-ansi": "^7.1.0",
193196
"tsx": "^4.19.1",

pnpm-lock.yaml

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

src/lib/typescript-formatter.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,36 @@ export const passthroughFormatter: Formatter = {
1010
formatText: (content) => Promise.resolve(content),
1111
}
1212

13+
export const getTypeScriptFormatter = async (): Promise<Formatter | null> => {
14+
return await getTypeScriptFormatterDprint() ?? await getTypeScriptFormatterPrettier()
15+
}
16+
17+
export const getTypeScriptFormatterPrettier = async (): Promise<Formatter | null> => {
18+
try {
19+
const prettier = await import(`prettier`)
20+
return {
21+
formatText: (content) => prettier.format(content, { parser: `typescript` }),
22+
}
23+
} catch (error) {
24+
return null
25+
}
26+
}
27+
1328
/**
1429
* Attempt to get a TypeScript formatter using dynamic imports. If none succeed then returns null.
1530
*
1631
* This allows users to bring their own formatters (within an allow list of what we try to dynamically import).
1732
*/
18-
export const getTypeScriptFormatter = async (): Promise<Formatter | null> => {
33+
export const getTypeScriptFormatterDprint = async (): Promise<Formatter | null> => {
1934
try {
2035
const { createFromBuffer } = await import(`@dprint/formatter`)
2136
const { getPath } = await import(`@dprint/typescript`)
2237
const formatter = createFromBuffer(await fs.readFile(getPath()))
38+
// todo handle failing to read configuration file gracefully. Don't swallow those errors.
39+
// TODO don't read config file manually? https://github.com/dprint/js-formatter/issues/13
40+
const localConfig = await readJsonFile<{ typescript?: JsonObject }>(`dprint.json`) ?? {}
2341
return {
2442
formatText: async (fileText, customFormatterConfig) => {
25-
// todo handle failing to read configuration file gracefully.
26-
// TODO don't read config file manually? https://github.com/dprint/js-formatter/issues/13
27-
const localConfig = await readJsonFile<{ typescript?: JsonObject }>(`dprint.json`) ?? {}
2843
const overrideConfig = {
2944
...localConfig.typescript,
3045
...customFormatterConfig,

tests/e2e/e2e.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,16 @@ test(`client uses dprint formatter if installed`, async ({ project }) => {
8787

8888
await project.run`pnpm dprint check graffle/**/*`
8989
})
90+
91+
test(`client uses prettier formatter if installed`, async ({ project }) => {
92+
// await project.addDprintConfig()
93+
const path = await project.addPokemonSchemaSDL()
94+
95+
await project.run`pnpm add --save-dev prettier`
96+
97+
const genResult = await project.run`pnpm graffle --schema ${path.relative} --format`
98+
const genResultStdout = genResult.stdout as string
99+
expect(genResultStdout.includes(`No TypeScript formatter found`)).toEqual(false)
100+
101+
await project.run`pnpm prettier --check graffle/**/*`
102+
})

0 commit comments

Comments
 (0)