/
checkJSONValidity.js
45 lines (40 loc) · 1.1 KB
/
checkJSONValidity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"use strict";
const fs = require("fs");
const path = require("path");
const { argv } = require("process");
if (argv.length != 3) {
console.error(
"This script takes exactly one argument: `node checkJSONValidity.js <path_to_directory>"
);
process.exit(1);
}
console.log(
`Recursively validating JSON files in the '${argv[2]}' directory...`
);
// see https://stackoverflow.com/a/66083078/11046178
function* walkSync(dir) {
const files = fs.readdirSync(dir, { withFileTypes: true });
for (const file of files) {
if (file.isDirectory()) {
yield* walkSync(path.join(dir, file.name));
} else {
if (file.name.split(".").pop() === "json") {
yield path.join(dir, file.name);
} else {
continue;
}
}
}
}
// for each files in locales directory, check json validity
for (const filePath of walkSync(`${argv[2]}`)) {
try {
let rawdata = fs.readFileSync(filePath);
JSON.parse(rawdata);
} catch (e) {
console.error(`Error while parsing json file '${filePath}'`, e);
return 1;
}
}
console.log(`Success! Every JSON files are valid.`);
return 0;