forked from microsoft/vscode-copilot-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonFile.ts
More file actions
71 lines (60 loc) · 1.97 KB
/
jsonFile.ts
File metadata and controls
71 lines (60 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { readFile, writeFile } from 'fs/promises';
import { TaskQueue } from '../common/async';
import { deepClone } from '../vs/base/common/objects';
export class JSONFile<T> {
public static async readOrCreate<T>(filePath: string, initialValue: T, indent: string | number = 4): Promise<JSONFile<T>> {
let data: T = initialValue;
const result = await readFileTextOrUndefined(filePath);
if (result !== undefined) {
const parsed = tryParseJson(result) as T | undefined;
if (parsed !== undefined) {
data = parsed;
}
}
return new JSONFile(filePath, data, indent);
}
private _value: T;
public get value(): Readonly<T> { return deepClone(this._value); }
private constructor(
public readonly filePath: string,
initialValue: T,
private readonly indent: string | number = 4,
) {
this._value = initialValue;
}
private readonly _writeQueue = new TaskQueue();
async setValue(value: T): Promise<void> {
this._value = value;
this._writeQueue.clearPending();
await this._writeQueue.scheduleSkipIfCleared(() => this._write());
}
private async _write(): Promise<void> {
await writeFile(this.filePath, JSON.stringify(this._value, null, this.indent), { encoding: 'utf8' });
}
}
export async function readFileTextOrUndefined(filePath: string): Promise<string | undefined> {
try {
return await readFile(filePath, 'utf8');
}
catch (e) {
if (e.code === 'ENOENT') {
return undefined;
}
throw e;
}
}
export function tryParseJson(str: string): unknown | undefined {
try {
return JSON.parse(str);
} catch (e) {
if (e instanceof SyntaxError) {
console.error(e);
return undefined;
}
throw e;
}
}