22import * as child_process from 'child_process' ;
33import * as fs from 'fs' ;
44import * as path from 'path' ;
5+ import * as os from 'os' ;
56import * as vscode from 'vscode' ;
67
78import {
@@ -671,6 +672,22 @@ export class WakaTime {
671672
672673 if ( doc . isUntitled ) heartbeat . is_unsaved_entity = true ;
673674
675+ if ( Utils . isRemoteUri ( doc . uri ) ) {
676+ try {
677+ const tmpFile = path . join (
678+ os . tmpdir ( ) ,
679+ `wakatime-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } ` ,
680+ ) ;
681+ await fs . promises . writeFile ( tmpFile , doc . getText ( ) , {
682+ encoding : doc . encoding as BufferEncoding ,
683+ } ) ;
684+ heartbeat . local_file = tmpFile ;
685+ heartbeat . entity = doc . fileName ;
686+ } catch ( e ) {
687+ this . logger . debugException ( e ) ;
688+ }
689+ }
690+
674691 this . logger . debug ( `Appending heartbeat to local buffer: ${ JSON . stringify ( heartbeat , null , 2 ) } ` ) ;
675692 this . heartbeats . push ( heartbeat ) ;
676693
@@ -749,6 +766,13 @@ export class WakaTime {
749766
750767 if ( heartbeat . is_unsaved_entity ) args . push ( '--is-unsaved-entity' ) ;
751768
769+ const cleanup : string [ ] = [ ] ;
770+ if ( heartbeat . local_file ) {
771+ args . push ( '--local-file' ) ;
772+ args . push ( Utils . quote ( heartbeat . local_file ) ) ;
773+ cleanup . push ( heartbeat . local_file ) ;
774+ }
775+
752776 const extraHeartbeats = this . getExtraHeartbeats ( ) ;
753777 if ( extraHeartbeats . length > 0 ) args . push ( '--extra-heartbeats' ) ;
754778
@@ -768,6 +792,7 @@ export class WakaTime {
768792 proc . stdin . write ( JSON . stringify ( extraHeartbeats ) ) ;
769793 proc . stdin . write ( '\n' ) ;
770794 proc . stdin . end ( ) ;
795+ cleanup . push ( ...( extraHeartbeats . map ( ( h ) => h . local_file ) . filter ( Boolean ) as string [ ] ) ) ;
771796 } else if ( extraHeartbeats . length > 0 ) {
772797 this . logger . error ( 'Unable to set stdio[0] to pipe' ) ;
773798 this . heartbeats . push ( ...extraHeartbeats ) ;
@@ -814,6 +839,8 @@ export class WakaTime {
814839 }
815840 this . logger . error ( error_msg ) ;
816841 }
842+
843+ cleanup . map ( ( tmpfile ) => fs . unlinkSync ( tmpfile ) ) ;
817844 } ) ;
818845 }
819846
0 commit comments