Skip to content

Commit

Permalink
Defaults content
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Jun 11, 2022
1 parent 0f57b23 commit 9d9a0f1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
7 changes: 4 additions & 3 deletions changelog_unreleased/cli/12800.md
Expand Up @@ -6,19 +6,20 @@ Two new CLI options have been added for a caching system similar to [ESLint's on

If this option is enabled, the following values are used as cache keys and the file is formatted only if one of them is changed.

- Prettier version
- Options
- Node.js version
- (if `--cache-strategy` is `metadata`) file metadata, such as timestamps
- (if `--cache-strategy` is `content`) content of the file
- (if `--cache-strategy` is `metadata`) file metadata, such as timestamps

```bash
prettier --write --cache src
```

##### `--cache-strategy`

Strategy for the cache to use for detecting changed files. Can be either `metadata` or `content`. If no strategy is specified, `metadata` will be used.
Strategy for the cache to use for detecting changed files. Can be either `metadata` or `content`. If no strategy is specified, `content` will be used.

```bash
prettier --write --cache --cache-strategy content src
prettier --write --cache --cache-strategy metadata src
```
4 changes: 2 additions & 2 deletions docs/cli.md
Expand Up @@ -227,8 +227,8 @@ Also, since the cache file is stored in `./node_modules/.cache/prettier/.prettie
## `--cache-strategy`

Strategy for the cache to use for detecting changed files. Can be either `metadata` or `content`. If no strategy is specified, `metadata` will be used.
Strategy for the cache to use for detecting changed files. Can be either `metadata` or `content`. If no strategy is specified, `content` will be used.

```bash
prettier --write --cache --cache-strategy content src
prettier --write --cache --cache-strategy metadata src
```
2 changes: 1 addition & 1 deletion src/cli/format.js
Expand Up @@ -303,7 +303,7 @@ async function formatFiles(context) {
if (context.argv.cache) {
formatResultsCache = new FormatResultsCache(
cacheFilePath,
context.argv.cacheStrategy || "metadata"
context.argv.cacheStrategy || "content"
);
} else {
if (context.argv.cacheStrategy) {
Expand Down
26 changes: 24 additions & 2 deletions tests/integration/__tests__/cache.js
Expand Up @@ -74,14 +74,16 @@ describe("--cache option", () => {
"code",
"ENOENT"
);
await runPrettier(dir, ["--cache", "."]);
await runPrettier(dir, ["--cache", "--cache-strategy", "metadata", "."]);
await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError();
});

it("does'nt format when cache is available", async () => {
const { stdout: firstStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
Expand All @@ -94,6 +96,8 @@ describe("--cache option", () => {
const { stdout: secondStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
Expand All @@ -108,6 +112,8 @@ describe("--cache option", () => {
const { stdout: firstStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
Expand All @@ -123,6 +129,8 @@ describe("--cache option", () => {
const { stdout: secondStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
Expand All @@ -138,6 +146,8 @@ describe("--cache option", () => {
const { stdout: firstStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
Expand All @@ -154,6 +164,8 @@ describe("--cache option", () => {
const { stdout: secondStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
Expand All @@ -169,6 +181,8 @@ describe("--cache option", () => {
const { stdout: firstStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
Expand All @@ -180,6 +194,8 @@ describe("--cache option", () => {

const { stdout: secondStdout } = await runPrettier(dir, [
"--cache",
"--cache-strategy",
"metadata",
"--write",
"--trailing-comma",
"all",
Expand All @@ -194,7 +210,13 @@ describe("--cache option", () => {
});

it("removes cache file when run Prettier without `--cache` option", async () => {
await runPrettier(dir, ["--cache", "--write", "."]);
await runPrettier(dir, [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError();
await runPrettier(dir, ["--write", "."]);
await expect(fs.stat(defaultCacheFile)).rejects.toThrowError();
Expand Down

0 comments on commit 9d9a0f1

Please sign in to comment.