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 , globalStateLeetcodeIsUserFresh , IProblem , 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 isUserFresh : boolean | undefined = context . globalState . get ( globalStateLeetcodeIsUserFresh ) ;
41
+ if ( isUserFresh !== false ) {
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 } ` ) ;
@@ -61,9 +67,11 @@ class LeetCodeExecutor implements Disposable {
61
67
try { // Check plugin
62
68
await this . executeCommandEx ( this . nodeExecutable , [ await this . getLeetCodeBinaryPath ( ) , "plugin" , "-e" , plugin ] ) ;
63
69
} catch ( 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
+ context . globalState . update ( globalStateLeetcodeIsUserFresh , false ) ;
67
75
return true ;
68
76
}
69
77
@@ -194,6 +202,14 @@ class LeetCodeExecutor implements Disposable {
194
202
}
195
203
return await executeCommandWithProgress ( message , command , args , options ) ;
196
204
}
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
+
197
213
}
198
214
199
215
export const leetCodeExecutor : LeetCodeExecutor = new LeetCodeExecutor ( ) ;
0 commit comments