17
17
18
18
const yargs = require ( 'yargs' ) ;
19
19
const 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' ) ;
22
22
23
23
const LOGDIR = process . env . CI ? process . env . HOME : '/tmp' ;
24
24
// Maps the packages where we should not run `test:all` and instead isolate the cross-browser tests.
@@ -30,16 +30,17 @@ const crossBrowserPackages = {
30
30
'packages/firestore-compat' : 'test:browser'
31
31
} ;
32
32
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 ) ;
37
43
} ) ;
38
- writeFileSync (
39
- path . join ( LOGDIR , `${ safeName } -ci-summary.txt` ) ,
40
- `${ status } : ${ name } ` ,
41
- { encoding : 'utf8' }
42
- ) ;
43
44
}
44
45
45
46
const argv = yargs . options ( {
@@ -60,33 +61,44 @@ const argv = yargs.options({
60
61
let scriptName = argv . s ;
61
62
const dir = path . resolve ( myPath ) ;
62
63
const { name } = require ( `${ dir } /package.json` ) ;
64
+ const safeName = name . replace ( / @ / g, 'at_' ) . replace ( / \/ / g, '_' ) ;
65
+ const testOutputFile = path . join (
66
+ LOGDIR ,
67
+ `${ getPathSafeName ( name ) } -ci-log.txt`
68
+ ) ;
63
69
64
- try {
65
- if ( process . env ?. BROWSERS ) {
66
- for ( const package in crossBrowserPackages ) {
67
- if ( dir . endsWith ( package ) ) {
68
- scriptName = crossBrowserPackages [ package ] ;
69
- }
70
+ if ( process . env ?. BROWSERS ) {
71
+ for ( const package in crossBrowserPackages ) {
72
+ if ( dir . endsWith ( package ) ) {
73
+ scriptName = crossBrowserPackages [ package ] ;
70
74
}
71
75
}
72
- const testProcessResult = await spawn ( 'yarn' , [ '--cwd' , dir , scriptName ] , {
73
- capture : [ 'stdout' , 'stderr' ]
74
- } ) ;
75
- console . log ( 'Success: ' + name ) ;
76
- writeLogs (
77
- 'Success' ,
78
- name ,
79
- testProcessResult . stdout + '\n' + testProcessResult . stderr
80
- ) ;
81
- } catch ( e ) {
82
- console . error ( 'Failure: ' + name ) ;
83
- console . log ( e . stdout ) ;
84
- console . error ( e . stderr ) ;
85
- writeLogs ( 'Failure' , name , e . stdout + '\n' + e . stderr ) ;
86
-
87
- // NOTE: Set `process.exitCode` rather than calling `process.exit()` because
88
- // the latter will exit forcefully even if stdout/stderr have not been fully
89
- // flushed, leading to truncated output.
90
- process . exitCode = 1 ;
91
76
}
77
+
78
+ const testOutputFileHandle = fs . openSync ( testOutputFile , 'w' ) ;
79
+ const testProcess = spawn ( 'yarn' , [ '--cwd' , dir , scriptName ] , {
80
+ stdio : [ 'inherit' , testOutputFileHandle , testOutputFileHandle ]
81
+ } ) ;
82
+
83
+ const exitCode = await new Promise ( ( resolve , reject ) => {
84
+ testProcess . once ( 'close' , resolve ) ;
85
+ testProcess . once ( 'error' , reject ) ;
86
+ } ) . finally ( ( ) => {
87
+ fs . closeSync ( testOutputFileHandle ) ;
88
+ } ) ;
89
+
90
+ const resultStr = exitCode === 0 ? 'Success' : 'Failure' ;
91
+ console . log ( `${ resultStr } : ` + name ) ;
92
+ await printFile ( testOutputFile ) ;
93
+
94
+ fs . writeFileSync (
95
+ path . join ( LOGDIR , `${ getPathSafeName ( name ) } -ci-summary.txt` ) ,
96
+ `${ resultStr } : ${ name } ` ,
97
+ { encoding : 'utf8' }
98
+ ) ;
99
+
100
+ // NOTE: Set `process.exitCode` rather than calling `process.exit()` because
101
+ // the latter will exit forcefully even if stdout/stderr have not been fully
102
+ // flushed, leading to truncated output.
103
+ process . exitCode = exitCode ;
92
104
} ) ( ) ;
0 commit comments