Skip to content

Commit c2711eb

Browse files
committed
fix: JSON file reading with optional null return for missing files.
- Moved the check for package.json file existing (`fs.existsSync`) in `Configruation::readExtensionsFromDirectory` method into the `readJsonFile` utility function. This is so that the util function can check if a JSON file exists before trying to read it. If file doesn't exist then it just returns `null`. - Removed the `fs.existsSync` check and replaced it with a `null` check in the `readExtensionsFromDirectory` method. - Refactored `readJsonFile` util function to accept a new `throwOnFileMissing` param with a default value of `true`. This param, along with it's accompanying logic, will throw an error if a file is missing (doesn't exist) and when the param is `true, instead of returning `null`. This allows only specific usages of the util function to return `null` instead of error throwing when the param is set to `false`. Thus being backward compatibile with other usages.
1 parent 6f77cc5 commit c2711eb

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

src/configuration.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,16 +495,17 @@ export class Configuration {
495495

496496
// Get the package.json file path.
497497
const packageJSONPath = path.join(extensionPath, "package.json");
498+
const packageJSON: IPackageJson = utils.readJsonFile(packageJSONPath, false);
498499

499-
// If the package.json file exists...
500-
if (fs.existsSync(packageJSONPath)) {
501-
const packageJSON: IPackageJson = utils.readJsonFile(packageJSONPath);
500+
if (packageJSON === null) {
501+
return;
502+
}
502503

503-
const id = `${packageJSON.publisher}.${packageJSON.name}`;
504+
const id = `${packageJSON.publisher}.${packageJSON.name}`;
504505

505-
// Push the extension data object into the array.
506-
foundExtensions.push({id, extensionPath, packageJSON});
507-
}
506+
// Push the extension data object into the array.
507+
foundExtensions.push({id, extensionPath, packageJSON});
508+
508509
}
509510
});
510511

src/utils.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,26 @@ import {window} from "vscode";
77
* Read the file and parse the JSON.
88
*
99
* @param {string} filepath The path of the file.
10+
* @param {boolean} [throwOnFileMissing=true] Whether to throw an error if the file doesn't exist.
11+
* If `false`, returns `null`. Default is `true`.
1012
*
11-
* @returns {any} The JSON file content as an object.
12-
* @throws Will throw an error if the JSON file cannot be parsed.
13+
* @returns {any | null} The JSON file content as an object, or `null` if file doesn't exist and `throwOnFileMissing` is `false`.
14+
* @throws Will throw an error if the JSON file cannot be parsed or if file doesn't exist and `throwOnFileMissing` is `true`.
1315
*/
14-
export function readJsonFile(filepath: string): any {
16+
export function readJsonFile(filepath: string, throwOnFileMissing: boolean = true): any | null {
17+
// Check if file exists first.
18+
// If file doesn't exist...
19+
if (!fs.existsSync(filepath)) {
20+
// If throwOnFileMissing param is true, throw an error.
21+
if (throwOnFileMissing) {
22+
const error = new Error(`JSON file not found: "${filepath}"`);
23+
logger.error(error.stack);
24+
throw error;
25+
}
26+
// Otherwise just return null.
27+
return null;
28+
}
29+
1530
const jsonErrors: jsonc.ParseError[] = [];
1631

1732
const fileContent = fs
@@ -31,7 +46,7 @@ export function readJsonFile(filepath: string): any {
3146
.showErrorMessage(
3247
`${errorMsg}. The extension cannot continue. Please check the "Auto Comment Blocks" Output Channel for errors.`,
3348
"OK",
34-
"Open Output Channel"
49+
"Open Output Channel",
3550
)
3651
.then((selection) => {
3752
if (selection === "Open Output Channel") {

0 commit comments

Comments
 (0)