Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4b89d96

Browse files
committed
fix: #593 by adding global flag to remove cache
1 parent 5fa449e commit 4b89d96

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { markdownEngine } from "./webview/markdownEngine";
2626

2727
export async function activate(context: vscode.ExtensionContext): Promise<void> {
2828
try {
29-
if (!await leetCodeExecutor.meetRequirements()) {
29+
if (!await leetCodeExecutor.meetRequirements(context)) {
3030
throw new Error("The environment doesn't meet requirements.");
3131
}
3232

src/leetCodeExecutor.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
import * as cp from "child_process";
55
import * as fse from "fs-extra";
6+
import * as os from "os";
67
import * as path from "path";
78
import * as requireFromString from "require-from-string";
9+
import { ExtensionContext } from "vscode";
810
import { ConfigurationChangeEvent, Disposable, MessageItem, window, workspace, WorkspaceConfiguration } from "vscode";
9-
import { Endpoint, IProblem, supportedPlugins } from "./shared";
11+
import { Endpoint, globalStateLeetcodeIsUserFresh, IProblem, supportedPlugins } from "./shared";
1012
import { executeCommand, executeCommandWithProgress } from "./utils/cpUtils";
1113
import { DialogOptions, openUrl } from "./utils/uiUtils";
1214
import * as wsl from "./utils/wslUtils";
@@ -34,7 +36,11 @@ class LeetCodeExecutor implements Disposable {
3436
return `"${path.join(this.leetCodeRootPath, "bin", "leetcode")}"`;
3537
}
3638

37-
public async meetRequirements(): Promise<boolean> {
39+
public async meetRequirements(context: ExtensionContext): Promise<boolean> {
40+
const isUserFresh: boolean | undefined = context.globalState.get(globalStateLeetcodeIsUserFresh);
41+
if (isUserFresh !== false) {
42+
await this.removeOldCache();
43+
}
3844
if (this.nodeExecutable !== "node") {
3945
if (!await fse.pathExists(this.nodeExecutable)) {
4046
throw new Error(`The Node.js executable does not exist on path ${this.nodeExecutable}`);
@@ -61,9 +67,11 @@ class LeetCodeExecutor implements Disposable {
6167
try { // Check plugin
6268
await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-e", plugin]);
6369
} catch (error) { // Download plugin and activate
70+
await this.removeOldCache();
6471
await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-i", plugin]);
6572
}
6673
}
74+
context.globalState.update(globalStateLeetcodeIsUserFresh, false);
6775
return true;
6876
}
6977

@@ -194,6 +202,14 @@ class LeetCodeExecutor implements Disposable {
194202
}
195203
return await executeCommandWithProgress(message, command, args, options);
196204
}
205+
206+
private async removeOldCache(): Promise<void> {
207+
const oldPath: string = path.join(os.homedir(), ".lc");
208+
if (await fse.pathExists(oldPath)) {
209+
await fse.remove(oldPath);
210+
}
211+
}
212+
197213
}
198214

199215
export const leetCodeExecutor: LeetCodeExecutor = new LeetCodeExecutor();

src/shared.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,5 @@ export enum DescriptionConfiguration {
114114
Both = "Both",
115115
None = "None",
116116
}
117+
118+
export const globalStateLeetcodeIsUserFresh: string = "leetcode.isUserFresh";

0 commit comments

Comments
 (0)