Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Only format changed files with Prettier #670

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- feat: Use Prettier's `cache` option to only format files that are modified / created (#670)

## 3.29.0

- ref(nextjs): Adjust dev server command in verification message (#665)
Expand Down
29 changes: 21 additions & 8 deletions src/utils/clack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import * as os from 'os';
import { setInterval } from 'timers';
import { URL } from 'url';
import * as Sentry from '@sentry/node';
import { hasPackageInstalled, PackageDotJson } from './package-json';
import {
getPackageVersion,
hasPackageInstalled,
PackageDotJson,
} from './package-json';
import { Feature, SentryProjectData, WizardOptions } from './types';
import { traceStep } from '../telemetry';
import {
Expand All @@ -19,6 +23,7 @@ import {
} from './package-manager';
import { debug } from './debug';
import { fulfillsVersionRange } from './semver';
import { gte } from 'semver';

const opn = require('opn') as (
url: string,
Expand Down Expand Up @@ -674,6 +679,11 @@ export async function runPrettierIfInstalled(): Promise<void> {
return traceStep('run-prettier', async () => {
const packageJson = await getPackageDotJson();
const prettierInstalled = hasPackageInstalled('prettier', packageJson);
const prettierVersion = prettierInstalled
? getPackageVersion('prettier', packageJson) || ''
: '';

const prettierSupportsCache = gte(prettierVersion, '2.7.0');

if (prettierInstalled) {
// prompt the user if they want to run prettier
Expand All @@ -696,13 +706,16 @@ export async function runPrettierIfInstalled(): Promise<void> {

try {
await new Promise<void>((resolve, reject) => {
childProcess.exec('npx prettier --write .', (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
childProcess.exec(
`npx prettier --write ${prettierSupportsCache ? '--cache' : ''} .`,
Copy link
Member

@Lms24 Lms24 Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work if users themselves didn't use --cache before? Reading the documentation, I'm not sure because it sounds like a cache already needs to be populated so that a file is skipped.

I'm not really worried about the performance aspect but rather that we format files that users (for whatever reason) haven't formatted before or don't want formatted.

If --cache works for first-time use, no worries, we can use it! Otherwise: We already have some logic around detecting new and changed files via git at the beginning of the wizard. Maybe we could re-use that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. I thought a cache was populated anyway via regular prettier use. But it looks like it was a leftover.

I have switched to using git. Thanks for pointing that out!

(err) => {
if (err) {
reject(err);
} else {
resolve();
}
},
);
});
} catch {
prettierSpinner.stop('Prettier failed to run.');
Expand Down
Loading