Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Install `file-entry-cache` and types * Implement `FormatResultsCache` * Add `--cache` and `--cache-location` option * Implement `--cache` and `--cache-location` * Add tests for `--cache` * Add tests for `--cache-location` * Avoid spellcheck error * Update snapshots for new options * Add `groupBy` for website * Use `fs.promises` instead of `fs/promises` * Use `rimraf` instead of `fs.rm` * Fix properties order of `options` * Install `find-cache-dir` and types * Change default cache file location to `node_modules/.cache/prettier` * Fix tests following to change default cache file location * Update snapshots for changing default cache file location * Set `os.tmpdir()` as a fallback for default cache dir * Add docs for `--cache` and `--cache-location` * Use `sdbm` instead of `node:crypto` * Add changelog * Fix `getHashOfOptions` * Remove `cache-location` * `prettiercache` -> `prettier-cache` * Remove cache file when run Prettier without `--cache` option * Implement `--cache-strategy` * Add tests for invalid cache strategy * Fix lint problems * Tweaks tests * Add tests for timestamp * Add tests for `--cache-strategy` * Add docs for cache-strategy * Address review * Fix `findCacheFile` * Use Set.prototype.has * Throw error with --stdin-filepath * Update snapshots * Fix error for cache and stdin * Use string constructor instead of toString * Update changelog * Use flag validation * Fix `cache-strategy` definition * Update docs * Mark highlihgt * Throw error for `--cache-strategy` without `--cache` * Update docs * Fix typo * Updates snapshots * Update docs/cli.md Co-authored-by: Simon Lydell <simon.lydell@gmail.com> * Fix error message * Remove `:::` syntax * Defaults `content` * Update docs * Fix by Prettier Co-authored-by: Simon Lydell <simon.lydell@gmail.com>
- Loading branch information
1 parent
97bab8f
commit fdccc0f
Showing
24 changed files
with
811 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#### [HIGHLIGHT]Add `--cache` and `--cache-strategy` CLI option (#12800 by @sosukesuzuki) | ||
|
||
Two new CLI options have been added for a caching system similar to [ESLint's one](https://eslint.org/docs/user-guide/command-line-interface#caching). | ||
|
||
##### `--cache` | ||
|
||
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 `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, `content` will be used. | ||
|
||
```bash | ||
prettier --write --cache --cache-strategy metadata src | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -289,6 +289,7 @@ | |
"sandhose", | ||
"Sapegin", | ||
"sbdchd", | ||
"sdbm", | ||
"scandir", | ||
"Serializers", | ||
"setlocal", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"use strict"; | ||
|
||
const os = require("os"); | ||
const path = require("path"); | ||
const findCacheDir = require("find-cache-dir"); | ||
|
||
/** | ||
* Find default cache file (`./node_modules/.cache/prettier/.prettier-cache`) using https://github.com/avajs/find-cache-dir | ||
* | ||
* @returns {string} | ||
*/ | ||
function findCacheFile() { | ||
const cacheDir = | ||
findCacheDir({ name: "prettier", create: true }) || os.tmpdir(); | ||
const cacheFilePath = path.join(cacheDir, ".prettier-cache"); | ||
return cacheFilePath; | ||
} | ||
|
||
module.exports = findCacheFile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"use strict"; | ||
|
||
// Inspired by LintResultsCache from ESLint | ||
// https://github.com/eslint/eslint/blob/c2d0a830754b6099a3325e6d3348c3ba983a677a/lib/cli-engine/lint-result-cache.js | ||
|
||
const fileEntryCache = require("file-entry-cache"); | ||
const stringify = require("fast-json-stable-stringify"); | ||
// eslint-disable-next-line no-restricted-modules | ||
const { version: prettierVersion } = require("../index.js"); | ||
const { createHash } = require("./utils.js"); | ||
|
||
const optionsHashCache = new WeakMap(); | ||
const nodeVersion = process && process.version; | ||
|
||
/** | ||
* @param {*} options | ||
* @returns {string} | ||
*/ | ||
function getHashOfOptions(options) { | ||
if (optionsHashCache.has(options)) { | ||
return optionsHashCache.get(options); | ||
} | ||
const hash = createHash( | ||
`${prettierVersion}_${nodeVersion}_${stringify(options)}` | ||
); | ||
optionsHashCache.set(options, hash); | ||
return hash; | ||
} | ||
|
||
/** | ||
* @typedef {{ hashOfOptions?: string }} OurMeta | ||
* @typedef {import("file-entry-cache").FileDescriptor} FileDescriptor | ||
* | ||
* @param {import("file-entry-cache").FileDescriptor} fileDescriptor | ||
* @returns {FileDescriptor["meta"] & OurMeta} | ||
*/ | ||
function getMetadataFromFileDescriptor(fileDescriptor) { | ||
return fileDescriptor.meta; | ||
} | ||
|
||
class FormatResultsCache { | ||
/** | ||
* @param {string} cacheFileLocation The path of cache file location. (default: `node_modules/.cache/prettier/prettier-cache`) | ||
* @param {string} cacheStrategy | ||
*/ | ||
constructor(cacheFileLocation, cacheStrategy) { | ||
const useChecksum = cacheStrategy === "content"; | ||
|
||
this.cacheFileLocation = cacheFileLocation; | ||
this.fileEntryCache = fileEntryCache.create( | ||
/* cacheId */ cacheFileLocation, | ||
/* directory */ undefined, | ||
useChecksum | ||
); | ||
} | ||
|
||
/** | ||
* @param {string} filePath | ||
* @param {any} options | ||
*/ | ||
existsAvailableFormatResultsCache(filePath, options) { | ||
const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath); | ||
const hashOfOptions = getHashOfOptions(options); | ||
const meta = getMetadataFromFileDescriptor(fileDescriptor); | ||
const changed = | ||
fileDescriptor.changed || meta.hashOfOptions !== hashOfOptions; | ||
|
||
if (fileDescriptor.notFound) { | ||
return false; | ||
} | ||
|
||
if (changed) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param {string} filePath | ||
* @param {any} options | ||
*/ | ||
setFormatResultsCache(filePath, options) { | ||
const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath); | ||
const meta = getMetadataFromFileDescriptor(fileDescriptor); | ||
if (fileDescriptor && !fileDescriptor.notFound) { | ||
meta.hashOfOptions = getHashOfOptions(options); | ||
} | ||
} | ||
|
||
reconcile() { | ||
this.fileEntryCache.reconcile(); | ||
} | ||
} | ||
|
||
module.exports = FormatResultsCache; |
Oops, something went wrong.