Skip to content

Commit

Permalink
chore: allow passing format options
Browse files Browse the repository at this point in the history
  • Loading branch information
davidenke committed Oct 21, 2024
1 parent aef4dfa commit 1017f53
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/utils/format.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ export async function loadPrettier(): Promise<typeof Prettier | undefined> {
}

// format code using prettier if available
export async function formatCode(code: string, target?: string): Promise<string> {
export async function formatCode(
code: string,
target?: string,
options?: Prettier.Options,
): Promise<string> {
const prettier = await loadPrettier();
if (!prettier) return code;
const options = (target && (await prettier.resolveConfig?.(target))) ?? {};
const defaults = (target && (await prettier.resolveConfig?.(target))) ?? {};
const imports = await Promise.all([
import('prettier/plugins/estree'),
import('prettier/plugins/typescript'),
]);
const plugins = imports.map(i => i.default);
return prettier.format(code, { ...options, parser: 'typescript', plugins });
return prettier.format(code, { ...defaults, ...options, parser: 'typescript', plugins });
}

0 comments on commit 1017f53

Please sign in to comment.