Skip to content

Commit

Permalink
Remove cache file when run Prettier without --cache option
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed May 18, 2022
1 parent b4de3fd commit cf3450e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
19 changes: 2 additions & 17 deletions 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 */

/**
Expand Down Expand Up @@ -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<import('fs').Stats | undefined>} 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
Expand Down
14 changes: 11 additions & 3 deletions src/cli/format.js
Expand Up @@ -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, "", "", {
Expand Down Expand Up @@ -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") {
Expand Down
20 changes: 19 additions & 1 deletion 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");

Expand Down Expand Up @@ -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<import('fs').Stats | undefined>} 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 };
7 changes: 7 additions & 0 deletions tests/integration/__tests__/cache.js
Expand Up @@ -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();
});
});

0 comments on commit cf3450e

Please sign in to comment.