@@ -4,25 +4,23 @@ const common = require('../common');
4
4
const spawn = require ( 'child_process' ) . spawn ;
5
5
6
6
let run = ( ) => { } ;
7
- function test ( extraArgs ) {
7
+ function test ( extraArgs , stdoutPattern ) {
8
8
const next = run ;
9
9
run = ( ) => {
10
+ var procStdout = '' ;
10
11
var procStderr = '' ;
11
12
var agentStdout = '' ;
13
+ var debuggerListening = false ;
14
+ var outputMatched = false ;
12
15
var needToSpawnAgent = true ;
13
16
var needToExit = true ;
14
17
15
18
const procArgs = [ `--debug-brk=${ common . PORT } ` ] . concat ( extraArgs ) ;
16
19
const proc = spawn ( process . execPath , procArgs ) ;
17
20
proc . stderr . setEncoding ( 'utf8' ) ;
18
21
19
- const exitAll = common . mustCall ( ( processes ) => {
20
- processes . forEach ( ( myProcess ) => { myProcess . kill ( ) ; } ) ;
21
- } ) ;
22
-
23
- proc . stderr . on ( 'data' , ( chunk ) => {
24
- procStderr += chunk ;
25
- if ( / D e b u g g e r l i s t e n i n g o n / . test ( procStderr ) && needToSpawnAgent ) {
22
+ const tryStartAgent = ( ) => {
23
+ if ( debuggerListening && outputMatched && needToSpawnAgent ) {
26
24
needToSpawnAgent = false ;
27
25
const agentArgs = [ 'debug' , `localhost:${ common . PORT } ` ] ;
28
26
const agent = spawn ( process . execPath , agentArgs ) ;
@@ -36,6 +34,27 @@ function test(extraArgs) {
36
34
}
37
35
} ) ;
38
36
}
37
+ } ;
38
+
39
+ const exitAll = common . mustCall ( ( processes ) => {
40
+ processes . forEach ( ( myProcess ) => { myProcess . kill ( ) ; } ) ;
41
+ } ) ;
42
+
43
+ if ( stdoutPattern != null ) {
44
+ proc . stdout . on ( 'data' , ( chunk ) => {
45
+ procStdout += chunk ;
46
+ outputMatched = outputMatched || stdoutPattern . test ( procStdout ) ;
47
+ tryStartAgent ( ) ;
48
+ } ) ;
49
+ } else {
50
+ outputMatched = true ;
51
+ }
52
+
53
+ proc . stderr . on ( 'data' , ( chunk ) => {
54
+ procStderr += chunk ;
55
+ debuggerListening = debuggerListening ||
56
+ / D e b u g g e r l i s t e n i n g o n / . test ( procStderr ) ;
57
+ tryStartAgent ( ) ;
39
58
} ) ;
40
59
41
60
proc . on ( 'exit' , ( ) => {
@@ -46,5 +65,6 @@ function test(extraArgs) {
46
65
47
66
test ( [ '-e' , '0' ] ) ;
48
67
test ( [ '-e' , '0' , 'foo' ] ) ;
68
+ test ( [ '-p' , 'process.argv[1]' , 'foo' ] , / ^ \s * f o o \s * $ / ) ;
49
69
50
70
run ( ) ;
0 commit comments