@@ -226,30 +226,29 @@ export class WakaTime {
226226 this . statusBarTeamOther . tooltip = tooltipText ;
227227 }
228228
229- public promptForApiKey ( hidden : boolean = true ) : void {
230- this . options . getApiKey ( ( defaultVal : string | null ) => {
231- if ( Utils . apiKeyInvalid ( defaultVal ?? undefined ) ) defaultVal = '' ;
232- let promptOptions = {
233- prompt : 'WakaTime Api Key' ,
234- placeHolder : 'Enter your api key from https://wakatime.com/api-key' ,
235- value : defaultVal ! ,
236- ignoreFocusOut : true ,
237- password : hidden ,
238- validateInput : Utils . apiKeyInvalid . bind ( this ) ,
239- } ;
240- vscode . window . showInputBox ( promptOptions ) . then ( ( val ) => {
241- if ( val != undefined ) {
242- let invalid = Utils . apiKeyInvalid ( val ) ;
243- if ( ! invalid ) {
244- this . options . setSetting ( 'settings' , 'api_key' , val , false ) ;
245- } else vscode . window . setStatusBarMessage ( invalid ) ;
246- } else vscode . window . setStatusBarMessage ( 'WakaTime api key not provided' ) ;
247- } ) ;
229+ public async promptForApiKey ( hidden : boolean = true ) : Promise < void > {
230+ let defaultVal = await this . options . getApiKey ( ) ;
231+ if ( Utils . apiKeyInvalid ( defaultVal ?? undefined ) ) defaultVal = '' ;
232+ let promptOptions = {
233+ prompt : 'WakaTime Api Key' ,
234+ placeHolder : 'Enter your api key from https://wakatime.com/api-key' ,
235+ value : defaultVal ! ,
236+ ignoreFocusOut : true ,
237+ password : hidden ,
238+ validateInput : Utils . apiKeyInvalid . bind ( this ) ,
239+ } ;
240+ vscode . window . showInputBox ( promptOptions ) . then ( ( val ) => {
241+ if ( val != undefined ) {
242+ let invalid = Utils . apiKeyInvalid ( val ) ;
243+ if ( ! invalid ) {
244+ this . options . setSetting ( 'settings' , 'api_key' , val , false ) ;
245+ } else vscode . window . setStatusBarMessage ( invalid ) ;
246+ } else vscode . window . setStatusBarMessage ( 'WakaTime api key not provided' ) ;
248247 } ) ;
249248 }
250249
251250 public async promptForApiUrl ( ) : Promise < void > {
252- const apiUrl = await this . options . getApiUrl ( ) ;
251+ const apiUrl = await this . options . getApiUrl ( true ) ;
253252 let promptOptions = {
254253 prompt : 'WakaTime Api Url (Defaults to https://api.wakatime.com/api/v1)' ,
255254 placeHolder : 'https://api.wakatime.com/api/v1' ,
@@ -379,7 +378,7 @@ export class WakaTime {
379378 }
380379
381380 public async openDashboardWebsite ( ) : Promise < void > {
382- const url = ( await this . options . getApiUrl ( ) ) . replace ( '/api/v1' , '' ) . replace ( 'api.' , '' ) ;
381+ const url = ( await this . options . getApiUrl ( true ) ) . replace ( '/api/v1' , '' ) . replace ( 'api.' , '' ) ;
383382 vscode . env . openExternal ( vscode . Uri . parse ( url ) ) ;
384383 }
385384
@@ -519,31 +518,30 @@ export class WakaTime {
519518 } , this . debounceMs ) ;
520519 }
521520
522- private sendHeartbeat (
521+ private async sendHeartbeat (
523522 doc : vscode . TextDocument ,
524523 time : number ,
525524 selection : vscode . Position ,
526525 isWrite : boolean ,
527526 isCompiling : boolean ,
528527 isDebugging : boolean ,
529- ) : void {
530- this . options . getApiKey ( ( apiKey ) => {
531- if ( apiKey ) {
532- this . _sendHeartbeat ( doc , time , selection , isWrite , isCompiling , isDebugging ) ;
533- } else {
534- this . promptForApiKey ( ) ;
535- }
536- } ) ;
528+ ) : Promise < void > {
529+ const apiKey = await this . options . getApiKey ( ) ;
530+ if ( apiKey ) {
531+ await this . _sendHeartbeat ( doc , time , selection , isWrite , isCompiling , isDebugging ) ;
532+ } else {
533+ await this . promptForApiKey ( ) ;
534+ }
537535 }
538536
539- private _sendHeartbeat (
537+ private async _sendHeartbeat (
540538 doc : vscode . TextDocument ,
541539 time : number ,
542540 selection : vscode . Position ,
543541 isWrite : boolean ,
544542 isCompiling : boolean ,
545543 isDebugging : boolean ,
546- ) : void {
544+ ) : Promise < void > {
547545 if ( ! this . dependencies . isCliInstalled ( ) ) return ;
548546
549547 let file = doc . fileName ;
@@ -580,7 +578,7 @@ export class WakaTime {
580578 const apiKey = this . options . getApiKeyFromEnv ( ) ;
581579 if ( ! Utils . apiKeyInvalid ( apiKey ) ) args . push ( '--key' , Utils . quote ( apiKey ) ) ;
582580
583- const apiUrl = this . options . getApiUrlFromEnv ( ) ;
581+ const apiUrl = await this . options . getApiUrl ( ) ;
584582 if ( apiUrl ) args . push ( '--api-url' , Utils . quote ( apiUrl ) ) ;
585583
586584 const project = this . getProjectName ( doc . uri ) ;
@@ -612,7 +610,7 @@ export class WakaTime {
612610 this . logger . error ( error . toString ( ) ) ;
613611 }
614612 } ) ;
615- proc . on ( 'close' , ( code , _signal ) => {
613+ proc . on ( 'close' , async ( code , _signal ) => {
616614 if ( code == 0 ) {
617615 if ( this . showStatusBar ) this . getCodingActivity ( ) ;
618616 } else if ( code == 102 || code == 112 ) {
@@ -642,7 +640,7 @@ export class WakaTime {
642640 let now : number = Date . now ( ) ;
643641 if ( this . lastApiKeyPrompted < now - 86400000 ) {
644642 // only prompt once per day
645- this . promptForApiKey ( false ) ;
643+ await this . promptForApiKey ( false ) ;
646644 this . lastApiKeyPrompted = now ;
647645 }
648646 } else {
@@ -656,21 +654,21 @@ export class WakaTime {
656654 } ) ;
657655 }
658656
659- private getCodingActivity ( ) {
657+ private async getCodingActivity ( ) {
660658 if ( ! this . showStatusBar ) return ;
661659
662660 const cutoff = Date . now ( ) - this . fetchTodayInterval ;
663661 if ( this . lastFetchToday > cutoff ) return ;
664662
665663 this . lastFetchToday = Date . now ( ) ;
666664
667- this . options . getApiKey ( ( apiKey ) => {
668- if ( ! apiKey ) return ;
669- this . _getCodingActivity ( ) ;
670- } ) ;
665+ const apiKey = await this . options . getApiKey ( ) ;
666+ if ( ! apiKey ) return ;
667+
668+ await this . _getCodingActivity ( ) ;
671669 }
672670
673- private _getCodingActivity ( ) {
671+ private async _getCodingActivity ( ) {
674672 if ( ! this . dependencies . isCliInstalled ( ) ) return ;
675673
676674 let user_agent =
@@ -682,7 +680,7 @@ export class WakaTime {
682680 const apiKey = this . options . getApiKeyFromEnv ( ) ;
683681 if ( ! Utils . apiKeyInvalid ( apiKey ) ) args . push ( '--key' , Utils . quote ( apiKey ) ) ;
684682
685- const apiUrl = this . options . getApiUrlFromEnv ( ) ;
683+ const apiUrl = await this . options . getApiUrl ( ) ;
686684 if ( apiUrl ) args . push ( '--api-url' , Utils . quote ( apiUrl ) ) ;
687685
688686 if ( Desktop . isWindows ( ) ) {
@@ -764,7 +762,7 @@ export class WakaTime {
764762 }
765763 }
766764
767- private updateTeamStatusBar ( doc ?: vscode . TextDocument ) {
765+ private async updateTeamStatusBar ( doc ?: vscode . TextDocument ) {
768766 if ( ! this . showStatusBarTeam ) return ;
769767 if ( ! this . hasTeamFeatures ) return ;
770768 if ( ! this . dependencies . isCliInstalled ( ) ) return ;
@@ -802,7 +800,7 @@ export class WakaTime {
802800 const apiKey = this . options . getApiKeyFromEnv ( ) ;
803801 if ( ! Utils . apiKeyInvalid ( apiKey ) ) args . push ( '--key' , Utils . quote ( apiKey ) ) ;
804802
805- const apiUrl = this . options . getApiUrlFromEnv ( ) ;
803+ const apiUrl = await this . options . getApiUrl ( ) ;
806804 if ( apiUrl ) args . push ( '--api-url' , Utils . quote ( apiUrl ) ) ;
807805
808806 const project = this . getProjectName ( doc . uri ) ;
0 commit comments