forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemporaryState.ts
More file actions
80 lines (68 loc) · 2.72 KB
/
Copy pathtemporaryState.ts
File metadata and controls
80 lines (68 loc) · 2.72 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
72
73
74
75
76
77
78
79
80
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import Logger from './logger';
let tempState: TemporaryState | undefined;
export class TemporaryState extends vscode.Disposable {
private readonly SUBPATH = 'temp';
private readonly disposables: vscode.Disposable[] = [];
constructor(private _storageUri: vscode.Uri) {
super(() => this.disposables.forEach(disposable => disposable.dispose()));
}
private get path(): vscode.Uri {
return vscode.Uri.joinPath(this._storageUri, this.SUBPATH);
}
private addDisposable(disposable: vscode.Disposable) {
if (this.disposables.length > 30) {
const oldDisposable = this.disposables.shift();
oldDisposable?.dispose();
}
this.disposables.push(disposable);
}
private async writeState(subpath: string, filename: string, contents: Uint8Array): Promise<vscode.Uri> {
let filePath: vscode.Uri = this.path;
const workspace = (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0)
? vscode.workspace.workspaceFolders[0].name : undefined;
if (workspace) {
filePath = vscode.Uri.joinPath(filePath, workspace);
}
if (subpath) {
filePath = vscode.Uri.joinPath(filePath, subpath);
}
await vscode.workspace.fs.createDirectory(filePath);
const file = vscode.Uri.joinPath(filePath, filename);
await vscode.workspace.fs.writeFile(file, contents);
const dispose = {
dispose: () => {
return vscode.workspace.fs.delete(file, { recursive: true });
}
};
this.addDisposable(dispose);
return file;
}
static async init(context: vscode.ExtensionContext): Promise<vscode.Disposable | undefined> {
if (context.globalStorageUri && !tempState) {
tempState = new TemporaryState(context.globalStorageUri);
try {
await vscode.workspace.fs.delete(tempState.path, { recursive: true });
} catch (e) {
Logger.appendLine(`TemporaryState> Error in initialization: ${e.message}`);
}
try {
await vscode.workspace.fs.createDirectory(tempState.path);
} catch (e) {
Logger.appendLine(`TemporaryState> Error in initialization: ${e.message}`);
}
context.subscriptions.push(tempState);
return tempState;
}
}
static async write(subpath: string, filename: string, contents: Uint8Array): Promise<vscode.Uri | undefined> {
if (!tempState) {
return;
}
return tempState.writeState(subpath, filename, contents);
}
}