From 4b89d96e2def6dd25ca0ed8feeaa566d484d4b3a Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Wed, 22 Jul 2020 12:27:38 +0800 Subject: [PATCH 1/3] fix: #593 by adding global flag to remove cache --- src/extension.ts | 2 +- src/leetCodeExecutor.ts | 20 ++++++++++++++++++-- src/shared.ts | 2 ++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 9bb3ad41..5bd026f1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -26,7 +26,7 @@ import { markdownEngine } from "./webview/markdownEngine"; export async function activate(context: vscode.ExtensionContext): Promise { try { - if (!await leetCodeExecutor.meetRequirements()) { + if (!await leetCodeExecutor.meetRequirements(context)) { throw new Error("The environment doesn't meet requirements."); } diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index 4c0aa312..b898d1f0 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -3,10 +3,12 @@ import * as cp from "child_process"; import * as fse from "fs-extra"; +import * as os from "os"; import * as path from "path"; import * as requireFromString from "require-from-string"; +import { ExtensionContext } from "vscode"; import { ConfigurationChangeEvent, Disposable, MessageItem, window, workspace, WorkspaceConfiguration } from "vscode"; -import { Endpoint, IProblem, supportedPlugins } from "./shared"; +import { Endpoint, globalStateLeetcodeIsUserFresh, IProblem, supportedPlugins } from "./shared"; import { executeCommand, executeCommandWithProgress } from "./utils/cpUtils"; import { DialogOptions, openUrl } from "./utils/uiUtils"; import * as wsl from "./utils/wslUtils"; @@ -34,7 +36,11 @@ class LeetCodeExecutor implements Disposable { return `"${path.join(this.leetCodeRootPath, "bin", "leetcode")}"`; } - public async meetRequirements(): Promise { + public async meetRequirements(context: ExtensionContext): Promise { + const isUserFresh: boolean | undefined = context.globalState.get(globalStateLeetcodeIsUserFresh); + if (isUserFresh !== false) { + await this.removeOldCache(); + } if (this.nodeExecutable !== "node") { if (!await fse.pathExists(this.nodeExecutable)) { throw new Error(`The Node.js executable does not exist on path ${this.nodeExecutable}`); @@ -61,9 +67,11 @@ class LeetCodeExecutor implements Disposable { try { // Check plugin await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-e", plugin]); } catch (error) { // Download plugin and activate + await this.removeOldCache(); await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-i", plugin]); } } + context.globalState.update(globalStateLeetcodeIsUserFresh, false); return true; } @@ -194,6 +202,14 @@ class LeetCodeExecutor implements Disposable { } return await executeCommandWithProgress(message, command, args, options); } + + private async removeOldCache(): Promise { + const oldPath: string = path.join(os.homedir(), ".lc"); + if (await fse.pathExists(oldPath)) { + await fse.remove(oldPath); + } + } + } export const leetCodeExecutor: LeetCodeExecutor = new LeetCodeExecutor(); diff --git a/src/shared.ts b/src/shared.ts index 9b6a749a..c04af084 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -114,3 +114,5 @@ export enum DescriptionConfiguration { Both = "Both", None = "None", } + +export const globalStateLeetcodeIsUserFresh: string = "leetcode.isUserFresh"; From 7f96865ead93cb79221e978ed4fd6195d39faade Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Wed, 22 Jul 2020 19:53:11 +0800 Subject: [PATCH 2/3] fix: maybe one old typo, add: some comments, change: global state logic --- src/leetCodeExecutor.ts | 13 +++++++------ src/shared.ts | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index b898d1f0..232648ca 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -8,7 +8,7 @@ import * as path from "path"; import * as requireFromString from "require-from-string"; import { ExtensionContext } from "vscode"; import { ConfigurationChangeEvent, Disposable, MessageItem, window, workspace, WorkspaceConfiguration } from "vscode"; -import { Endpoint, globalStateLeetcodeIsUserFresh, IProblem, supportedPlugins } from "./shared"; +import { Endpoint, globalStateLeetcodeHasInited, IProblem, supportedPlugins } from "./shared"; import { executeCommand, executeCommandWithProgress } from "./utils/cpUtils"; import { DialogOptions, openUrl } from "./utils/uiUtils"; import * as wsl from "./utils/wslUtils"; @@ -37,8 +37,8 @@ class LeetCodeExecutor implements Disposable { } public async meetRequirements(context: ExtensionContext): Promise { - const isUserFresh: boolean | undefined = context.globalState.get(globalStateLeetcodeIsUserFresh); - if (isUserFresh !== false) { + const hasInited: boolean | undefined = context.globalState.get(globalStateLeetcodeHasInited); + if (!hasInited) { await this.removeOldCache(); } if (this.nodeExecutable !== "node") { @@ -66,12 +66,13 @@ class LeetCodeExecutor implements Disposable { for (const plugin of supportedPlugins) { try { // Check plugin await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-e", plugin]); - } catch (error) { // Download plugin and activate + } catch (error) { // Remove old cache that may cause the error download plugin and activate await this.removeOldCache(); await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-i", plugin]); } } - context.globalState.update(globalStateLeetcodeIsUserFresh, false); + // Set the global state HasInited true to skip delete old cache after init + context.globalState.update(globalStateLeetcodeHasInited, true); return true; } @@ -84,7 +85,7 @@ class LeetCodeExecutor implements Disposable { } public async signOut(): Promise { - return await await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "user", "-L"]); + return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "user", "-L"]); } public async listProblems(showLocked: boolean): Promise { diff --git a/src/shared.ts b/src/shared.ts index c04af084..cd5b791b 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -115,4 +115,4 @@ export enum DescriptionConfiguration { None = "None", } -export const globalStateLeetcodeIsUserFresh: string = "leetcode.isUserFresh"; +export const globalStateLeetcodeHasInited: string = "leetcode.isUserFresh"; From e326d8ca337e115f6c0181dfb53b1b9c2092fef8 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Thu, 23 Jul 2020 10:31:34 +0800 Subject: [PATCH 3/3] change: global state name --- src/leetCodeExecutor.ts | 6 +++--- src/shared.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index 232648ca..5157b6c1 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -8,7 +8,7 @@ import * as path from "path"; import * as requireFromString from "require-from-string"; import { ExtensionContext } from "vscode"; import { ConfigurationChangeEvent, Disposable, MessageItem, window, workspace, WorkspaceConfiguration } from "vscode"; -import { Endpoint, globalStateLeetcodeHasInited, IProblem, supportedPlugins } from "./shared"; +import { Endpoint, IProblem, leetcodeHasInited, supportedPlugins } from "./shared"; import { executeCommand, executeCommandWithProgress } from "./utils/cpUtils"; import { DialogOptions, openUrl } from "./utils/uiUtils"; import * as wsl from "./utils/wslUtils"; @@ -37,7 +37,7 @@ class LeetCodeExecutor implements Disposable { } public async meetRequirements(context: ExtensionContext): Promise { - const hasInited: boolean | undefined = context.globalState.get(globalStateLeetcodeHasInited); + const hasInited: boolean | undefined = context.globalState.get(leetcodeHasInited); if (!hasInited) { await this.removeOldCache(); } @@ -72,7 +72,7 @@ class LeetCodeExecutor implements Disposable { } } // Set the global state HasInited true to skip delete old cache after init - context.globalState.update(globalStateLeetcodeHasInited, true); + context.globalState.update(leetcodeHasInited, true); return true; } diff --git a/src/shared.ts b/src/shared.ts index cd5b791b..e09943f8 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -115,4 +115,4 @@ export enum DescriptionConfiguration { None = "None", } -export const globalStateLeetcodeHasInited: string = "leetcode.isUserFresh"; +export const leetcodeHasInited: string = "leetcode.hasInited";