Skip to content

Commit

Permalink
feat(core): support prettier v3 as a formatter
Browse files Browse the repository at this point in the history
closed #17990
  • Loading branch information
Michsior14 committed Aug 15, 2023
1 parent 40d66ec commit 22ab0bd
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/nx/src/command-line/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,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 @@ -202,3 +203,15 @@ function sortTsConfig() {
// catch noop
}
}

function getPrettierPath() {
let prettierVersion: string;
try {
prettierVersion = require(require.resolve('prettier/package.json')).version;
} catch {}

if (prettierVersion && gte(prettierVersion, '3.0.0')) {
return require.resolve('prettier/bin/prettier.cjs');
}
return require.resolve('prettier/bin-prettier');
}

0 comments on commit 22ab0bd

Please sign in to comment.