Skip to content

Commit

Permalink
feat(generator): use prettier if present (#1222)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Oct 25, 2024
1 parent 5b91cf0 commit d859d37
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 6 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@
"@dprint/formatter": "^0.4.0",
"@dprint/typescript": "^0.93.0",
"@opentelemetry/api": "^1.9.0",
"graphql": "14 - 16"
"graphql": "14 - 16",
"prettier": "^3.3.3"
},
"peerDependenciesMeta": {
"dprint": {
"prettier": {
"optional": true
},
"@opentelemetry/api": {
Expand Down Expand Up @@ -168,6 +169,7 @@
"@typescript-eslint/parser": "^8.11.0",
"async-cleanup": "^1.0.0",
"doctoc": "^2.2.1",
"dprint": "^0.47.4",
"dripip": "^0.10.0",
"es-toolkit": "^1.26.1",
"eslint": "^9.13.0",
Expand All @@ -188,6 +190,7 @@
"graphql-upload-minimal": "^1.6.1",
"graphql-yoga": "^5.7.0",
"jsdom": "^25.0.1",
"prettier": "^3.3.3",
"publint": "^0.2.12",
"strip-ansi": "^7.1.0",
"tsx": "^4.19.1",
Expand Down
85 changes: 85 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions src/lib/typescript-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,36 @@ export const passthroughFormatter: Formatter = {
formatText: (content) => Promise.resolve(content),
}

export const getTypeScriptFormatter = async (): Promise<Formatter | null> => {
return await getTypeScriptFormatterDprint() ?? await getTypeScriptFormatterPrettier()
}

export const getTypeScriptFormatterPrettier = async (): Promise<Formatter | null> => {
try {
const prettier = await import(`prettier`)
return {
formatText: (content) => prettier.format(content, { parser: `typescript` }),
}
} catch (error) {
return null
}
}

/**
* Attempt to get a TypeScript formatter using dynamic imports. If none succeed then returns null.
*
* This allows users to bring their own formatters (within an allow list of what we try to dynamically import).
*/
export const getTypeScriptFormatter = async (): Promise<Formatter | null> => {
export const getTypeScriptFormatterDprint = async (): Promise<Formatter | null> => {
try {
const { createFromBuffer } = await import(`@dprint/formatter`)
const { getPath } = await import(`@dprint/typescript`)
const formatter = createFromBuffer(await fs.readFile(getPath()))
// todo handle failing to read configuration file gracefully. Don't swallow those errors.
// TODO don't read config file manually? https://github.com/dprint/js-formatter/issues/13
const localConfig = await readJsonFile<{ typescript?: JsonObject }>(`dprint.json`) ?? {}
return {
formatText: async (fileText, customFormatterConfig) => {
// todo handle failing to read configuration file gracefully.
// TODO don't read config file manually? https://github.com/dprint/js-formatter/issues/13
const localConfig = await readJsonFile<{ typescript?: JsonObject }>(`dprint.json`) ?? {}
const overrideConfig = {
...localConfig.typescript,
...customFormatterConfig,
Expand Down
13 changes: 13 additions & 0 deletions tests/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,16 @@ test(`client uses dprint formatter if installed`, async ({ project }) => {

await project.run`pnpm dprint check graffle/**/*`
})

test(`client uses prettier formatter if installed`, async ({ project }) => {
// await project.addDprintConfig()
const path = await project.addPokemonSchemaSDL()

await project.run`pnpm add --save-dev prettier`

const genResult = await project.run`pnpm graffle --schema ${path.relative} --format`
const genResultStdout = genResult.stdout as string
expect(genResultStdout.includes(`No TypeScript formatter found`)).toEqual(false)

await project.run`pnpm prettier --check graffle/**/*`
})

0 comments on commit d859d37

Please sign in to comment.