Skip to content

Commit

Permalink
fix(cli): replace runtime require of prettier with import (#5290)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimKolberger committed Dec 27, 2021
1 parent ae6fd7a commit 94e4e4d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 31 deletions.
22 changes: 5 additions & 17 deletions tooling/cli/src/utils/format-with-prettier.ts
@@ -1,25 +1,13 @@
import type { format, resolveConfig } from "prettier"
import { format, resolveConfig } from "prettier"

const createFormatFileWithPrettier = (prettier: {
format: typeof format
resolveConfig: typeof resolveConfig
}) => async (content: string) => {
const prettierConfig = await prettier.resolveConfig(process.cwd())
return prettier.format(String(content), {
async function createFormatFileWithPrettier(content: string) {
const prettierConfig = await resolveConfig(process.cwd())
return format(String(content), {
...prettierConfig,
parser: "typescript",
})
}

export async function formatWithPrettierIfAvailable(content: string) {
let prettier
try {
// eslint-disable-next-line global-require
prettier = require("prettier")
} catch {
// silent exit if prettier is not installed
return content
}

return createFormatFileWithPrettier(prettier)(content)
return createFormatFileWithPrettier(content)
}
14 changes: 0 additions & 14 deletions tooling/cli/test/format-with-prettier.test.ts
Expand Up @@ -14,18 +14,4 @@ describe("Format With Prettier", () => {
"
`)
})

it("should not fail if prettier is not available", async () => {
jest.mock("prettier", () => {
throw new Error("module not found")
})

const content =
"export interface ThemeTypings { fonts: 'test1'|'test2'|'test3'}"
const pretty = await formatWithPrettierIfAvailable(content)

expect(pretty).toMatchInlineSnapshot(
`"export interface ThemeTypings { fonts: 'test1'|'test2'|'test3'}"`,
)
})
})

1 comment on commit 94e4e4d

@vercel
Copy link

@vercel vercel bot commented on 94e4e4d Dec 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.