Skip to content

Commit

Permalink
fix(devkit): catch errors resolving configs while formatting files (#…
Browse files Browse the repository at this point in the history
…16872)

(cherry picked from commit ec22b7e)
  • Loading branch information
FrozenPandaz committed May 9, 2023
1 parent feb2b31 commit d6afd62
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
34 changes: 17 additions & 17 deletions packages/devkit/src/generators/format-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@ export async function formatFiles(tree: Tree): Promise<void> {

await Promise.all(
Array.from(files).map(async (file) => {
const systemPath = path.join(tree.root, file.path);
try {
const systemPath = path.join(tree.root, file.path);

const resolvedOptions = await prettier.resolveConfig(systemPath, {
editorconfig: true,
});
const resolvedOptions = await prettier.resolveConfig(systemPath, {
editorconfig: true,
});

const options: Prettier.Options = {
...resolvedOptions,
...changedPrettierInTree,
filepath: systemPath,
};
const options: Prettier.Options = {
...resolvedOptions,
...changedPrettierInTree,
filepath: systemPath,
};

if (file.path.endsWith('.swcrc')) {
options.parser = 'json';
}
if (file.path.endsWith('.swcrc')) {
options.parser = 'json';
}

const support = await prettier.getFileInfo(systemPath, options as any);
if (support.ignored || !support.inferredParser) {
return;
}
const support = await prettier.getFileInfo(systemPath, options as any);
if (support.ignored || !support.inferredParser) {
return;
}

try {
tree.write(
file.path,
prettier.format(file.content.toString('utf-8'), options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ export async function formatChangedFilesWithPrettierIfAvailable(
);
await Promise.all(
Array.from(files).map(async (file) => {
const systemPath = path.join(tree.root, file.path);
let options: any = {
filepath: systemPath,
};
try {
const systemPath = path.join(tree.root, file.path);
let options: any = {
filepath: systemPath,
};

const resolvedOptions = await prettier.resolveConfig(systemPath, {
editorconfig: true,
});
if (!resolvedOptions) {
return;
}
options = {
...options,
...resolvedOptions,
};
const resolvedOptions = await prettier.resolveConfig(systemPath, {
editorconfig: true,
});
if (!resolvedOptions) {
return;
}
options = {
...options,
...resolvedOptions,
};

const support = await prettier.getFileInfo(systemPath, options);
if (support.ignored || !support.inferredParser) {
return;
}
const support = await prettier.getFileInfo(systemPath, options);
if (support.ignored || !support.inferredParser) {
return;
}

try {
tree.write(
file.path,
prettier.format(file.content.toString('utf-8'), options)
Expand Down

0 comments on commit d6afd62

Please sign in to comment.