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') tree.listChanges().filter((file) => file.type !== 'DELETE')
); );
const changedPrettierInTree = getChangedPrettierConfigInTree(tree);
await Promise.all( await Promise.all(
Array.from(files).map(async (file) => { Array.from(files).map(async (file) => {
const systemPath = path.join(tree.root, file.path); const systemPath = path.join(tree.root, file.path);
@ -33,22 +35,17 @@ export async function formatFiles(tree: Tree): Promise<void> {
editorconfig: true, editorconfig: true,
}); });
let optionsFromTree; const options: Prettier.Options = {
if (!resolvedOptions) { ...resolvedOptions,
try { ...changedPrettierInTree,
optionsFromTree = readJson(tree, '.prettierrc');
} catch {}
}
const options: any = {
filepath: systemPath, filepath: systemPath,
...(resolvedOptions ?? optionsFromTree),
}; };
if (file.path.endsWith('.swcrc')) { if (file.path.endsWith('.swcrc')) {
options.parser = 'json'; options.parser = 'json';
} }
const support = await prettier.getFileInfo(systemPath, options); const support = await prettier.getFileInfo(systemPath, options as any);
if (support.ignored || !support.inferredParser) { if (support.ignored || !support.inferredParser) {
return; return;
} }
@ -92,3 +89,15 @@ function getRootTsConfigPath(tree: Tree): string | null {
return 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;
}
}