1
1
"use strict" ;
2
2
3
+ import * as cp from "child_process" ;
3
4
import * as vscode from "vscode" ;
5
+ import { leetcodeChannel } from "../leetCodeChannel" ;
4
6
import { leetCodeBinaryPath } from "../shared" ;
5
7
import { executeCommand } from "../utils/cpUtils" ;
6
8
import { DialogOptions , DialogType , promptForOpenOutputChannel } from "../utils/uiUtils" ;
@@ -22,16 +24,60 @@ export async function getSignedInAccount(): Promise<string | undefined> {
22
24
}
23
25
}
24
26
25
- export function signIn ( terminal : vscode . Terminal ) : void {
26
- terminal . show ( ) ;
27
- terminal . sendText ( `node ${ leetCodeBinaryPath } user -l` ) ;
27
+ export async function signIn ( ) : Promise < void > {
28
+ // work around for interactive login
29
+ try {
30
+ await new Promise ( async ( resolve : ( res : string ) => void , reject : ( e : Error ) => void ) : Promise < void > => {
31
+ let result : string = "" ;
32
+ const childProc : cp . ChildProcess = cp . spawn ( "node" , [ leetCodeBinaryPath , "user" , "-l" ] ) ;
33
+ childProc . stdout . on ( "data" , ( data : string | Buffer ) => {
34
+ data = data . toString ( ) ;
35
+ result = result . concat ( data ) ;
36
+ leetcodeChannel . append ( data ) ;
37
+ } ) ;
38
+
39
+ childProc . stderr . on ( "data" , ( data : string | Buffer ) => leetcodeChannel . append ( data . toString ( ) ) ) ;
40
+
41
+ childProc . on ( "error" , reject ) ;
42
+ childProc . on ( "exit" , ( code : number ) => {
43
+ if ( code !== 0 || result . indexOf ( "ERROR" ) > - 1 ) {
44
+ reject ( new Error ( "Login failed" ) ) ;
45
+ } else {
46
+ resolve ( result ) ;
47
+ }
48
+ } ) ;
49
+ const user : string | undefined = await vscode . window . showInputBox ( {
50
+ prompt : "Enter user name." ,
51
+ validateInput : ( s : string ) => s ? undefined : "User name must not be empty" ,
52
+ } ) ;
53
+ if ( ! user ) {
54
+ childProc . kill ( ) ;
55
+ reject ( new Error ( "Login Cancelled" ) ) ;
56
+ }
57
+ childProc . stdin . write ( `${ user } \n` ) ;
58
+ const pwd : string | undefined = await vscode . window . showInputBox ( {
59
+ prompt : "Enter user name." ,
60
+ password : true ,
61
+ validateInput : ( s : string ) => s ? undefined : "Password must not be empty" ,
62
+ } ) ;
63
+ if ( ! pwd ) {
64
+ childProc . kill ( ) ;
65
+ reject ( new Error ( "Login Cancelled" ) ) ;
66
+ }
67
+ childProc . stdin . write ( `${ pwd } \n` ) ;
68
+ childProc . stdin . end ( ) ;
69
+ } ) ;
70
+ vscode . window . showInformationMessage ( "Successfully signed in." ) ;
71
+ } catch ( error ) {
72
+ await promptForOpenOutputChannel ( "Failed to sign in. Please open the output channel for details" , DialogType . error ) ;
73
+ }
28
74
}
29
75
30
76
export async function signOut ( ) : Promise < void > {
31
77
try {
32
78
await executeCommand ( "node" , [ leetCodeBinaryPath , "user" , "-L" ] ) ;
33
79
vscode . window . showInformationMessage ( "Successfully signed out." ) ;
34
80
} catch ( error ) {
35
- await promptForOpenOutputChannel ( "Failed to sign out. Would you like to open output channel for detais? " , DialogType . error ) ;
81
+ await promptForOpenOutputChannel ( "Failed to sign out. Please open the output channel for details " , DialogType . error ) ;
36
82
}
37
83
}
0 commit comments