@@ -56,7 +56,6 @@ export class PersistentShell {
56
56
) : Promise < { output : string ; exitCode : string } > {
57
57
this . stdoutBuffer = ''
58
58
this . stderrBuffer = ''
59
- let exitCode = 0
60
59
61
60
return new Promise ( ( resolve , reject ) => {
62
61
abortSignal ?. throwIfAborted ( )
@@ -68,11 +67,11 @@ export class PersistentShell {
68
67
69
68
// Add exit code capture
70
69
const getExitCodeCommand = process . platform === 'win32' ? 'echo %errorlevel%' : 'echo $?'
71
- this . shell . stdin ?. write ( `${ command } ` + '\n' )
72
- this . shell . stdin ?. write ( `${ getExitCodeCommand } \n` )
70
+ this . shell . stdin ?. write ( `${ command } ` + os . EOL )
71
+ this . shell . stdin ?. write ( `${ getExitCodeCommand } ` + os . EOL )
73
72
74
73
const endMarker = `__END_OF_COMMAND_${ Date . now ( ) } __`
75
- this . shell . stdin ?. write ( `echo "${ endMarker } "\n` )
74
+ this . shell . stdin ?. write ( `echo "${ endMarker } "` + os . EOL )
76
75
77
76
const timeout = 30000
78
77
@@ -85,37 +84,38 @@ export class PersistentShell {
85
84
const abortListener = ( ) => {
86
85
clearTimeout ( timeoutId )
87
86
this . dispose ( )
88
- reject ( new Error ( 'Command execution aborted' ) )
89
87
abortSignal ?. removeEventListener ( 'abort' , abortListener )
88
+ reject ( new Error ( 'Command execution aborted' ) )
90
89
}
91
90
abortSignal ?. addEventListener ( 'abort' , abortListener )
92
91
93
92
const checkBuffer = ( ) => {
94
93
if ( this . stdoutBuffer . includes ( endMarker ) ) {
95
- //const sliceStart = process.platform === 'win32' ? 1 : 0
94
+ const sliceStart = process . platform === 'win32' ? 1 : 0
95
+ const sliceEnd = process . platform === 'win32' ? - 4 : - 2
96
+
96
97
clearTimeout ( timeoutId )
97
98
const outputParts = this . stdoutBuffer
98
99
. split ( `echo "${ endMarker } "` ) [ 0 ]
99
100
. trim ( )
100
101
. split ( '\n' )
101
-
102
102
// Extract exit code from the last line
103
- exitCode = Number . parseInt ( outputParts [ outputParts . length - 2 ] , 10 )
104
-
103
+ const exitCode = Number . parseInt (
104
+ outputParts [ outputParts . length - ( 2 + sliceStart ) ] ,
105
+ 10
106
+ )
105
107
// Remove exit code line from output
106
108
const output = outputParts
107
- . slice ( 0 , - 2 )
109
+ // .slice(0, -2)
108
110
. filter ( chunk => {
109
111
if ( chunk . includes ( '(c) Microsoft Corporation.' ) ) return false
110
112
if ( chunk . includes ( 'Microsoft Windows' ) ) return false
111
113
if ( chunk . match ( / ^ [ A - Z a - z ] : \\ .* > .* $ / ) ) return false
112
114
return true
113
115
} )
114
- . join ( '\n' )
115
-
116
+ . slice ( sliceStart , sliceEnd )
117
+ . join ( os . EOL )
116
118
abortSignal ?. removeEventListener ( 'abort' , abortListener )
117
-
118
- // Return stderr if command failed, stdout otherwise
119
119
resolve ( {
120
120
output : exitCode !== 0 ? this . stderrBuffer : output ,
121
121
exitCode : `${ exitCode } ` ,
0 commit comments