3
3
4
4
import * as cp from "child_process";
5
5
import * as fse from "fs-extra";
6
+ import * as os from "os";
6
7
import * as path from "path";
7
8
import * as requireFromString from "require-from-string";
9
+ import { ExtensionContext } from "vscode";
8
10
import { ConfigurationChangeEvent, Disposable, MessageItem, window, workspace, WorkspaceConfiguration } from "vscode";
9
- import { Endpoint, IProblem, supportedPlugins } from "./shared";
11
+ import { Endpoint, IProblem, leetcodeHasInited, supportedPlugins } from "./shared";
10
12
import { executeCommand, executeCommandWithProgress } from "./utils/cpUtils";
11
13
import { DialogOptions, openUrl } from "./utils/uiUtils";
12
14
import * as wsl from "./utils/wslUtils";
@@ -34,7 +36,11 @@ class LeetCodeExecutor implements Disposable {
34
36
return `"${path.join(this.leetCodeRootPath, "bin", "leetcode")}"`;
35
37
}
36
38
37
- public async meetRequirements(): Promise<boolean> {
39
+ public async meetRequirements(context: ExtensionContext): Promise<boolean> {
40
+ const hasInited: boolean | undefined = context.globalState.get(leetcodeHasInited);
41
+ if (!hasInited) {
42
+ await this.removeOldCache();
43
+ }
38
44
if (this.nodeExecutable !== "node") {
39
45
if (!await fse.pathExists(this.nodeExecutable)) {
40
46
throw new Error(`The Node.js executable does not exist on path ${this.nodeExecutable}`);
@@ -60,10 +66,13 @@ class LeetCodeExecutor implements Disposable {
60
66
for (const plugin of supportedPlugins) {
61
67
try { // Check plugin
62
68
await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-e", plugin]);
63
- } catch (error) { // Download plugin and activate
69
+ } catch (error) { // Remove old cache that may cause the error download plugin and activate
70
+ await this.removeOldCache();
64
71
await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-i", plugin]);
65
72
}
66
73
}
74
+ // Set the global state HasInited true to skip delete old cache after init
75
+ context.globalState.update(leetcodeHasInited, true);
67
76
return true;
68
77
}
69
78
@@ -76,7 +85,7 @@ class LeetCodeExecutor implements Disposable {
76
85
}
77
86
78
87
public async signOut(): Promise<string> {
79
- return await await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "user", "-L"]);
88
+ return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "user", "-L"]);
80
89
}
81
90
82
91
public async listProblems(showLocked: boolean): Promise<string> {
@@ -194,6 +203,14 @@ class LeetCodeExecutor implements Disposable {
194
203
}
195
204
return await executeCommandWithProgress(message, command, args, options);
196
205
}
206
+
207
+ private async removeOldCache(): Promise<void> {
208
+ const oldPath: string = path.join(os.homedir(), ".lc");
209
+ if (await fse.pathExists(oldPath)) {
210
+ await fse.remove(oldPath);
211
+ }
212
+ }
213
+
197
214
}
198
215
199
216
export const leetCodeExecutor: LeetCodeExecutor = new LeetCodeExecutor();
0 commit comments