|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +import*ascpfrom"child_process"; |
| 4 | +import*asopnfrom"opn"; |
| 5 | +import*aspathfrom"path"; |
| 6 | +import*asvscodefrom"vscode"; |
| 7 | +import{executeCommand,executeCommandWithProgress}from"./utils/cpUtils"; |
| 8 | +import{DialogOptions}from"./utils/uiUtils"; |
| 9 | +import*aswslfrom"./utils/wslUtils"; |
| 10 | + |
| 11 | +exportinterfaceILeetCodeExecutor{ |
| 12 | +meetRequirements():Promise<boolean>; |
| 13 | +getLeetCodeBinaryPath():Promise<string>; |
| 14 | + |
| 15 | +/* section for user command */ |
| 16 | +getUserInfo():Promise<string>; |
| 17 | +signOut():Promise<string>; |
| 18 | +// TODO: implement login when leetcode-cli support login in batch mode. |
| 19 | +// signIn(): Promise<string>; |
| 20 | + |
| 21 | +/* section for problem command */ |
| 22 | +listProblems(showLocked:boolean):Promise<string>; |
| 23 | +showProblem(id:string,language:string,outdir:string):Promise<string>; |
| 24 | + |
| 25 | +/* section for session command */ |
| 26 | +listSessions():Promise<string>; |
| 27 | +enableSession(name:string):Promise<string>; |
| 28 | +createSession(name:string):Promise<string>; |
| 29 | + |
| 30 | +/* section for solution command */ |
| 31 | +submitSolution(filePath:string):Promise<string>; |
| 32 | +testSolution(filePath:string,testString?:string):Promise<string>; |
| 33 | +} |
| 34 | + |
| 35 | +classLeetCodeExecutorimplementsILeetCodeExecutor{ |
| 36 | +privateleetCodeBinaryPath:string; |
| 37 | +privateleetCodeBinaryPathInWsl:string; |
| 38 | + |
| 39 | +constructor(){ |
| 40 | +this.leetCodeBinaryPath=path.join(__dirname,"..","..","node_modules","leetcode-cli","bin","leetcode"); |
| 41 | +this.leetCodeBinaryPathInWsl=""; |
| 42 | +} |
| 43 | + |
| 44 | +publicasyncgetLeetCodeBinaryPath():Promise<string>{ |
| 45 | +if(wsl.useWsl()){ |
| 46 | +if(!this.leetCodeBinaryPathInWsl){ |
| 47 | +this.leetCodeBinaryPathInWsl=`${awaitwsl.toWslPath(this.leetCodeBinaryPath)}`; |
| 48 | +} |
| 49 | +return`"${this.leetCodeBinaryPathInWsl}"`; |
| 50 | +} |
| 51 | +return`"${this.leetCodeBinaryPath}"`; |
| 52 | +} |
| 53 | + |
| 54 | +publicasyncmeetRequirements():Promise<boolean>{ |
| 55 | +try{ |
| 56 | +awaitthis.executeCommandEx("node",["-v"]); |
| 57 | +returntrue; |
| 58 | +}catch(error){ |
| 59 | +constchoice:vscode.MessageItem|undefined=awaitvscode.window.showErrorMessage( |
| 60 | +"LeetCode extension needs Node.js installed in environment path", |
| 61 | +DialogOptions.open, |
| 62 | +); |
| 63 | +if(choice===DialogOptions.open){ |
| 64 | +opn("https://nodejs.org"); |
| 65 | +} |
| 66 | +returnfalse; |
| 67 | +} |
| 68 | +} |
| 69 | + |
| 70 | +publicasyncgetUserInfo():Promise<string>{ |
| 71 | +returnawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"user"]); |
| 72 | +} |
| 73 | + |
| 74 | +publicasyncsignOut():Promise<string>{ |
| 75 | +returnawaitawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"user","-L"]); |
| 76 | +} |
| 77 | + |
| 78 | +publicasynclistProblems(showLocked:boolean):Promise<string>{ |
| 79 | +returnawaitthis.executeCommandEx("node",showLocked ? |
| 80 | +[awaitthis.getLeetCodeBinaryPath(),"list"] : |
| 81 | +[awaitthis.getLeetCodeBinaryPath(),"list","-q","L"], |
| 82 | +); |
| 83 | +} |
| 84 | + |
| 85 | +publicasyncshowProblem(id:string,language:string,outdir:string):Promise<string>{ |
| 86 | +returnawaitthis.executeCommandWithProgressEx("Fetching problem data...","node",[awaitthis.getLeetCodeBinaryPath(),"show",id,"-gx","-l",language,"-o",`"${outdir}"`]); |
| 87 | +} |
| 88 | + |
| 89 | +publicasynclistSessions():Promise<string>{ |
| 90 | +returnawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"session"]); |
| 91 | +} |
| 92 | + |
| 93 | +publicasyncenableSession(name:string):Promise<string>{ |
| 94 | +returnawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"session","-e",name]); |
| 95 | +} |
| 96 | + |
| 97 | +publicasynccreateSession(name:string):Promise<string>{ |
| 98 | +returnawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"session","-c",name]); |
| 99 | +} |
| 100 | + |
| 101 | +publicasyncsubmitSolution(filePath:string):Promise<string>{ |
| 102 | +returnawaitthis.executeCommandWithProgressEx("Submitting to LeetCode...","node",[awaitthis.getLeetCodeBinaryPath(),"submit",`"${filePath}"`]); |
| 103 | +} |
| 104 | + |
| 105 | +publicasynctestSolution(filePath:string,testString?:string):Promise<string>{ |
| 106 | +if(testString){ |
| 107 | +returnawaitthis.executeCommandWithProgressEx("Submitting to LeetCode...","node",[awaitthis.getLeetCodeBinaryPath(),"test",`"${filePath}"`,"-t",`"${testString}"`]); |
| 108 | +} |
| 109 | +returnawaitthis.executeCommandWithProgressEx("Submitting to LeetCode...","node",[awaitthis.getLeetCodeBinaryPath(),"test",`"${filePath}"`]); |
| 110 | +} |
| 111 | + |
| 112 | +privateasyncexecuteCommandEx(command:string,args:string[],options:cp.SpawnOptions={shell:true}):Promise<string>{ |
| 113 | +if(wsl.useWsl()){ |
| 114 | +returnawaitexecuteCommand("wsl",[command].concat(args),options); |
| 115 | +} |
| 116 | +returnawaitexecuteCommand(command,args,options); |
| 117 | +} |
| 118 | + |
| 119 | +privateasyncexecuteCommandWithProgressEx(message:string,command:string,args:string[],options:cp.SpawnOptions={shell:true}):Promise<string>{ |
| 120 | +if(wsl.useWsl()){ |
| 121 | +returnawaitexecuteCommandWithProgress(message,"wsl",[command].concat(args),options); |
| 122 | +} |
| 123 | +returnawaitexecuteCommandWithProgress(message,command,args,options); |
| 124 | +} |
| 125 | +} |
| 126 | + |
| 127 | +exportconstleetCodeExecutor:ILeetCodeExecutor=newLeetCodeExecutor(); |