1717
1818const yargs = require ( 'yargs' ) ;
1919const path = require ( 'path' ) ;
20- const { spawn } = require ( 'child-process-promise ' ) ;
21- const { writeFileSync } = require ( 'fs' ) ;
20+ const { spawn } = require ( 'node:child_process ' ) ;
21+ const fs = require ( 'node: fs' ) ;
2222
2323const LOGDIR = process . env . CI ? process . env . HOME : '/tmp' ;
2424// Maps the packages where we should not run `test:all` and instead isolate the cross-browser tests.
@@ -30,16 +30,17 @@ const crossBrowserPackages = {
3030 'packages/firestore-compat' : 'test:browser'
3131} ;
3232
33- function writeLogs ( status , name , logText ) {
34- const safeName = name . replace ( / @ / g, 'at_' ) . replace ( / \/ / g, '_' ) ;
35- writeFileSync ( path . join ( LOGDIR , `${ safeName } -ci-log.txt` ) , logText , {
36- encoding : 'utf8'
33+ function getPathSafeName ( name ) {
34+ return name . replace ( / @ / g, 'at_' ) . replace ( / \/ / g, '_' ) ;
35+ }
36+
37+ async function printFile ( path ) {
38+ const readStream = fs . createReadStream ( path ) ;
39+ readStream . pipe ( process . stdout ) ;
40+ await new Promise ( ( resolve , reject ) => {
41+ readStream . once ( 'end' , resolve ) ;
42+ readStream . once ( 'error' , reject ) ;
3743 } ) ;
38- writeFileSync (
39- path . join ( LOGDIR , `${ safeName } -ci-summary.txt` ) ,
40- `${ status } : ${ name } ` ,
41- { encoding : 'utf8' }
42- ) ;
4344}
4445
4546const argv = yargs . options ( {
@@ -60,34 +61,43 @@ const argv = yargs.options({
6061 let scriptName = argv . s ;
6162 const dir = path . resolve ( myPath ) ;
6263 const { name } = require ( `${ dir } /package.json` ) ;
64+ const testOutputFile = path . join (
65+ LOGDIR ,
66+ `${ getPathSafeName ( name ) } -ci-log.txt`
67+ ) ;
6368
64- let stdout = '' ;
65- let stderr = '' ;
66- try {
67- if ( process . env ?. BROWSERS ) {
68- for ( const package in crossBrowserPackages ) {
69- if ( dir . endsWith ( package ) ) {
70- scriptName = crossBrowserPackages [ package ] ;
71- }
69+ if ( process . env ?. BROWSERS ) {
70+ for ( const package in crossBrowserPackages ) {
71+ if ( dir . endsWith ( package ) ) {
72+ scriptName = crossBrowserPackages [ package ] ;
7273 }
7374 }
74- const testProcess = spawn ( 'yarn' , [ '--cwd' , dir , scriptName ] ) ;
75+ }
76+
77+ const testOutputFileHandle = fs . openSync ( testOutputFile , 'w' ) ;
78+ const testProcess = spawn ( 'yarn' , [ '--cwd' , dir , scriptName ] , {
79+ stdio : [ 'inherit' , testOutputFileHandle , testOutputFileHandle ]
80+ } ) ;
81+
82+ const exitCode = await new Promise ( ( resolve , reject ) => {
83+ testProcess . once ( 'close' , resolve ) ;
84+ testProcess . once ( 'error' , reject ) ;
85+ } ) . finally ( ( ) => {
86+ fs . closeSync ( testOutputFileHandle ) ;
87+ } ) ;
7588
76- testProcess . childProcess . stdout . on ( 'data' , data => {
77- stdout += data . toString ( ) ;
78- } ) ;
79- testProcess . childProcess . stderr . on ( 'data' , data => {
80- stderr += data . toString ( ) ;
81- } ) ;
89+ const resultStr = exitCode === 0 ? 'Success' : 'Failure' ;
90+ console . log ( `${ resultStr } : ${ name } ` ) ;
91+ await printFile ( testOutputFile ) ;
8292
83- await testProcess ;
84- console . log ( 'Success: ' + name ) ;
85- writeLogs ( 'Success' , name , stdout + '\n' + stderr ) ;
86- } catch ( e ) {
87- console . error ( 'Failure: ' + name ) ;
88- console . log ( stdout ) ;
89- console . error ( stderr ) ;
90- writeLogs ( 'Failure' , name , stdout + '\n' + stderr ) ;
91- process . exit ( 1 ) ;
92- }
93+ fs . writeFileSync (
94+ path . join ( LOGDIR , ` ${ getPathSafeName ( name ) } -ci-summary.txt` ) ,
95+ ` ${ resultStr } : ${ name } ` ,
96+ { encoding : 'utf8' }
97+ ) ;
98+
99+ // NOTE: Set `process.exitCode` rather than calling `process.exit()` because
100+ // the latter will exit forcefully even if stdout/ stderr have not been fully
101+ // flushed, leading to truncated output.
102+ process . exitCode = exitCode ;
93103} ) ( ) ;
0 commit comments