Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit dd549da

Browse files
committed
fix(userSettings): Don't fail when user settings file is not a valid json
In case when user executes some command and by some reasons user settings file is not a valid json, {N} CLI throws "Unexpected token in JSON at position 0" error. After that user is not able to execute any cli command until the file exists.
1 parent a8b2bc8 commit dd549da

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

services/user-settings-service.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as path from "path";
2+
import { parseJson } from "../helpers";
23

34
export class UserSettingsServiceBase implements IUserSettingsService {
45
private userSettingsFilePath: string = null;
@@ -9,7 +10,8 @@ export class UserSettingsServiceBase implements IUserSettingsService {
910

1011
constructor(userSettingsFilePath: string,
1112
protected $fs: IFileSystem,
12-
protected $lockfile: ILockFile) {
13+
protected $lockfile: ILockFile,
14+
private $logger: ILogger) {
1315
this.userSettingsFilePath = userSettingsFilePath;
1416
}
1517

@@ -38,7 +40,6 @@ export class UserSettingsServiceBase implements IUserSettingsService {
3840
};
3941

4042
return this.executeActionWithLock<void>(action);
41-
4243
}
4344

4445
private async executeActionWithLock<T>(action: () => Promise<T>): Promise<T> {
@@ -90,7 +91,14 @@ export class UserSettingsServiceBase implements IUserSettingsService {
9091
}
9192
}
9293

93-
this.userSettingsData = this.$fs.readJson(this.userSettingsFilePath);
94+
const data = this.$fs.readText(this.userSettingsFilePath);
95+
96+
try {
97+
this.userSettingsData = parseJson(data);
98+
} catch (err) {
99+
this.$logger.trace(`Error while trying to parseJson ${data} data from ${this.userSettingsFilePath} file. Err is: ${err}`);
100+
this.$fs.deleteFile(this.userSettingsFilePath);
101+
}
94102
}
95103

96104
private getUnexistingDirectories(filePath: string): Array<string> {

0 commit comments

Comments
 (0)