Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Validate JSON
  • Loading branch information
sosukesuzuki committed Jul 16, 2022
commit afcbd3ca39296a3bc707aa8b9d7475bda27e3816
17 changes: 14 additions & 3 deletions src/cli/find-cache-file.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict";

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

/**
* Find default cache file (`./node_modules/.cache/prettier/.prettier-cache`) using https://github.com/avajs/find-cache-dir
Expand All @@ -21,9 +22,19 @@ async function findCacheFileFromOption(cacheLocation) {
const cacheFile = path.join(cwd, normalized);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we run path.isAbsolute() check first? Not sure how it works for /foo and D:\foo(Windows, different partition))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.cwd() returns absolute path. So I don't think we should run path.isAbsolute check.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't mean check cwd, cacheLocation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe path.resolve(cacheLocation) is enough.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const stat = await statSafe(cacheFile);
if (stat && stat.isDirectory()) {
throw new Error(`Resolved --cache-location '${cacheFile}' is a directory`);
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;
}

Expand Down
15 changes: 14 additions & 1 deletion src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,17 @@ async function statSafe(filePath) {
}
}

module.exports = { printToScreen, groupBy, pick, createHash, statSafe };
/**
* @param {string} value
* @returns {boolean}
*/
function isJson(value) {
try {
JSON.parse(value);
return true;
} catch {
return false;
}
}

module.exports = { printToScreen, groupBy, pick, createHash, statSafe, isJson };
12 changes: 12 additions & 0 deletions tests/integration/__tests__/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,18 @@ describe("--cache option", () => {
);
});

it("throws error for invalid JSON file", async () => {
const { stderr } = await runPrettier(dir, [
"--cache",
"--cache-location",
"a.js",
".",
]);
expect(stripAnsi(stderr).trim()).toEqual(
expect.stringMatching(/\[error] '.+' isn't a valid JSON file/)
);
});

describe("file", () => {
it("creates the cache file at location specified by `--cache-location`", async () => {
await expect(fs.stat(nonDefaultCacheFilePath)).rejects.toHaveProperty(
Expand Down