@@ -14,8 +14,7 @@ import * as wsl from "./utils/wslUtils";
14
14
class LeetCodeManager extends EventEmitter {
15
15
private currentUser : string | undefined ;
16
16
private userStatus : UserStatus ;
17
- private readonly successRegex : RegExp =
18
- / (?: .* ) S u c c e s s f u l l y .* l o g i n a s ( .* ) / i;
17
+ private readonly successRegex : RegExp = / (?: .* ) S u c c e s s f u l l y .* l o g i n a s ( .* ) / i;
19
18
private readonly failRegex : RegExp = / .* \[ E R R O R \] .* / i;
20
19
21
20
constructor ( ) {
@@ -62,118 +61,78 @@ class LeetCodeManager extends EventEmitter {
62
61
value : "Cookie" ,
63
62
}
64
63
) ;
65
- const choice : IQuickItemEx < string > | undefined =
66
- await vscode . window . showQuickPick ( picks ) ;
64
+ const choice : IQuickItemEx < string > | undefined = await vscode . window . showQuickPick ( picks ) ;
67
65
if ( ! choice ) {
68
66
return ;
69
67
}
70
68
const loginMethod : string = choice . value ;
71
- const commandArg : string | undefined =
72
- loginArgsMapping . get ( loginMethod ) ;
69
+ const commandArg : string | undefined = loginArgsMapping . get ( loginMethod ) ;
73
70
if ( ! commandArg ) {
74
- throw new Error (
75
- `The login method "${ loginMethod } " is not supported.`
76
- ) ;
71
+ throw new Error ( `The login method "${ loginMethod } " is not supported.` ) ;
77
72
}
78
73
const isByCookie : boolean = loginMethod === "Cookie" ;
79
74
const inMessage : string = isByCookie ? "sign in by cookie" : "sign in" ;
80
75
try {
81
76
const userName : string | undefined = await new Promise (
82
- async (
83
- resolve : ( res : string | undefined ) => void ,
84
- reject : ( e : Error ) => void
85
- ) : Promise < void > => {
86
- const leetCodeBinaryPath : string =
87
- await leetCodeExecutor . getLeetCodeBinaryPath ( ) ;
77
+ async ( resolve : ( res : string | undefined ) => void , reject : ( e : Error ) => void ) : Promise < void > => {
78
+ const leetCodeBinaryPath : string = await leetCodeExecutor . getLeetCodeBinaryPath ( ) ;
88
79
89
80
const childProc : cp . ChildProcess = wsl . useWsl ( )
90
- ? cp . spawn (
91
- "wsl" ,
92
- [
93
- leetCodeExecutor . node ,
94
- leetCodeBinaryPath ,
95
- "user" ,
96
- commandArg ,
97
- ] ,
98
- { shell : true }
99
- )
100
- : cp . spawn (
101
- leetCodeExecutor . node ,
102
- [ leetCodeBinaryPath , "user" , commandArg ] ,
103
- {
104
- shell : true ,
105
- env : createEnvOption ( ) ,
106
- }
107
- ) ;
81
+ ? cp . spawn ( "wsl" , [ leetCodeExecutor . node , leetCodeBinaryPath , "user" , commandArg ] , {
82
+ shell : true ,
83
+ } )
84
+ : cp . spawn ( leetCodeExecutor . node , [ leetCodeBinaryPath , "user" , commandArg ] , {
85
+ shell : true ,
86
+ env : createEnvOption ( ) ,
87
+ } ) ;
108
88
109
- childProc . stdout ?. on (
110
- "data" ,
111
- async ( data : string | Buffer ) => {
112
- data = data . toString ( ) ;
113
- leetCodeChannel . append ( data ) ;
114
- if ( data . includes ( "twoFactorCode" ) ) {
115
- const twoFactor : string | undefined =
116
- await vscode . window . showInputBox ( {
117
- prompt : "Enter two-factor code." ,
118
- ignoreFocusOut : true ,
119
- validateInput : (
120
- s : string
121
- ) : string | undefined =>
122
- s && s . trim ( )
123
- ? undefined
124
- : "The input must not be empty" ,
125
- } ) ;
126
- if ( ! twoFactor ) {
127
- childProc . kill ( ) ;
128
- return resolve ( undefined ) ;
129
- }
130
- childProc . stdin ?. write ( `${ twoFactor } \n` ) ;
131
- }
132
- const successMatch : RegExpMatchArray | null =
133
- data . match ( this . successRegex ) ;
134
- if ( successMatch && successMatch [ 1 ] ) {
135
- childProc . stdin ?. end ( ) ;
136
- return resolve ( successMatch [ 1 ] ) ;
137
- } else if ( data . match ( this . failRegex ) ) {
138
- childProc . stdin ?. end ( ) ;
139
- return reject ( new Error ( "Faile to login" ) ) ;
89
+ childProc . stdout ?. on ( "data" , async ( data : string | Buffer ) => {
90
+ data = data . toString ( ) ;
91
+ leetCodeChannel . append ( data ) ;
92
+ if ( data . includes ( "twoFactorCode" ) ) {
93
+ const twoFactor : string | undefined = await vscode . window . showInputBox ( {
94
+ prompt : "Enter two-factor code." ,
95
+ ignoreFocusOut : true ,
96
+ validateInput : ( s : string ) : string | undefined =>
97
+ s && s . trim ( ) ? undefined : "The input must not be empty" ,
98
+ } ) ;
99
+ if ( ! twoFactor ) {
100
+ childProc . kill ( ) ;
101
+ return resolve ( undefined ) ;
140
102
}
103
+ childProc . stdin ?. write ( `${ twoFactor } \n` ) ;
104
+ }
105
+ const successMatch : RegExpMatchArray | null = data . match ( this . successRegex ) ;
106
+ if ( successMatch && successMatch [ 1 ] ) {
107
+ childProc . stdin ?. end ( ) ;
108
+ return resolve ( successMatch [ 1 ] ) ;
109
+ } else if ( data . match ( this . failRegex ) ) {
110
+ childProc . stdin ?. end ( ) ;
111
+ return reject ( new Error ( "Faile to login" ) ) ;
141
112
}
142
- ) ;
113
+ } ) ;
143
114
144
- childProc . stderr ?. on ( "data" , ( data : string | Buffer ) =>
145
- leetCodeChannel . append ( data . toString ( ) )
146
- ) ;
115
+ childProc . stderr ?. on ( "data" , ( data : string | Buffer ) => leetCodeChannel . append ( data . toString ( ) ) ) ;
147
116
148
117
childProc . on ( "error" , reject ) ;
149
- const name : string | undefined =
150
- await vscode . window . showInputBox ( {
151
- prompt : "Enter username or E-mail." ,
152
- ignoreFocusOut : true ,
153
- validateInput : ( s : string ) : string | undefined =>
154
- s && s . trim ( )
155
- ? undefined
156
- : "The input must not be empty" ,
157
- } ) ;
118
+ const name : string | undefined = await vscode . window . showInputBox ( {
119
+ prompt : "Enter username or E-mail." ,
120
+ ignoreFocusOut : true ,
121
+ validateInput : ( s : string ) : string | undefined =>
122
+ s && s . trim ( ) ? undefined : "The input must not be empty" ,
123
+ } ) ;
158
124
if ( ! name ) {
159
125
childProc . kill ( ) ;
160
126
return resolve ( undefined ) ;
161
127
}
162
128
childProc . stdin ?. write ( `${ name } \n` ) ;
163
- const pwd : string | undefined =
164
- await vscode . window . showInputBox ( {
165
- prompt : isByCookie
166
- ? "Enter cookie"
167
- : "Enter password." ,
168
- password : true ,
169
- ignoreFocusOut : true ,
170
- validateInput : ( s : string ) : string | undefined =>
171
- s
172
- ? undefined
173
- : isByCookie
174
- ? "Cookie must not be empty"
175
- : "Password must not be empty" ,
176
- } ) ;
129
+ const pwd : string | undefined = await vscode . window . showInputBox ( {
130
+ prompt : isByCookie ? "Enter cookie" : "Enter password." ,
131
+ password : true ,
132
+ ignoreFocusOut : true ,
133
+ validateInput : ( s : string ) : string | undefined =>
134
+ s ? undefined : isByCookie ? "Cookie must not be empty" : "Password must not be empty" ,
135
+ } ) ;
177
136
if ( ! pwd ) {
178
137
childProc . kill ( ) ;
179
138
return resolve ( undefined ) ;
@@ -182,9 +141,7 @@ class LeetCodeManager extends EventEmitter {
182
141
}
183
142
) ;
184
143
if ( userName ) {
185
- vscode . window . showInformationMessage (
186
- `Successfully ${ inMessage } .`
187
- ) ;
144
+ vscode . window . showInformationMessage ( `Successfully ${ inMessage } .` ) ;
188
145
this . currentUser = userName ;
189
146
this . userStatus = UserStatus . SignedIn ;
190
147
this . emit ( "statusChanged" ) ;
0 commit comments