Skip to content

Commit

Permalink
feat(core): handle prettier not installed when running nx format
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Sep 18, 2024
1 parent eb61254 commit 234d222
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
38 changes: 30 additions & 8 deletions packages/nx/src/command-line/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<void> {
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',
Expand Down Expand Up @@ -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> | SupportInfo)
await (require('prettier').getSupportInfo() as
| Promise<SupportInfo>
| SupportInfo)
).languages
.flatMap((language) => language.extensions)
.filter((extension) => !!extension)
Expand Down Expand Up @@ -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(
' '
)}`,
{
Expand All @@ -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`,
{
Expand All @@ -219,9 +231,12 @@ async function check(patterns: string[]): Promise<string[]> {
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) {
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<% } %>
Expand Down

0 comments on commit 234d222

Please sign in to comment.