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..5157b6c1 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, IProblem, leetcodeHasInited, 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 hasInited: boolean | undefined = context.globalState.get(leetcodeHasInited); + if (!hasInited) { + 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}`); @@ -60,10 +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]); } } + // Set the global state HasInited true to skip delete old cache after init + context.globalState.update(leetcodeHasInited, true); return true; } @@ -76,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 { @@ -194,6 +203,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..e09943f8 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -114,3 +114,5 @@ export enum DescriptionConfiguration { Both = "Both", None = "None", } + +export const leetcodeHasInited: string = "leetcode.hasInited";