Skip to content

Missing prettier config silently ignored #908

Closed
@duncanbeevers

Description

@duncanbeevers

Currently, when specifying --prettier-config <FILE>, no complaint is made if <FILE> doesn't exist.
Instead, a fallback prettier config is silently used.

I came across this behavior while refining specs for #890
Many of these specs specific a --prettier-config but end up using this fallback behavior.

I began work on enforcing the existence of this config file, if specified, but since it breaks so many existing specs I figured I'd put on the brakes and ask whether this is intentional behavior, and to see what you'd like to do about it.

Enforcement

diff --git a/src/index.ts b/src/index.ts
index 5d67142..3f45ffc 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,5 +1,6 @@
 import type { GlobalContext, OpenAPI2, OpenAPI3, SchemaObject, SwaggerToTSOptions } from "./types.js";
 import path from "path";
+import fs from "fs";
 import prettier from "prettier";
 import parserTypescript from "prettier/parser-typescript.js";
 import { Readable } from "stream";
@@ -115,7 +116,9 @@ async function openapiTS(
   };
   if (options && options.prettierConfig) {
     try {
-      const userOptions = await prettier.resolveConfig(path.resolve(process.cwd(), options.prettierConfig));
+      const prettierConfigFile = path.resolve(process.cwd(), options.prettierConfig);
+      await fs.promises.access(prettierConfigFile, fs.constants.F_OK);
+      const userOptions = await prettier.resolveConfig(prettierConfigFile);
       prettierOptions = {
         ...(userOptions || {}),
         ...prettierOptions,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions