Skip to content

Commit

Permalink
Remove directory support
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Jul 9, 2022
1 parent 0ead20a commit 45f441b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 91 deletions.
45 changes: 5 additions & 40 deletions src/cli/find-cache-file.js
@@ -1,10 +1,8 @@
"use strict";

const { promises: fs } = require("fs");
const os = require("os");
const path = require("path");
const findCacheDir = require("find-cache-dir");
const { createHash } = require("./utils.js");

function findDefaultCacheFile() {
const cacheDir =
Expand All @@ -13,52 +11,19 @@ function findDefaultCacheFile() {
return cacheFilePath;
}

/**
* If a file path is passed, that file is used as the cache file.
* If a directory path is passed,
* a file with the name hashed process.cwd() is created in that directory and used as a cache file.
* e.g. For `--cache-location=foo/`, cache file: `./foo/.cache_139328449`
*
* @param {string} cacheLocation
* @returns {Promise<string>}
*/
async function findCacheFileFromOption(cacheLocation) {
const cwd = process.cwd();
const normalizedCacheLocation = path.normalize(cacheLocation);
const looksLikeADirectory = normalizedCacheLocation.slice(-1) === path.sep;
const resolvedCacheLocation = path.resolve(cwd, normalizedCacheLocation);
const getCacheFileForDirectory = () =>
path.join(resolvedCacheLocation, `.cache_${createHash(cwd)}`);
let fileStats;
try {
fileStats = await fs.lstat(resolvedCacheLocation);
} catch {
fileStats = null;
}
if (fileStats) {
if (fileStats.isDirectory() || looksLikeADirectory) {
return getCacheFileForDirectory();
}
return resolvedCacheLocation;
}
if (looksLikeADirectory) {
return getCacheFileForDirectory();
}
return resolvedCacheLocation;
}

/**
* Find default cache file (`./node_modules/.cache/prettier/.prettier-cache`) using https://github.com/avajs/find-cache-dir
*
* @param {string | undefined} cacheLocation
* @returns {Promise<string>}
* @returns {string}
*/
async function findCacheFile(cacheLocation) {
function findCacheFile(cacheLocation) {
if (!cacheLocation) {
return findDefaultCacheFile();
}
const cacheFilePath = await findCacheFileFromOption(cacheLocation);
return cacheFilePath;
const cwd = process.cwd();
const normalized = path.normalize(cacheLocation);
return path.join(cwd, normalized);
}

module.exports = findCacheFile;
2 changes: 1 addition & 1 deletion src/cli/format.js
Expand Up @@ -299,7 +299,7 @@ async function formatFiles(context) {
}

let formatResultsCache;
const cacheFilePath = await findCacheFile(context.argv.cacheLocation);
const cacheFilePath = findCacheFile(context.argv.cacheLocation);
if (context.argv.cache) {
formatResultsCache = new FormatResultsCache(
cacheFilePath,
Expand Down
50 changes: 0 additions & 50 deletions tests/integration/__tests__/cache.js
Expand Up @@ -444,55 +444,5 @@ describe("--cache option", () => {
);
});
});

describe("dir", () => {
it("creates the cache file at location specified by `--cache-location`", async () => {
await expect(fs.stat(nonDefaultCacheDirPath)).rejects.toHaveProperty(
"code",
"ENOENT"
);
await runPrettier(dir, [
"--cache",
"--cache-location",
`${nonDefaultCacheDirName}/`,
".",
]);
const stat = await fs.stat(nonDefaultCacheDirPath);
expect(stat.isDirectory()).toBe(true);
const filesInCacheDir = await fs.readdir(nonDefaultCacheDirPath);
expect(filesInCacheDir.length).toBe(1);
expect(filesInCacheDir[0].startsWith(".cache")).toBe(true);
});

it("does'nt format when cache is available", async () => {
const { stdout: firstStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-location",
`${nonDefaultCacheDirName}/`,
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
])
);

const { stdout: secondStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-location",
`${nonDefaultCacheDirName}/`,
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/),
expect.stringMatching(/^b\.js .+ms \(cached\)$/),
])
);
});
});
});
});

0 comments on commit 45f441b

Please sign in to comment.