Skip to content
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
18 changes: 17 additions & 1 deletion 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 @@ -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",
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 {
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after colon in parameter type annotation. Should be logError: boolean for consistency with TypeScript style conventions.

Copilot uses AI. Check for mistakes.
try {
JSON.parse(string);
YAML.parse(string);
} catch (e) {
if (logError) {
console.log('Failed parsing .nsprc file: ' + e);
throw e;
}
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function returns true even when YAML parsing fails (when logError is false). If parsing throws an exception and logError is false, the exception is caught but the function still returns true, incorrectly indicating valid YAML. Add return false; in the catch block before line 31.

Suggested change
}
}
return false;

Copilot uses AI. Check for mistakes.
}
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))) {
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic incorrectly determines if content is YAML. The expression isYamlString(string, false) || isJsonString(string, false) assigns the result of the OR operation to isYaml, but since valid JSON is also valid YAML, isYamlString will return true for JSON content, making isYaml always true when either format is valid. This defeats the purpose of distinguishing between formats. Consider checking JSON first, then YAML only if JSON fails.

Copilot uses AI. Check for mistakes.
return [true, isYaml];
}
} catch (e) {
console.log('Failed parsing .nsprc file: ' + e);
}
return [false, false];
Comment on lines +37 to +48
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type Array<boolean> is unclear and error-prone. Consider using a more descriptive return type such as an object with named properties like { isValid: boolean, isYaml: boolean } or a tuple type [isValid: boolean, isYaml: boolean] to make the API more self-documenting.

Suggested change
* @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];
* @return {{ isValid: boolean, isYaml: boolean }} An object where `isValid` indicates if the string was parse-able, and `isYaml` indicates if it is YAML or not
*/
export function getValidStatusAndType(string: string): { isValid: boolean; isYaml: boolean } {
let isYaml = false;
try {
if ((isYaml = isYamlString(string, false) || isJsonString(string, false))) {
return { isValid: true, isYaml };
}
} catch (e) {
console.log('Failed parsing .nsprc file: ' + e);
}
return { isValid: false, isYaml: false };

Copilot uses AI. Check for mistakes.
}

/**
* @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 {
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after colon in parameter type annotation. Should be logError: boolean for consistency with TypeScript style conventions.

Copilot uses AI. Check for mistakes.
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