From 54496e899b0708224cee45252fdb9aeea952fb37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Mrozek?= Date: Wed, 16 Aug 2023 21:56:57 +0200 Subject: [PATCH] feat(core): support prettier v3 as a formatter (#18644) --- packages/nx/src/command-line/format/format.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/nx/src/command-line/format/format.ts b/packages/nx/src/command-line/format/format.ts index 5fd7508f2a236..efa7c63a50ac6 100644 --- a/packages/nx/src/command-line/format/format.ts +++ b/packages/nx/src/command-line/format/format.ts @@ -12,6 +12,7 @@ import * as yargs from 'yargs'; import * as prettier from 'prettier'; import { sortObjectByKeys } from '../../utils/object-sort'; +import { readModulePackageJson } from '../../utils/package-json'; import { getRootTsConfigFileName, getRootTsConfigPath, @@ -22,8 +23,9 @@ import { readNxJson } from '../../config/configuration'; import { ProjectGraph } from '../../config/project-graph'; import { chunkify } from '../../utils/chunkify'; import { allFileData } from '../../utils/all-file-data'; +import { gte } from 'semver'; -const PRETTIER_PATH = require.resolve('prettier/bin-prettier'); +const PRETTIER_PATH = getPrettierPath(); export async function format( command: 'check' | 'write', @@ -202,3 +204,11 @@ function sortTsConfig() { // catch noop } } + +function getPrettierPath() { + const prettierVersion = readModulePackageJson('prettier').packageJson.version; + if (gte(prettierVersion, '3.0.0')) { + return require.resolve('prettier/bin/prettier.cjs'); + } + return require.resolve('prettier/bin-prettier'); +}