Skip to content

Commit 1dd3b78

Browse files
committed
feat: stream tasks output to output panel
1 parent 98f0ba2 commit 1dd3b78

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ This extension integrates your Taskfile into Visual Studio Code.
1515

1616
- Run last task command
1717
- Loading icon when a task is running in tree view
18-
- Stream output to output channel instead of waiting for command to finish
1918
- Switch between nested/flat task view
2019
- Refresh up-to-date status when a task's sources change
2120
- Status polling to update up-to-date status

src/services/taskfile.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import * as fs from 'fs';
77
class TaskfileService {
88
private static _instance: TaskfileService;
99
private static outputChannel: vscode.OutputChannel;
10+
private static readonly taskCommand = 'task';
1011

1112
private constructor() {
1213
TaskfileService.outputChannel = vscode.window.createOutputChannel('Task');
1314
}
1415

15-
public static get Instance() {
16+
public static get instance() {
1617
return this._instance ?? (this._instance = new this());
1718
}
1819

@@ -55,12 +56,28 @@ class TaskfileService {
5556

5657
public async runTask(taskName: string, dir?: string): Promise<void> {
5758
return await new Promise((resolve) => {
58-
let command = `task ${taskName}`;
59-
cp.exec(command, { cwd: dir }, (_, stdout: string, stderr: string) => {
60-
TaskfileService.outputChannel.append(stderr);
61-
TaskfileService.outputChannel.append(stdout);
62-
TaskfileService.outputChannel.append("-----\n");
63-
TaskfileService.outputChannel.show();
59+
// Spawn a child process
60+
let child = cp.spawn(TaskfileService.taskCommand, [taskName], { cwd: dir });
61+
62+
// Clear the output channel and show it
63+
TaskfileService.outputChannel.clear();
64+
TaskfileService.outputChannel.show();
65+
66+
// Listen for stderr
67+
child.stderr.setEncoding('utf8');
68+
child.stderr.on("data", data => {
69+
TaskfileService.outputChannel.append(data.toString());
70+
});
71+
72+
// Listen for stdout
73+
child.stdout.setEncoding('utf8');
74+
child.stdout.on("data", data => {
75+
TaskfileService.outputChannel.append(data.toString());
76+
});
77+
78+
// When the task finishes, print the exit code and resolve the promise
79+
child.on('close', code => {
80+
TaskfileService.outputChannel.append(`task: completed with code ${code}\n`);
6481
return resolve();
6582
});
6683
});
@@ -93,4 +110,4 @@ class TaskfileService {
93110
}
94111
}
95112

96-
export const taskfile = TaskfileService.Instance;
113+
export const taskfile = TaskfileService.instance;

0 commit comments

Comments
 (0)