Skip to content

Commit

Permalink
feat(core): support prettier v3 as a formatter (#18644)
Browse files Browse the repository at this point in the history
(cherry picked from commit 54496e8)
  • Loading branch information
Michsior14 authored and FrozenPandaz committed Aug 23, 2023
1 parent 8c3822c commit 498b61b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/nx/src/command-line/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down Expand Up @@ -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');
}

0 comments on commit 498b61b

Please # to comment.