From ec22b7e54084569942bc77e9be2655450e805f43 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Tue, 9 May 2023 16:19:36 -0400 Subject: [PATCH] fix(devkit): catch errors resolving configs while formatting files (#16872) --- .../devkit/src/generators/format-files.ts | 34 ++++++++--------- ...hanged-files-with-prettier-if-available.ts | 38 +++++++++---------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/packages/devkit/src/generators/format-files.ts b/packages/devkit/src/generators/format-files.ts index de359ee0af8de..977153408883f 100644 --- a/packages/devkit/src/generators/format-files.ts +++ b/packages/devkit/src/generators/format-files.ts @@ -29,28 +29,28 @@ export async function formatFiles(tree: Tree): Promise { 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) diff --git a/packages/nx/src/generators/internal-utils/format-changed-files-with-prettier-if-available.ts b/packages/nx/src/generators/internal-utils/format-changed-files-with-prettier-if-available.ts index 965d3a59ff60b..53e0013be524b 100644 --- a/packages/nx/src/generators/internal-utils/format-changed-files-with-prettier-if-available.ts +++ b/packages/nx/src/generators/internal-utils/format-changed-files-with-prettier-if-available.ts @@ -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)