-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathjsonlint.js
44 lines (37 loc) · 1.29 KB
/
jsonlint.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
/* eslint-disable no-console */
const chalk = require('chalk');
const fse = require('fs-extra');
const glob = require('glob-gitignore');
const path = require('path');
const passMessage = (message) => `✓ ${chalk.gray(message)}`;
const failMessage = (message) => `✗ ${chalk.whiteBright(message)}`;
async function run() {
const workspaceRoot = path.resolve(__dirname, '..');
const eslintignoreContent = await fse.readFile(path.join(workspaceRoot, '.eslintignore'), {
encoding: 'utf8',
});
const eslintignore = eslintignoreContent.split(/\r?\n/).slice(0, -1);
const filenames = glob.sync('**/*.json', {
cwd: workspaceRoot,
ignore: [...eslintignore, 'tsconfig*.json', 'tslint.json'],
});
let passed = true;
const checks = filenames.map(async (filename) => {
const content = await fse.readFile(path.join(workspaceRoot, filename), { encoding: 'utf8' });
try {
JSON.parse(content);
console.log(passMessage(filename));
} catch (error) {
passed = false;
console.error(failMessage(`Error parsing ${filename}:\n\n${String(error)}`));
}
});
await Promise.all(checks);
if (passed === false) {
throw new Error('At least one file did not pass. Check the console output');
}
}
run().catch((error) => {
console.error(error);
process.exit(1);
});