From 94e4e4d3de2dda2027caff4b20b1b612287a8bb8 Mon Sep 17 00:00:00 2001 From: Tim Kolberger Date: Mon, 27 Dec 2021 10:47:53 +0100 Subject: [PATCH] fix(cli): replace runtime require of prettier with import (#5290) --- tooling/cli/src/utils/format-with-prettier.ts | 22 +++++-------------- tooling/cli/test/format-with-prettier.test.ts | 14 ------------ 2 files changed, 5 insertions(+), 31 deletions(-) diff --git a/tooling/cli/src/utils/format-with-prettier.ts b/tooling/cli/src/utils/format-with-prettier.ts index c33f2cfac38..0ca30e39eee 100644 --- a/tooling/cli/src/utils/format-with-prettier.ts +++ b/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) } diff --git a/tooling/cli/test/format-with-prettier.test.ts b/tooling/cli/test/format-with-prettier.test.ts index 2a0fb9d2247..f02eb06cc24 100644 --- a/tooling/cli/test/format-with-prettier.test.ts +++ b/tooling/cli/test/format-with-prettier.test.ts @@ -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'}"`, - ) - }) })