diff --git a/packages/nx/src/command-line/format/format.ts b/packages/nx/src/command-line/format/format.ts index fa8cd52095177..8bf1dd2a84e76 100644 --- a/packages/nx/src/command-line/format/format.ts +++ b/packages/nx/src/command-line/format/format.ts @@ -12,7 +12,6 @@ import { fileExists, readJsonFile, writeJsonFile } from '../../utils/fileutils'; import { getIgnoreObject } from '../../utils/ignore'; import type { SupportInfo } from 'prettier'; -import * as prettier from 'prettier'; import { readNxJson } from '../../config/configuration'; import { ProjectGraph } from '../../config/project-graph'; import { @@ -28,12 +27,22 @@ import { output } from '../../utils/output'; import { readModulePackageJson } from '../../utils/package-json'; import { workspaceRoot } from '../../utils/workspace-root'; -const PRETTIER_PATH = getPrettierPath(); - export async function format( command: 'check' | 'write', args: yargs.Arguments ): Promise { + try { + require('prettier'); + } catch { + output.error({ + title: 'Prettier is not installed.', + bodyLines: [ + `Please install "prettier" and try again, or don't run the "nx format:${command}" command.`, + ], + }); + process.exit(1); + } + const { nxArgs } = splitArgsIntoNxArgsAndOverrides( args, 'affected', @@ -103,7 +112,9 @@ async function getPatterns( // In prettier v3 the getSupportInfo result is a promise const supportedExtensions = new Set( ( - await (prettier.getSupportInfo() as Promise | SupportInfo) + await (require('prettier').getSupportInfo() as + | Promise + | SupportInfo) ).languages .flatMap((language) => language.extensions) .filter((extension) => !!extension) @@ -192,9 +203,10 @@ function write(patterns: string[]) { }, [[], []] as [swcrcPatterns: string[], regularPatterns: string[]] ); + const prettierPath = getPrettierPath(); execSync( - `node "${PRETTIER_PATH}" --write --list-different ${regularPatterns.join( + `node "${prettierPath}" --write --list-different ${regularPatterns.join( ' ' )}`, { @@ -204,7 +216,7 @@ function write(patterns: string[]) { if (swcrcPatterns.length > 0) { execSync( - `node "${PRETTIER_PATH}" --write --list-different ${swcrcPatterns.join( + `node "${prettierPath}" --write --list-different ${swcrcPatterns.join( ' ' )} --parser json`, { @@ -219,9 +231,12 @@ async function check(patterns: string[]): Promise { if (patterns.length === 0) { return []; } + + const prettierPath = getPrettierPath(); + return new Promise((resolve) => { exec( - `node "${PRETTIER_PATH}" --list-different ${patterns.join(' ')}`, + `node "${prettierPath}" --list-different ${patterns.join(' ')}`, { encoding: 'utf-8' }, (error, stdout) => { if (error) { @@ -248,7 +263,14 @@ function sortTsConfig() { } } +let prettierPath: string; function getPrettierPath() { + if (prettierPath) { + return prettierPath; + } + const { bin } = readModulePackageJson('prettier').packageJson; - return require.resolve(path.join('prettier', bin as string)); + prettierPath = require.resolve(path.join('prettier', bin as string)); + + return prettierPath; } diff --git a/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap b/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap index c997a1c33a9e9..e66dc26f8e668 100644 --- a/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap +++ b/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap @@ -81,7 +81,8 @@ pipelines: - npm ci --legacy-peer-deps - - npx nx-cloud record -- nx format:check + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # npx nx-cloud record -- echo Hello World # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - npx nx affected --base=origin/main -t lint test build - npx nx affected --base=origin/main --parallel 1 -t e2e-ci @@ -312,7 +313,8 @@ pipelines: - bun install --no-cache - - bun nx-cloud record -- nx format:check + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # bun nx-cloud record -- echo Hello World # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - bun nx affected --base=origin/main -t lint test build @@ -569,7 +571,8 @@ pipelines: - npm ci --legacy-peer-deps - - npx nx-cloud record -- nx format:check + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # npx nx-cloud record -- echo Hello World # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - npx nx affected --base=origin/main -t lint test build @@ -827,7 +830,8 @@ pipelines: - pnpm install --frozen-lockfile - - pnpm exec nx-cloud record -- nx format:check + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # pnpm exec nx-cloud record -- echo Hello World # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - pnpm exec nx affected --base=origin/main -t lint test build @@ -1096,7 +1100,8 @@ pipelines: - yarn install --frozen-lockfile - - yarn nx-cloud record -- nx format:check + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # yarn nx-cloud record -- echo Hello World # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - yarn nx affected --base=origin/main -t lint test build @@ -1351,7 +1356,8 @@ pipelines: - npm ci --legacy-peer-deps - - npx nx-cloud record -- nx format:check + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # npx nx-cloud record -- echo Hello World # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - npx nx affected --base=origin/main -t lint test build - npx nx affected --base=origin/main --parallel 1 -t e2e-ci @@ -1588,7 +1594,8 @@ pipelines: - bun install --no-cache - - bun nx-cloud record -- nx format:check + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # bun nx-cloud record -- echo Hello World # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - bun nx affected --base=origin/main -t lint test build @@ -1852,7 +1859,8 @@ pipelines: - npm ci --legacy-peer-deps - - npx nx-cloud record -- nx format:check + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # npx nx-cloud record -- echo Hello World # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - npx nx affected --base=origin/main -t lint test build @@ -2117,7 +2125,8 @@ pipelines: - pnpm install --frozen-lockfile - - pnpm exec nx-cloud record -- nx format:check + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # pnpm exec nx-cloud record -- echo Hello World # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - pnpm exec nx affected --base=origin/main -t lint test build @@ -2393,7 +2402,8 @@ pipelines: - yarn install --frozen-lockfile - - yarn nx-cloud record -- nx format:check + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # yarn nx-cloud record -- echo Hello World # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - yarn nx affected --base=origin/main -t lint test build diff --git a/packages/workspace/src/generators/ci-workflow/files/bitbucket-pipelines/bitbucket-pipelines.yml__tmpl__ b/packages/workspace/src/generators/ci-workflow/files/bitbucket-pipelines/bitbucket-pipelines.yml__tmpl__ index 56e3aeb0912cb..9fc352107e3a3 100644 --- a/packages/workspace/src/generators/ci-workflow/files/bitbucket-pipelines/bitbucket-pipelines.yml__tmpl__ +++ b/packages/workspace/src/generators/ci-workflow/files/bitbucket-pipelines/bitbucket-pipelines.yml__tmpl__ @@ -28,7 +28,8 @@ pipelines: <% } %> - <%= packageManagerInstall %> - - <%= packageManagerPrefix %> nx-cloud record -- nx format:check + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # <%= packageManagerPrefix %> nx-cloud record -- echo Hello World # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - <%= packageManagerPrefix %> nx affected --base=origin/<%= mainBranch %> -t lint test build<% if(hasE2E){ %> - <%= packageManagerPrefix %> nx affected --base=origin/<%= mainBranch %> --parallel 1 -t e2e-ci<% } %>