33import * as cp from "child_process" ;
44import * as vscode from "vscode" ;
55import { leetcodeChannel } from "../leetCodeChannel" ;
6+ import { LeetCodeStatus , leetCodeStatusBarItem } from "../leetCodeStatusBarItem" ;
67import { leetCodeBinaryPath } from "../shared" ;
78import { executeCommand } from "../utils/cpUtils" ;
89import { DialogOptions , DialogType , promptForOpenOutputChannel } from "../utils/uiUtils" ;
@@ -18,7 +19,7 @@ export async function getSignedInAccount(): Promise<string | undefined> {
1819 DialogOptions . no ,
1920 ) ;
2021 if ( choice === DialogOptions . yes ) {
21- // sign in
22+ await vscode . commands . executeCommand ( "leetcode.signin" ) ;
2223 }
2324 return undefined ;
2425 }
@@ -27,7 +28,7 @@ export async function getSignedInAccount(): Promise<string | undefined> {
2728export async function signIn ( ) : Promise < void > {
2829 // work around for interactive login
2930 try {
30- await new Promise ( async ( resolve : ( res : string ) => void , reject : ( e : Error ) => void ) : Promise < void > => {
31+ const userName : string = await new Promise ( async ( resolve : ( res : string ) => void , reject : ( e : Error ) => void ) : Promise < void > => {
3132 let result : string = "" ;
3233 const childProc : cp . ChildProcess = cp . spawn ( "node" , [ leetCodeBinaryPath , "user" , "-l" ] ) ;
3334 childProc . stdout . on ( "data" , ( data : string | Buffer ) => {
@@ -39,11 +40,12 @@ export async function signIn(): Promise<void> {
3940 childProc . stderr . on ( "data" , ( data : string | Buffer ) => leetcodeChannel . append ( data . toString ( ) ) ) ;
4041
4142 childProc . on ( "error" , reject ) ;
42- childProc . on ( "exit" , ( code : number ) => {
43- if ( code !== 0 || result . indexOf ( "ERROR" ) > - 1 ) {
44- reject ( new Error ( "Login failed" ) ) ;
43+ childProc . on ( "exit" , ( ) => {
44+ 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) ;
45+ if ( match && match [ 1 ] ) {
46+ resolve ( match [ 1 ] ) ;
4547 } else {
46- resolve ( result ) ;
48+ reject ( new Error ( "Login failed" ) ) ;
4749 }
4850 } ) ;
4951 const user : string | undefined = await vscode . window . showInputBox ( {
@@ -68,6 +70,7 @@ export async function signIn(): Promise<void> {
6870 childProc . stdin . end ( ) ;
6971 } ) ;
7072 vscode . window . showInformationMessage ( "Successfully signed in." ) ;
73+ leetCodeStatusBarItem . updateStatusBar ( LeetCodeStatus . SignedIn , userName ) ;
7174 } catch ( error ) {
7275 await promptForOpenOutputChannel ( "Failed to sign in. Please open the output channel for details" , DialogType . error ) ;
7376 }
@@ -77,6 +80,7 @@ export async function signOut(): Promise<void> {
7780 try {
7881 await executeCommand ( "node" , [ leetCodeBinaryPath , "user" , "-L" ] ) ;
7982 vscode . window . showInformationMessage ( "Successfully signed out." ) ;
83+ leetCodeStatusBarItem . updateStatusBar ( LeetCodeStatus . SignedOut ) ;
8084 } catch ( error ) {
8185 await promptForOpenOutputChannel ( "Failed to sign out. Please open the output channel for details" , DialogType . error ) ;
8286 }
0 commit comments