201
202
write(path: JSONPath, value: unknown): Promise<void> {
203
>
return this.queue.queue(() => this.doWriteConfiguration(path, value)); // queue up writes to prevent race conditions
configurationService.ts
204
>
}
205
206
private async doWriteConfiguration(path: JSONPath, value: unknown): Promise<void> {
208
>
try {
209
>
const fileContent = await this.fileService.readFile(this.settingsResource);
210
content = fileContent.value.toString();
212
>
if ((<FileOperationError>error).fileOperationResult === FileOperationResult.FILE_NOT_FOUND) {
213
>
content = '{}';
214
>
} else {
215
throw error;
216
}
218
>
219
>
const parseErrors: ParseError[] = [];
220
>
parse(content, parseErrors, { allowTrailingComma: true, allowEmptyContent: true });
221
>
if (parseErrors.length > 0) {
222
throw new Error('Unable to write into the settings file. Please open the file to correct errors/warnings in the file and try again.');
223
}
225
>
const edits = this.getEdits(content, path, value);
226
>
content = applyEdits(content, edits);
227
>
228
>
await this.fileService.writeFile(this.settingsResource, VSBuffer.fromString(content));
229
>
}
230
231
private getEdits(content: string, path: JSONPath, value: unknown): Edit[] {
233
>
234
>
// With empty path the entire file is being replaced, so we just use JSON.stringify
235
>
if (!path.length) {
236
const content = JSON.stringify(value, null, insertSpaces ? ' '.repeat(tabSize) : '\t');
237
return [{