fix(devkit): formatFiles should work when writing initial prettierrc in generator (#16711)

This commit is contained in:
Craigory Coppola 2023-05-02 19:18:45 -04:00 committed by GitHub
parent fa55ca4f5f
commit 7b56a8cbf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
@ -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;
}
@ -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;
}
}