Skip to content

Commit 7e35b97

Browse files
committed
Copy remote file contents to local tmp file for better ssh and container support
1 parent 8aa9dd2 commit 7e35b97

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const SEND_BUFFER_SECONDS = 30;
2222
export interface Heartbeat {
2323
time: number;
2424
entity: string;
25+
local_file?: string;
2526
is_write: boolean;
2627
lineno: number;
2728
cursorpos: number;

src/wakatime.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as child_process from 'child_process';
33
import * as fs from 'fs';
44
import * as path from 'path';
5+
import * as os from 'os';
56
import * as vscode from 'vscode';
67

78
import {
@@ -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

Comments
 (0)