From ebff1e8b88cb4bb21c2d69258f2850e3850ab88d Mon Sep 17 00:00:00 2001 From: Maximilian Berkmann Date: Mon, 20 May 2019 09:47:51 +0100 Subject: [PATCH] feat: added a JSON handler/fixer (#165) * feat: added a JSON handler/fixer [wip] Added a module that will check JSON files and give a good level of details for errors while attempting to fix it re https://github.com/all-contributors/all-contributors-bot/issues/102 * refactor: consolidated the fix handler Made the fixer a bit more concise and added the fields to allow for an auto-update of the config file (if it was changed) * chore(package): use another `jsonlint` * refactor(config-file): use `json-fixer` * chore(package): bump `json-fixer` to 1.3 * chore(package): bumped `json-fixer` --- .gitignore | 2 ++ package.json | 1 + src/util/config-file.js | 7 ++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 09048d2..ab4f4cb 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ dist # when working with contributors package-lock.json yarn.lock +.vscode +cache diff --git a/package.json b/package.json index bf26862..6094ff1 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "chalk": "^2.3.0", "didyoumean": "^1.2.1", "inquirer": "^6.2.1", + "json-fixer": "^1.3.1-0", "lodash": "^4.11.2", "pify": "^4.0.1", "request": "^2.72.0", diff --git a/src/util/config-file.js b/src/util/config-file.js index dd68db8..912d95f 100644 --- a/src/util/config-file.js +++ b/src/util/config-file.js @@ -1,16 +1,21 @@ const fs = require('fs') const pify = require('pify') const _ = require('lodash/fp') +const jf = require('json-fixer') function readConfig(configPath) { try { - const config = JSON.parse(fs.readFileSync(configPath, 'utf-8')) + const {data: config, changed} = jf(fs.readFileSync(configPath, 'utf-8')) if (!('repoType' in config)) { config.repoType = 'github' } if (!('commitConvention' in config)) { config.commitConvention = 'none' } + if (changed) { + //Updates the file with fixes + fs.writeFileSync(configPath, JSON.stringify(config, null, 2)) + } return config } catch (error) { if (error instanceof SyntaxError) {