@@ -7,12 +7,13 @@ import * as fs from 'fs';
77class 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