diff --git a/src/cli/expand-patterns.js b/src/cli/expand-patterns.js index d62108f7ec95..c097b10d3870 100644 --- a/src/cli/expand-patterns.js +++ b/src/cli/expand-patterns.js @@ -1,9 +1,10 @@ "use strict"; const path = require("path"); -const { promises: fs } = require("fs"); const fastGlob = require("fast-glob"); +const { statSafe } = require("./utils.js"); + /** @typedef {import('./context').Context} Context */ /** @@ -173,22 +174,6 @@ function sortPaths(paths) { return paths.sort((a, b) => a.localeCompare(b)); } -/** - * Get stats of a given path. - * @param {string} filePath The path to target file. - * @returns {Promise} The stats. - */ -async function statSafe(filePath) { - try { - return await fs.stat(filePath); - } catch (error) { - /* istanbul ignore next */ - if (error.code !== "ENOENT") { - throw error; - } - } -} - /** * This function should be replaced with `fastGlob.escapePath` when these issues are fixed: * - https://github.com/mrmlnc/fast-glob/issues/261 diff --git a/src/cli/format.js b/src/cli/format.js index ef81f6ed2881..99209da6af7e 100644 --- a/src/cli/format.js +++ b/src/cli/format.js @@ -17,6 +17,7 @@ const getOptionsForFile = require("./options/get-options-for-file.js"); const isTTY = require("./is-tty.js"); const findCacheFile = require("./find-cache-file.js"); const FormatResultsCache = require("./format-results-cache.js"); +const { statSafe } = require("./utils.js"); function diff(a, b) { return require("diff").createTwoFilesPatch("", "", a, b, "", "", { @@ -297,9 +298,16 @@ async function formatFiles(context) { context.logger.log("Checking formatting..."); } - const formatResultsCache = context.argv.cache - ? new FormatResultsCache(findCacheFile()) - : undefined; + let formatResultsCache; + const cacheFilePath = findCacheFile(); + if (context.argv.cache) { + formatResultsCache = new FormatResultsCache(cacheFilePath); + } else { + const stat = await statSafe(cacheFilePath); + if (stat) { + await fs.unlink(cacheFilePath); + } + } for await (const pathOrError of expandPatterns(context)) { if (typeof pathOrError === "object") { diff --git a/src/cli/utils.js b/src/cli/utils.js index dc71b97a2b46..9eb93b3c06a3 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -1,5 +1,7 @@ "use strict"; +const { promises: fs } = require("fs"); + // eslint-disable-next-line no-restricted-modules const { default: sdbm } = require("../../vendors/sdbm.js"); @@ -49,4 +51,20 @@ function createHash(source) { return sdbm(source).toString(); } -module.exports = { printToScreen, groupBy, pick, createHash }; +/** + * Get stats of a given path. + * @param {string} filePath The path to target file. + * @returns {Promise} The stats. + */ +async function statSafe(filePath) { + try { + return await fs.stat(filePath); + } catch (error) { + /* istanbul ignore next */ + if (error.code !== "ENOENT") { + throw error; + } + } +} + +module.exports = { printToScreen, groupBy, pick, createHash, statSafe }; diff --git a/tests/integration/__tests__/cache.js b/tests/integration/__tests__/cache.js index e2dd9a6be7f8..cebcd00d0172 100644 --- a/tests/integration/__tests__/cache.js +++ b/tests/integration/__tests__/cache.js @@ -124,4 +124,11 @@ describe("--cache option", () => { ]) ); }); + + it("removes cache file when run Prettier without `--cache` option", async () => { + await runPrettier(dir, ["--cache", "--write", "."]); + await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError(); + await runPrettier(dir, ["--write", "."]); + await expect(fs.stat(defaultCacheFile)).rejects.toThrowError(); + }); });