From 498b61be0866dfa292d0b2b4c7768be3691e9f70 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) (cherry picked from commit 54496e899b0708224cee45252fdb9aeea952fb37) --- 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 724a43b663581..540f91ffa2b41 100644 --- a/packages/nx/src/command-line/format/format.ts +++ b/packages/nx/src/command-line/format/format.ts @@ -13,6 +13,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, @@ -23,8 +24,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', @@ -210,3 +212,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'); +}