diff --git a/packages/devkit/src/generators/format-files.ts b/packages/devkit/src/generators/format-files.ts index 1ca43de7ea938..de359ee0af8de 100644 --- a/packages/devkit/src/generators/format-files.ts +++ b/packages/devkit/src/generators/format-files.ts @@ -25,6 +25,8 @@ export async function formatFiles(tree: Tree): Promise { tree.listChanges().filter((file) => file.type !== 'DELETE') ); + const changedPrettierInTree = getChangedPrettierConfigInTree(tree); + await Promise.all( Array.from(files).map(async (file) => { const systemPath = path.join(tree.root, file.path); @@ -33,22 +35,17 @@ export async function formatFiles(tree: Tree): Promise { editorconfig: true, }); - let optionsFromTree; - if (!resolvedOptions) { - try { - optionsFromTree = readJson(tree, '.prettierrc'); - } catch {} - } - const options: any = { + const options: Prettier.Options = { + ...resolvedOptions, + ...changedPrettierInTree, filepath: systemPath, - ...(resolvedOptions ?? optionsFromTree), }; if (file.path.endsWith('.swcrc')) { options.parser = 'json'; } - const support = await prettier.getFileInfo(systemPath, options); + const support = await prettier.getFileInfo(systemPath, options as any); if (support.ignored || !support.inferredParser) { return; } @@ -92,3 +89,15 @@ function getRootTsConfigPath(tree: Tree): string | null { return null; } + +function getChangedPrettierConfigInTree(tree: Tree): Prettier.Options | null { + if (tree.listChanges().find((file) => file.path === '.prettierrc')) { + try { + return readJson(tree, '.prettierrc'); + } catch { + return null; + } + } else { + return null; + } +}