Skip to content

Commit

Permalink
[test] Lint JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Jul 27, 2020
1 parent 1865ee2 commit 25b1f3d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ jobs:
- run:
name: Lint
command: yarn lint:ci
- run:
name: Lint JSON
command: yarn --silent jsonlint
workflows:
version: 2
pipeline:
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@
},
"scripts": {
"deduplicate": "node scripts/deduplicate.js",
"jsonlint": "node scripts/jsonlint.js",
"hoist": "lerna bootstrap --hoist",
"bootstrap": "lerna bootstrap",
"build": "lerna run build --stream",
"start": "lerna run start --parallel",
"prettier": "node ./scripts/prettier.js",
"test": "lerna run test --parallel",
"lint": "eslint . --cache --report-unused-disable-directives --ext .js,.ts,.tsx",
"lint:ci": "eslint . --report-unused-disable-directives --ext .js,.ts,.tsx"
"lint": "yarn eslint && yarn jsonlint",
"eslint": "eslint . --cache --report-unused-disable-directives --ext .js,.ts,.tsx",
"eslint:ci": "eslint . --report-unused-disable-directives --ext .js,.ts,.tsx"
},
"setupFiles": [
"<rootDir>/src/setupTests.js"
Expand Down
44 changes: 44 additions & 0 deletions scripts/jsonlint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
});

0 comments on commit 25b1f3d

Please sign in to comment.