forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
--cache-location
option (prettier#13019)
* Implement searching cache file from `--cache-location` option * Add tests * Add docs * Fix * Fix doc * Add changelog * Update snapshots * Update docs * Update docs/cli.md Co-authored-by: Holger Jeromin <mailgithub@katur.de> * Fix changelog * Remove directory support * Update docs * Update tests * Update docs * Add validation for dir * Fix comment * Update option description * Validate JSON * Use `path.resolve` Co-authored-by: Holger Jeromin <mailgithub@katur.de>
- Loading branch information
Showing
10 changed files
with
188 additions
and
8 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,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.