@@ -34,16 +34,20 @@ class LeetCodeManager extends EventEmitter {
3434 }
3535 }
3636
37- public async signIn ( ) : Promise < void > {
37+ public async signIn ( isByCookie : boolean = false ) : Promise < void > {
38+ const loginArg : string = "-l" ;
39+ const cookieArg : string = "-c" ;
40+ const commandArg : string = isByCookie ? cookieArg : loginArg ;
41+ const inMessage : string = isByCookie ? "sign in by cookie" : "sign in" ;
3842 try {
3943 const userName : string | undefined = await new Promise ( async ( resolve : ( res : string | undefined ) => void , reject : ( e : Error ) => void ) : Promise < void > => {
4044 let result : string = "" ;
4145
4246 const leetCodeBinaryPath : string = await leetCodeExecutor . getLeetCodeBinaryPath ( ) ;
4347
4448 const childProc : cp . ChildProcess = wsl . useWsl ( )
45- ? cp . spawn ( "wsl" , [ leetCodeExecutor . node , leetCodeBinaryPath , "user" , "-l" ] , { shell : true } )
46- : cp . spawn ( leetCodeExecutor . node , [ leetCodeBinaryPath , "user" , "-l" ] , {
49+ ? cp . spawn ( "wsl" , [ leetCodeExecutor . node , leetCodeBinaryPath , "user" , commandArg ] , { shell : true } )
50+ : cp . spawn ( leetCodeExecutor . node , [ leetCodeBinaryPath , "user" , commandArg ] , {
4751 shell : true ,
4852 env : createEnvOption ( ) ,
4953 } ) ;
@@ -67,9 +71,9 @@ class LeetCodeManager extends EventEmitter {
6771 }
6872 childProc . stdin . write ( `${ name } \n` ) ;
6973 const pwd : string | undefined = await vscode . window . showInputBox ( {
70- prompt : "Enter password." ,
74+ prompt : isByCookie ? "Enter cookie" : "Enter password." ,
7175 password : true ,
72- validateInput : ( s : string ) : string | undefined => s ? undefined : "Password must not be empty" ,
76+ validateInput : ( s : string ) : string | undefined => s ? undefined : isByCookie ? "Cookie must not be empty" : "Password must not be empty" ,
7377 } ) ;
7478 if ( ! pwd ) {
7579 childProc . kill ( ) ;
@@ -78,22 +82,22 @@ class LeetCodeManager extends EventEmitter {
7882 childProc . stdin . write ( `${ pwd } \n` ) ;
7983 childProc . stdin . end ( ) ;
8084 childProc . on ( "close" , ( ) => {
81- const match : RegExpMatchArray | null = result . match ( / (?: .* ) S u c c e s s f u l l y l o g i n a s ( .* ) / i) ;
82- if ( match && match [ 1 ] ) {
83- resolve ( match [ 1 ] ) ;
85+ const match : RegExpMatchArray | null = result . match ( / (?: .* ) S u c c e s s f u l l y ( l o g i n | c o o k i e l o g i n ) a s ( .* ) / i) ;
86+ if ( match && match [ 2 ] ) {
87+ resolve ( match [ 2 ] ) ;
8488 } else {
85- reject ( new Error ( " Failed to sign in." ) ) ;
89+ reject ( new Error ( ` Failed to ${ inMessage } .` ) ) ;
8690 }
8791 } ) ;
8892 } ) ;
8993 if ( userName ) {
90- vscode . window . showInformationMessage ( " Successfully signed in." ) ;
94+ vscode . window . showInformationMessage ( ` Successfully ${ inMessage } .` ) ;
9195 this . currentUser = userName ;
9296 this . userStatus = UserStatus . SignedIn ;
9397 this . emit ( "statusChanged" ) ;
9498 }
9599 } catch ( error ) {
96- promptForOpenOutputChannel ( " Failed to sign in . Please open the output channel for details" , DialogType . error ) ;
100+ promptForOpenOutputChannel ( ` Failed to ${ inMessage } . Please open the output channel for details` , DialogType . error ) ;
97101 }
98102
99103 }
0 commit comments