11"use strict" ;
22
3+ import * as cp from "child_process" ;
34import * as vscode from "vscode" ;
5+ import { leetcodeChannel } from "../leetCodeChannel" ;
46import { leetCodeBinaryPath } from "../shared" ;
57import { executeCommand } from "../utils/cpUtils" ;
68import { DialogOptions , DialogType , promptForOpenOutputChannel } from "../utils/uiUtils" ;
@@ -22,16 +24,60 @@ export async function getSignedInAccount(): Promise<string | undefined> {
2224 }
2325}
2426
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+ }
2874}
2975
3076export async function signOut ( ) : Promise < void > {
3177 try {
3278 await executeCommand ( "node" , [ leetCodeBinaryPath , "user" , "-L" ] ) ;
3379 vscode . window . showInformationMessage ( "Successfully signed out." ) ;
3480 } 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 ) ;
3682 }
3783}
0 commit comments