Skip to content

Commit

Permalink
Added basic functionality that allows writing yaml in the nsprc file.…
Browse files Browse the repository at this point in the history
… This relates to issue jeemok#70.
  • Loading branch information
maxper committed May 27, 2022
1 parent c7cdc15 commit b4aaef5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 16 deletions.
21 changes: 15 additions & 6 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"commander": "^8.0.0",
"dayjs": "^1.10.6",
"js-yaml": "^4.1.0",
"lodash.get": "^4.4.2",
"table": "^6.7.1"
},
Expand Down
47 changes: 43 additions & 4 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { ExecException } from "child_process";

const { load } = require("js-yaml");

// TODO: This might be unused
/**
* @param {String | Number | Null | Boolean} value The input number
Expand All @@ -14,14 +18,49 @@ 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
* @returns {Boolean} Returns true if the input string is parse-able
*/
export function isYamlString(string: string, logError: boolean = true): boolean {
try {
load(string);
} catch(e){
if (logError){
console.log('Failed parsing .nsprc file: ' + e);
throw e;
}
}
return true;
}

/**
* @param {String} string The YAML/JSON stringified object
* @returns {Array<Boolean>} An array with two booleans where the first determines if the input string was valid and the second if the contents are 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 an error should be logged to console
* @return {Boolean} Returns true if the input string is parse-able
*/
export function isJsonString(string: string): boolean {
export function isJsonString(string: string, logError: boolean = true): boolean {
try {
JSON.parse(string);
} catch (e) {
console.log('Failed parsing .nsprc file: ' + e);
if (logError){
console.log('Failed parsing .nsprc file: ' + e);
}
return false;
}
return true;
Expand Down
21 changes: 15 additions & 6 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import fs from 'fs';
import { NsprcFile } from 'src/types';
import { isJsonString } from './common';
import { getValidStatusAndType } from './common';
const { load } = require("js-yaml");

/**
* Read file from path
* @param {String} path File path
* @return {(Object | Boolean)} Returns the parsed data if found, or else returns `false`
*/
export function readFile(path: string): NsprcFile | boolean {
try {
const data = fs.readFileSync(path, 'utf8');
if (!isJsonString(data)) {
return false;
try {
const data = fs.readFileSync(path, 'utf8');
const validAndType = getValidStatusAndType(data);

if(validAndType[0]){
if(validAndType[1]){
return load(data);
} else {
return JSON.parse(data);
}
}
return JSON.parse(data);

return false;

} catch (err) {
return false;
}
Expand Down

0 comments on commit b4aaef5

Please sign in to comment.