From 8a7c2880ac3a28f1d2c3b76ed164ffe45381e9e6 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Fri, 24 Jan 2020 12:46:54 +0100 Subject: [PATCH] feat(workspace): cross-os jsonlint --- .circleci/config.yml | 2 +- package.json | 5 ++--- scripts/jsonlint.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 16 ++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 scripts/jsonlint.js diff --git a/.circleci/config.yml b/.circleci/config.yml index 03c0752f927e40..687fdd63eeb1b8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -126,7 +126,7 @@ jobs: command: yarn lint:ci - run: name: Lint JSON - command: yarn jsonlint + command: yarn --silent jsonlint test_types: <<: *defaults steps: diff --git a/package.json b/package.json index 75958d611152d0..430e313d3338ac 100644 --- a/package.json +++ b/package.json @@ -21,8 +21,7 @@ "docs:typescript:formatted": "yarn workspace docs typescript:transpile", "docs:mdicons:synonyms": "babel-node --config-file ./babel.config.js ./docs/scripts/updateIconSynonyms", "framer:build": "yarn workspace framer build", - "jsonlint": "yarn --silent jsonlint:files | xargs -n1 jsonlint -q -c && echo \"jsonlint: no lint errors\"", - "jsonlint:files": "find . -name \"*.json\" | grep -v -f .eslintignore", + "jsonlint": "node scripts/jsonlint.js", "lint": "eslint . --cache --report-unused-disable-directives", "lint:ci": "eslint . --report-unused-disable-directives", "lint:fix": "eslint . --cache --fix", @@ -79,6 +78,7 @@ "babel-plugin-transform-react-remove-prop-types": "^0.4.21", "chai": "^4.1.2", "chai-dom": "^1.8.1", + "chalk": "^3.0.0", "compression-webpack-plugin": "^3.0.0", "confusing-browser-globals": "^1.0.9", "cross-env": "^6.0.0", @@ -102,7 +102,6 @@ "glob": "^7.1.2", "glob-gitignore": "^1.0.11", "jsdom": "^16.0.0", - "jsonlint": "^1.6.3", "karma": "^4.3.0", "karma-browserstack-launcher": "~1.4.0", "karma-chrome-launcher": "^3.0.0", diff --git a/scripts/jsonlint.js b/scripts/jsonlint.js new file mode 100644 index 00000000000000..fa5548a078c706 --- /dev/null +++ b/scripts/jsonlint.js @@ -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 = chalk.gray; +const failMessage = chalk.whiteBright; + +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, + }); + + 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); +}); diff --git a/yarn.lock b/yarn.lock index e078a1f127884e..5abe69e90b785c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2965,6 +2965,14 @@ ansi-styles@^4.0.0: "@types/color-name" "^1.1.1" color-convert "^2.0.1" +ansi-styles@^4.1.0: + version "4.2.1" + resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.2.1.tgz#90ae75c424d008d2624c5bf29ead3177ebfcf359" + integrity sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA== + dependencies: + "@types/color-name" "^1.1.1" + color-convert "^2.0.1" + ansi-styles@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-1.0.0.tgz#cb102df1c56f5123eab8b67cd7b98027a0279178" @@ -4228,6 +4236,14 @@ chalk@^1.0.0, chalk@^1.1.3: strip-ansi "^3.0.0" supports-color "^2.0.0" +chalk@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4" + integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg== + dependencies: + ansi-styles "^4.1.0" + supports-color "^7.1.0" + chalk@~0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/chalk/-/chalk-0.4.0.tgz#5199a3ddcd0c1efe23bc08c1b027b06176e0c64f"