Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding yaml support #79

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"commander": "^8.0.0",
"dayjs": "^1.10.6",
"lodash.get": "^4.4.2",
"table": "^6.7.1"
"table": "^6.7.1",
"yaml": "^2.1.0"
},
"devDependencies": {
"@types/chai": "^4.2.19",
Expand Down
46 changes: 42 additions & 4 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import YAML from 'yaml';

// TODO: This might be unused
/**
* @param {String | Number | Null | Boolean} value The input number
Expand All @@ -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<Boolean>} The first boolean determines if the input string was valid, the second if it is yaml or not
*/
export function getValidStatusAndType(string: string): Array<boolean> {
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;
Expand Down
16 changes: 12 additions & 4 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}
Expand Down