-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add --cache-location
option
#13019
Merged
Merged
Add --cache-location
option
#13019
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e820934
Implement searching cache file from `--cache-location` option
sosukesuzuki 6c892a1
Add tests
sosukesuzuki 92eda3a
Add docs
sosukesuzuki f212950
Fix
sosukesuzuki e91420f
Fix doc
sosukesuzuki ec9eda7
Add changelog
sosukesuzuki ce2704f
Update snapshots
sosukesuzuki c0e7dc6
Update docs
sosukesuzuki 4b252dd
Update docs/cli.md
sosukesuzuki f5142ca
Fix changelog
sosukesuzuki a541b2e
Remove directory support
sosukesuzuki a0e611c
Update docs
sosukesuzuki ca0ed76
Update tests
sosukesuzuki f83a70a
Update docs
sosukesuzuki e3bd652
Add validation for dir
sosukesuzuki 6e680da
Fix comment
sosukesuzuki 2e228d9
Update option description
sosukesuzuki afcbd3c
Validate JSON
sosukesuzuki c5b2063
Use `path.resolve`
sosukesuzuki File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,9 @@ | ||
#### Add `--cache-location` option (#13019 by @sosukesuzuki) | ||
|
||
Path to the cache file location used by `--cache` flag. If you don't explicit `--cache-location`, Prettier saves cache file at `./node_modules/.cache/prettier/.prettier-cache`. | ||
|
||
If a file path is passed, that file is used as the cache file. | ||
|
||
```bash | ||
prettier --write --cache --cache-location=my_cache_file 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
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 |
---|---|---|
@@ -1,19 +1,51 @@ | ||
"use strict"; | ||
|
||
const fs = require("fs").promises; | ||
const os = require("os"); | ||
const path = require("path"); | ||
const findCacheDir = require("find-cache-dir"); | ||
const { statSafe, isJson } = require("./utils.js"); | ||
|
||
/** | ||
* Find default cache file (`./node_modules/.cache/prettier/.prettier-cache`) using https://github.com/avajs/find-cache-dir | ||
* | ||
* @returns {string} | ||
*/ | ||
function findCacheFile() { | ||
function findDefaultCacheFile() { | ||
const cacheDir = | ||
findCacheDir({ name: "prettier", create: true }) || os.tmpdir(); | ||
const cacheFilePath = path.join(cacheDir, ".prettier-cache"); | ||
return cacheFilePath; | ||
} | ||
|
||
async function findCacheFileFromOption(cacheLocation) { | ||
const cacheFile = path.resolve(cacheLocation); | ||
|
||
const stat = await statSafe(cacheFile); | ||
if (stat) { | ||
if (stat.isDirectory()) { | ||
throw new Error( | ||
`Resolved --cache-location '${cacheFile}' is a directory` | ||
); | ||
} | ||
|
||
const data = await fs.readFile(cacheFile, "utf8"); | ||
if (!isJson(data)) { | ||
throw new Error(`'${cacheFile}' isn't a valid JSON file`); | ||
} | ||
} | ||
|
||
return cacheFile; | ||
} | ||
|
||
/** | ||
* @param {string | undefined} cacheLocation | ||
* @returns {Promise<string>} | ||
*/ | ||
async function findCacheFile(cacheLocation) { | ||
if (!cacheLocation) { | ||
return findDefaultCacheFile(); | ||
} | ||
const cacheFile = await findCacheFileFromOption(cacheLocation); | ||
return cacheFile; | ||
} | ||
|
||
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
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
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I didn't get this wrong, this line is going to remove default cache file when running Prettier without
--cache
, but I don't think we should. People may use Prettier without--cache
temporarily.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a direct implementation of ESLint's behavior and has nothing to do with --cache-location. It will be fixed in another PR.