@@ -4,19 +4,8 @@ import path from "path";
44import { createRequire } from "module" ;
55
66export const NULL_DEVICE = '/dev/null' ;
7- const require = createRequire ( import . meta. url ) ;
8-
9- type CommandResult = Promise < { code : number , stdout : string } > ;
107
11- function devcontainerBinaryPath ( ) : string {
12- try {
13- const pkgPath = require . resolve ( '@devcontainers/cli/package.json' ) ;
14- const pkg = require ( pkgPath ) ;
15- return path . join ( path . dirname ( pkgPath ) , pkg . bin . devcontainer ) ;
16- } catch ( error ) {
17- throw new Error ( 'Failed to locate devcontainer CLI: ' + ( error as Error ) . message ) ;
18- }
19- }
8+ type CommandResult = Promise < string > ;
209
2110interface DevcontainerOptions {
2211 stdioFilePath ?: string ;
@@ -35,6 +24,18 @@ interface DevContainerExecOptions extends DevcontainerOptions {
3524 command : string [ ] ;
3625}
3726
27+ const require = createRequire ( import . meta. url ) ;
28+
29+ function devcontainerBinaryPath ( ) : string {
30+ try {
31+ const pkgPath = require . resolve ( '@devcontainers/cli/package.json' ) ;
32+ const pkg = require ( pkgPath ) ;
33+ return path . join ( path . dirname ( pkgPath ) , pkg . bin . devcontainer ) ;
34+ } catch ( error ) {
35+ throw new Error ( 'Failed to locate devcontainer CLI: ' + ( error as Error ) . message ) ;
36+ }
37+ }
38+
3839function createOutputStream ( stdioFilePath : string = NULL_DEVICE ) : fs . WriteStream {
3940 try {
4041 return fs . createWriteStream ( stdioFilePath , { flags : 'w' } )
@@ -50,8 +51,8 @@ async function runCommand(args: string[], stdoutStream: fs.WriteStream): Command
5051 stdio : [ 'ignore' , 'pipe' , 'pipe' ] ,
5152 } as SpawnOptions ) ;
5253
53- let stdoutData = '' ;
54- let stderrData = '' ;
54+ const stdoutData : string [ ] = [ ] ;
55+ const stderrData : string [ ] = [ ] ;
5556
5657 child . on ( 'error' , ( error ) => {
5758 cleanup ( error ) ;
@@ -60,14 +61,13 @@ async function runCommand(args: string[], stdoutStream: fs.WriteStream): Command
6061
6162 // Pipe stdout to the stream as before, but also collect it
6263 child . stdout ?. on ( 'data' , ( data ) => {
63- console . log ( data . toString ( ) )
64- stdoutData += JSON . stringify ( data . toString ( ) , null , 2 ) ;
64+ stdoutData . push ( data . toString ( ) )
6565 stdoutStream . write ( data ) ;
6666 } ) ;
6767
6868 // Collect stderr data instead of piping to process.stderr
6969 child . stderr ?. on ( 'data' , ( data ) => {
70- stderrData += data . toString ( ) ;
70+ stderrData . push ( data . toString ( ) . trim ( ) ) ;
7171 } ) ;
7272
7373 const cleanup = ( error ?: Error ) => {
@@ -84,17 +84,14 @@ async function runCommand(args: string[], stdoutStream: fs.WriteStream): Command
8484 child . on ( 'close' , ( code , signal ) => {
8585 cleanup ( ) ;
8686 if ( code === 0 ) {
87- resolve ( {
88- code : code ,
89- stdout : stdoutData
90- } ) ;
87+ resolve ( `success with code ${ code } \n-------\n${ stdoutData . join ( '\n\n' ) } ` ) ;
9188 } else {
9289 const reason = signal
9390 ? `terminated by signal ${ signal } `
9491 : `exited with code ${ code } ` ;
9592
9693 // Combine the error message with the collected stderr output
97- const errorMessage = `Command failed: devcontainer ${ args . join ( ' ' ) } (${ reason } )\n${ stderrData . trim ( ) } ` ;
94+ const errorMessage = `Command failed: devcontainer ${ args . join ( ' ' ) } (${ reason } )\n-------\n ${ stderrData . join ( '\n\n' ) } ` ;
9895 reject ( new Error ( errorMessage ) ) ;
9996 }
10097 } ) ;
@@ -119,4 +116,4 @@ export async function devExec(options: DevContainerExecOptions): CommandResult {
119116 options . workspaceFolder ,
120117 ...options . command
121118 ] , stream ) ;
122- }
119+ }
0 commit comments