diff --git a/package-lock.json b/package-lock.json index ef0a32d..c39a552 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,8 @@ "dayjs": "^1.10.6", "lodash.get": "^4.4.2", "semver": "^7.6.3", - "table": "^6.7.1" + "table": "^6.7.1", + "yaml": "^2.1.0" }, "bin": { "better-npm-audit": "index.js" @@ -3014,6 +3015,21 @@ "node": ">=10" } }, + "node_modules/yaml": { + "version": "2.8.2", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.8.2.tgz", + "integrity": "sha512-mplynKqc1C2hTVYxd0PU2xQAc22TI1vShAYGksCCfxbn/dFwnHTNi1bvYsBTkhdUNtGIf5xNOg938rrSSYvS9A==", + "license": "ISC", + "bin": { + "yaml": "bin.mjs" + }, + "engines": { + "node": ">= 14.6" + }, + "funding": { + "url": "https://github.com/sponsors/eemeli" + } + }, "node_modules/yargs": { "version": "16.2.0", "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", diff --git a/package.json b/package.json index 9544da0..990e362 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ "dayjs": "^1.10.6", "lodash.get": "^4.4.2", "semver": "^7.6.3", - "table": "^6.7.1" + "table": "^6.7.1", + "yaml": "^2.1.0" }, "devDependencies": { "@types/chai": "^4.2.19", diff --git a/src/utils/common.ts b/src/utils/common.ts index a3c65ff..7cccad3 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -1,3 +1,5 @@ +import YAML from 'yaml'; + // TODO: This might be unused /** * @param {String | Number | Null | Boolean} value The input number @@ -14,14 +16,50 @@ export function isWholeNumber(value: string | number | null | undefined): boolea } /** - * @param {String} string The JSON stringified object - * @return {Boolean} Returns true if the input string is parse-able + * @param {String} string The YAML stringified object + * @param {Boolean} logError A boolean that determines if the function should log a caught error to console + * @return {Boolean} Returns true if the input string is parse-able */ -export function isJsonString(string: string): boolean { +export function isYamlString(string: string, logError:boolean = true): boolean { try { - JSON.parse(string); + YAML.parse(string); + } catch (e) { + if (logError) { + console.log('Failed parsing .nsprc file: ' + e); + throw e; + } + } + return true; +} + +/** + * @param {String} string The YAML/JSON stringified object + * @return {Array} The first boolean determines if the input string was valid, the second if it is yaml or not + */ +export function getValidStatusAndType(string: string): Array { + let isYaml = false; + try { + if ((isYaml = isYamlString(string, false) || isJsonString(string, false))) { + return [true, isYaml]; + } } catch (e) { console.log('Failed parsing .nsprc file: ' + e); + } + return [false, false]; +} + +/** + * @param {String} string The JSON stringified object + * @param {Boolean} logError A boolean that determines if the function should log a caught error to console + * @return {Boolean} Returns true if the input string is parse-able + */ +export function isJsonString(string: string, logError:boolean = true): boolean { + try { + JSON.parse(string); + } catch (e) { + if (logError) { + console.log('Failed parsing .nsprc file: ' + e); + } return false; } return true; diff --git a/src/utils/file.ts b/src/utils/file.ts index 82d7461..b5e6777 100644 --- a/src/utils/file.ts +++ b/src/utils/file.ts @@ -1,6 +1,7 @@ import fs from 'fs'; import { NsprcFile } from 'src/types'; -import { isJsonString } from './common'; +import { getValidStatusAndType } from './common'; +import YAML from 'yaml'; /** * Read file from path @@ -10,10 +11,17 @@ import { isJsonString } from './common'; export function readFile(path: string): NsprcFile | boolean { try { const data = fs.readFileSync(path, 'utf8'); - if (!isJsonString(data)) { - return false; + const validAndType = getValidStatusAndType(data); + + if (validAndType[0]) { + if (validAndType[1]) { + return YAML.parse(data); + } else { + return JSON.parse(data); + } } - return JSON.parse(data); + + return false; } catch (err) { return false; }