Skip to content

Commit

Permalink
fix(devkit): formatFiles should work when writing initial prettierrc …
Browse files Browse the repository at this point in the history
…in generator (#16711)
  • Loading branch information
AgentEnder authored May 2, 2023
1 parent fa55ca4 commit 7b56a8c
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions packages/devkit/src/generators/format-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export async function formatFiles(tree: Tree): Promise<void> {
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);
Expand All @@ -33,22 +35,17 @@ export async function formatFiles(tree: Tree): Promise<void> {
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;
}
Expand Down Expand Up @@ -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;
}
}

1 comment on commit 7b56a8c

@vercel
Copy link

@vercel vercel bot commented on 7b56a8c May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.