diff --git a/packages/nx/src/command-line/migrate/migrate.ts b/packages/nx/src/command-line/migrate/migrate.ts index 301fd7379e09f..83cd3708d65ae 100644 --- a/packages/nx/src/command-line/migrate/migrate.ts +++ b/packages/nx/src/command-line/migrate/migrate.ts @@ -67,7 +67,7 @@ import { createProjectGraphAsync, readProjectsConfigurationFromProjectGraph, } from '../../project-graph/project-graph'; -import type * as Prettier from 'prettier'; +import { formatFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available'; export interface ResolvedMigrationConfiguration extends MigrationsJson { packageGroup?: ArrayPackageGroup; @@ -1325,45 +1325,17 @@ async function writeFormattedJsonFile( content: any, options?: JsonWriteOptions ): Promise { - let prettier: typeof Prettier; - try { - prettier = await import('prettier'); - } catch {} - - if (!prettier) { - // no prettier, write the json file as is - writeJsonFile(filePath, content, options); - return; - } + const formattedContent = await formatFilesWithPrettierIfAvailable( + [{ path: filePath, content: JSON.stringify(content) }], + workspaceRoot, + { silent: true } + ); - try { - const resolvedOptions = await prettier.resolveConfig(filePath, { - editorconfig: true, + if (formattedContent.has(filePath)) { + writeFileSync(filePath, formattedContent.get(filePath)!, { + encoding: 'utf-8', }); - - const prettierOptions: Prettier.Options = { - ...resolvedOptions, - filepath: filePath, - }; - - const support = await prettier.getFileInfo( - filePath, - prettierOptions as any - ); - if (support.ignored || !support.inferredParser) { - // it's ignored or the parser could not be inferred, write the json file as is - writeJsonFile(filePath, content, options); - return; - } - - // format and write the file - const formattedContent = await (prettier.format( - JSON.stringify(content), - prettierOptions - ) as Promise | string); - writeFileSync(filePath, formattedContent, { encoding: 'utf-8' }); - } catch { - // prettier failed, ignore and write the json file as is + } else { writeJsonFile(filePath, content, 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 06768575c726a..d3dd48593a729 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 @@ -12,20 +12,43 @@ export async function formatChangedFilesWithPrettierIfAvailable( silent?: boolean; } ): Promise { + const files = new Set( + tree.listChanges().filter((file) => file.type !== 'DELETE') + ); + + const results = await formatFilesWithPrettierIfAvailable( + Array.from(files), + tree.root, + options + ); + + for (const [path, content] of results) { + tree.write(path, content); + } +} + +export async function formatFilesWithPrettierIfAvailable( + files: { path: string; content: string | Buffer }[], + root: string, + options?: { + silent?: boolean; + } +): Promise> { + const results = new Map(); + let prettier: typeof Prettier; try { prettier = await import('prettier'); } catch {} - if (!prettier) return; + if (!prettier) { + return results; + } - const files = new Set( - tree.listChanges().filter((file) => file.type !== 'DELETE') - ); await Promise.all( Array.from(files).map(async (file) => { try { - const systemPath = path.join(tree.root, file.path); + const systemPath = path.join(root, file.path); let options: any = { filepath: systemPath, }; @@ -46,7 +69,7 @@ export async function formatChangedFilesWithPrettierIfAvailable( return; } - tree.write( + results.set( file.path, // In prettier v3 the format result is a promise await (prettier.format(file.content.toString('utf-8'), options) as @@ -60,4 +83,6 @@ export async function formatChangedFilesWithPrettierIfAvailable( } }) ); + + return results; }