Skip to content

Commit b8ff4ee

Browse files
committed
fix: Error on missing prettier config
When a prettier config file is specified but can't be found a default prettier config is silently used as a fallback. This change causes an error to be raised when the specified prettier config doesn't exist. Closes #908
1 parent 23dc67f commit b8ff4ee

File tree

71 files changed

+266008
-268050
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+266008
-268050
lines changed

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { GlobalContext, OpenAPI2, OpenAPI3, SchemaObject, SwaggerToTSOptions } from "./types.js";
22
import path from "path";
3+
import fs from "fs";
34
import prettier from "prettier";
45
import parserTypescript from "prettier/parser-typescript.js";
56
import { Readable } from "stream";
@@ -115,7 +116,9 @@ async function openapiTS(
115116
};
116117
if (options && options.prettierConfig) {
117118
try {
118-
const userOptions = await prettier.resolveConfig(path.resolve(process.cwd(), options.prettierConfig));
119+
const prettierConfigFile = path.resolve(process.cwd(), options.prettierConfig);
120+
await fs.promises.access(prettierConfigFile, fs.constants.F_OK);
121+
const userOptions = await prettier.resolveConfig(prettierConfigFile);
119122
prettierOptions = {
120123
...(userOptions || {}),
121124
...prettierOptions,

test/bin/cli.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ describe("cli", () => {
2020
});
2121

2222
it("--prettier-config (.js)", async () => {
23-
execSync(`${cmd} specs/petstore.yaml -o generated/prettier-js.ts --prettier-config fixtures/prettier.config.js`, {
23+
execSync(`${cmd} specs/petstore.yaml -o generated/prettier-js.ts --prettier-config fixtures/prettier.config.cjs`, {
2424
cwd,
2525
});
2626
const generated = fs.readFileSync(new URL("./generated/prettier-js.ts", cwd), "utf8");
2727
const expected = eol.lf(fs.readFileSync(new URL("./expected/prettier-js.ts", cwd), "utf8"));
2828
expect(generated).to.equal(expected);
2929
});
3030

31+
it("--prettier-config (missing)", async () => {
32+
expect(() => {
33+
execSync(`${cmd} specs/petstore.yaml -o generated/prettier-missing.ts --prettier-config NO_SUCH_FILE`);
34+
}).to.throw('NO_SUCH_FILE');
35+
});
36+
3137
it("stdout", async () => {
3238
const generated = execSync(`${cmd} specs/petstore.yaml`, { cwd });
3339
const expected = eol.lf(fs.readFileSync(new URL("./expected/stdout.ts", cwd), "utf8"));

0 commit comments

Comments
 (0)