33
44import * as cp from "child_process" ;
55import * as fse from "fs-extra" ;
6+ import * as os from "os" ;
67import * as path from "path" ;
78import * as requireFromString from "require-from-string" ;
9+ import { ExtensionContext } from "vscode" ;
810import { ConfigurationChangeEvent , Disposable , MessageItem , window , workspace , WorkspaceConfiguration } from "vscode" ;
9- import { Endpoint , IProblem , supportedPlugins } from "./shared" ;
11+ import { Endpoint , IProblem , leetcodeHasInited , supportedPlugins } from "./shared" ;
1012import { executeCommand , executeCommandWithProgress } from "./utils/cpUtils" ;
1113import { DialogOptions , openUrl } from "./utils/uiUtils" ;
1214import * 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 hasInited : boolean | undefined = context . globalState . get ( leetcodeHasInited ) ;
41+ if ( ! hasInited ) {
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 } ` ) ;
@@ -60,10 +66,13 @@ class LeetCodeExecutor implements Disposable {
6066 for ( const plugin of supportedPlugins ) {
6167 try { // Check plugin
6268 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 ( ) ;
6471 await this . executeCommandEx ( this . nodeExecutable , [ await this . getLeetCodeBinaryPath ( ) , "plugin" , "-i" , plugin ] ) ;
6572 }
6673 }
74+ // Set the global state HasInited true to skip delete old cache after init
75+ context . globalState . update ( leetcodeHasInited , true ) ;
6776 return true ;
6877 }
6978
@@ -76,7 +85,7 @@ class LeetCodeExecutor implements Disposable {
7685 }
7786
7887 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" ] ) ;
8089 }
8190
8291 public async listProblems ( showLocked : boolean ) : Promise < string > {
@@ -194,6 +203,14 @@ class LeetCodeExecutor implements Disposable {
194203 }
195204 return await executeCommandWithProgress ( message , command , args , options ) ;
196205 }
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+
197214}
198215
199216export const leetCodeExecutor : LeetCodeExecutor = new LeetCodeExecutor ( ) ;
0 commit comments