Skip to content

Commit

Permalink
pref(prettier): enable prettier cache (#511)
Browse files Browse the repository at this point in the history
**Problem:**

Prettier can be slow when there are many files (particularly with large
repos and running with `--all`).

**Solution:**

Enable Prettier' cache by default.
  • Loading branch information
paularmstrong authored Dec 30, 2023
1 parent 154a2d1 commit 0125556
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-pumpkins-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@onerepo/plugin-prettier': minor
---

Enable using cache to speed up formatting files. This feature can be disabled at the plugin configuration level by setting `useCache: false` or by passing `--no-cache` to the command.
31 changes: 26 additions & 5 deletions plugins/prettier/src/commands/__tests__/prettier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('handler', () => {
expect(graph.packageManager.run).toHaveBeenCalledWith(
expect.objectContaining({
cmd: 'prettier',
args: ['--ignore-unknown', '--write', '.'],
args: ['--ignore-unknown', '--write', '--cache', '--cache-strategy', 'content', '.'],
}),
);
});
Expand All @@ -66,7 +66,15 @@ describe('handler', () => {
expect(graph.packageManager.run).toHaveBeenCalledWith(
expect.objectContaining({
cmd: 'prettier',
args: ['--ignore-unknown', '--write', 'modules/burritos', 'modules/tacos'],
args: [
'--ignore-unknown',
'--write',
'--cache',
'--cache-strategy',
'content',
'modules/burritos',
'modules/tacos',
],
}),
);
});
Expand All @@ -79,7 +87,7 @@ describe('handler', () => {
expect(graph.packageManager.run).toHaveBeenCalledWith(
expect.objectContaining({
cmd: 'prettier',
args: ['--ignore-unknown', '--list-different', '.'],
args: ['--ignore-unknown', '--list-different', '--cache', '--cache-strategy', 'content', '.'],
}),
);
});
Expand All @@ -102,7 +110,7 @@ bar/**/*
expect(graph.packageManager.run).toHaveBeenCalledWith(
expect.objectContaining({
cmd: 'prettier',
args: ['--ignore-unknown', '--write', 'foo.js'],
args: ['--ignore-unknown', '--write', '--cache', '--cache-strategy', 'content', 'foo.js'],
}),
);
});
Expand All @@ -126,7 +134,7 @@ bar/**/*
expect(graph.packageManager.run).toHaveBeenCalledWith(
expect.objectContaining({
cmd: 'prettier',
args: ['--ignore-unknown', '--write', 'bar.js'],
args: ['--ignore-unknown', '--write', '--cache', '--cache-strategy', 'content', 'bar.js'],
}),
);
expect(git.updateIndex).toHaveBeenCalledWith(['bar.js']);
Expand All @@ -147,4 +155,17 @@ bar/**/*
expect(core.error).toHaveBeenCalledWith(expect.stringContaining('This file needs formatting'), { file: 'bop.js' });
expect(core.error).not.toHaveBeenCalledWith(expect.any(String), { file: 'bar.js' });
});

test('can disable cache', async () => {
vi.spyOn(graph.packageManager, 'run').mockResolvedValue(['', '']);

await run('--all --no-cache');

expect(graph.packageManager.run).toHaveBeenCalledWith(
expect.objectContaining({
cmd: 'prettier',
args: ['--ignore-unknown', '--write', '.'],
}),
);
});
});
18 changes: 15 additions & 3 deletions plugins/prettier/src/commands/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const description = 'Format files with prettier';

type Args = {
add?: boolean;
cache: boolean;
check?: boolean;
'github-annotate': boolean;
} & builders.WithAllInputs;
Expand All @@ -29,7 +30,13 @@ export const builder: Builder<Args> = (yargs) =>
type: 'boolean',
})
.option('github-annotate', {
description: 'Annotate files in GitHub with errors when failing format checks in GitHub Actions',
description: 'Annotate files in GitHub with errors when failing format checks in GitHub Actions.',
type: 'boolean',
default: true,
hidden: true,
})
.option('cache', {
description: 'Use Prettier’s built-in cache to determin whether files need formatting or not.',
type: 'boolean',
default: true,
hidden: true,
Expand All @@ -41,7 +48,7 @@ export const builder: Builder<Args> = (yargs) =>
});

export const handler: Handler<Args> = async function handler(argv, { getFilepaths, graph, logger }) {
const { add, all, check, 'dry-run': isDry, 'github-annotate': github, $0: cmd, _: positionals } = argv;
const { add, all, cache, check, 'dry-run': isDry, 'github-annotate': github, $0: cmd, _: positionals } = argv;

const filteredPaths = [];
if (!all) {
Expand Down Expand Up @@ -81,7 +88,12 @@ export const handler: Handler<Args> = async function handler(argv, { getFilepath
await graph.packageManager.run({
name: `Format files ${all ? '' : filteredPaths.join(', ').substring(0, 60)}…`,
cmd: 'prettier',
args: ['--ignore-unknown', isDry || check ? '--list-different' : '--write', ...(all ? ['.'] : filteredPaths)],
args: [
'--ignore-unknown',
isDry || check ? '--list-different' : '--write',
...(cache ? ['--cache', '--cache-strategy', 'content'] : []),
...(all ? ['.'] : filteredPaths),
],
step: runStep,
});
} catch (e) {
Expand Down
9 changes: 8 additions & 1 deletion plugins/prettier/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ export type Options = {
name?: string | Array<string>;
/**
* When `true` or unset and run in GitHub Actions, any files failing format checks will be annotated with an error in the GitHub user interface.
* @default true
*/
githubAnnotate?: boolean;
/**
* Whether to use Prettier's built-in cache determinism.
* @default true
*/
useCache?: boolean;
};

/**
Expand Down Expand Up @@ -51,7 +57,8 @@ export function prettier(opts: Options = {}): Plugin {
(yargs) =>
builder(yargs)
.usage(`$0 ${Array.isArray(name) ? name[0] : name} [options]`)
.default('github-annotate', opts.githubAnnotate ?? true),
.default('github-annotate', opts.githubAnnotate ?? true)
.default('cache', opts.useCache ?? true),
handler,
);
},
Expand Down

0 comments on commit 0125556

Please sign in to comment.