Skip to content

Commit

Permalink
rtk query code gen: add optional prettierrc override
Browse files Browse the repository at this point in the history
Allow users to optionally provide the explicit location of the prettier
config to use. For example unblocks users that are using an
incompatible/different major version of prettier as part of their
project that breaks the prettier version rtk query code gen depends on.

see #4038
  • Loading branch information
FabianFrank authored and markerikson committed Aug 30, 2024
1 parent f3756c3 commit b9363e1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
9 changes: 6 additions & 3 deletions packages/rtk-query-codegen-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ export async function generateEndpoints(options: GenerationOptions): Promise<str
const { generateApi } = await import('./generate');
return generateApi(schemaAbsPath, options);
});
const { outputFile } = options;
const { outputFile, prettierConfigFile } = options;
if (outputFile) {
fs.writeFileSync(path.resolve(process.cwd(), outputFile), await prettify(outputFile, sourceCode));
fs.writeFileSync(
path.resolve(process.cwd(), outputFile),
await prettify(outputFile, sourceCode, prettierConfigFile)
);
} else {
return await prettify(null, sourceCode);
return await prettify(null, sourceCode, prettierConfigFile);
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/rtk-query-codegen-openapi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,17 @@ export interface CommonOptions {
*/
mergeReadWriteOnly?: boolean;
/**
*
* HTTPResolverOptions object that is passed to the SwaggerParser bundle function.
*/
httpResolverOptions?: SwaggerParser.HTTPResolverOptions;

/**
* defaults to undefined
* If present the given file will be used as prettier config when formatting the generated code. If undefined the default prettier config
* resolution mechanism will be used.
*/
prettierConfigFile?: string;
}

export type TextMatcher = string | RegExp | (string | RegExp)[];
Expand Down
10 changes: 8 additions & 2 deletions packages/rtk-query-codegen-openapi/src/utils/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const EXTENSION_TO_PARSER: Record<string, string> = {
json: 'json',
};

export async function prettify(filePath: string | null, content: string): Promise<string> {
export async function prettify(filePath: string | null, content: string, prettierConfigFile?: string): Promise<string> {
let config = null;
let parser = 'typescript';

Expand All @@ -28,7 +28,13 @@ export async function prettify(filePath: string | null, content: string): Promis
parser = EXTENSION_TO_PARSER[fileExtension];
config = await prettier.resolveConfig(process.cwd(), {
useCache: true,
editorconfig: true,
editorconfig: !prettierConfigFile,
config: prettierConfigFile,
});
} else if (prettierConfigFile) {
config = await prettier.resolveConfig(process.cwd(), {
useCache: true,
config: prettierConfigFile,
});
}

Expand Down

0 comments on commit b9363e1

Please sign in to comment.