11const execa = require ( "execa" ) ;
22const { resolve } = require ( "path" ) ;
33
4+ // Used for `MutationObserver`. Unsure if it's really needed, but it's worth mentioning that these are not tied to
5+ // specific CLI instances of `render`. This means that if there are e2e CLI tests that run in parallel, they will
6+ // execute far more frequently than needed.
7+ const _observers = new Map ( ) ;
8+
9+ // Not perfect as a way to make "MutationObserver" unique IDs, but it should work
10+ let mutId = 0 ;
11+
12+ class MutationObserver {
13+ constructor ( cb ) {
14+ this . _id = ++ mutId ;
15+ this . _cb = cb ;
16+ }
17+
18+ observe ( ) {
19+ _observers . set ( this . _id , this . _cb ) ;
20+ }
21+
22+ disconnect ( ) {
23+ _observers . delete ( this . _id ) ;
24+ }
25+ }
26+
427module . exports = {
528 /**
629 * @param {Array } args
@@ -25,61 +48,52 @@ module.exports = {
2548 }
2649 ) ;
2750
28- const stdoutArr = [ ] ;
29-
30- let localResolve = null ;
31- const isReady = new Promise ( resolve => localResolve = resolve ) ;
32- /**
33- * Made an array so if, for some reason, we have more than a single
34- * awaited promise on data waiting, this will call all of them, rather
35- * than accidentally hanging
36- *
37- * @type {{promise: Promise, resolve: () => void}[] }
38- */
51+ let _readyPromiseInternals = null ;
52+ const isReady = new Promise ( ( resolve , reject ) => ( _readyPromiseInternals = { resolve, reject} ) ) ;
53+
3954 const additionalExecProps = {
4055 // Clear buffer of stdout to do more accurate `t.regex` checks
4156 cleanup ( ) {
4257 this . stdoutArr = [ ] ;
4358 } ,
4459 // An array of strings gathered from stdout when unable to do
4560 // `await stdout` because of inquirer interactive prompts
46- stdoutArr,
47- _observers : new Map ( ) ,
61+ stdoutArr : [ ] ,
4862 _runObservers ( ) {
49- [ ...this . _observers . values ( ) ] . forEach ( cb => cb ( ) ) ;
63+ [ ..._observers . values ( ) ] . forEach ( ( cb ) => cb ( ) ) ;
5064 }
5165 } ;
5266
53- // Not perfect as a way to make "MutationObserver" unique IDs, but it should work
54- let mutId = 0 ;
55-
56- class MutationObserver {
57- constructor ( cb ) {
58- this . _id = ++ mutId ;
59- this . _cb = cb ;
60- }
61-
62- observe ( ) {
63- additionalExecProps . _observers . set ( this . _id , this . _cb ) ;
64- }
65-
66- disconnect ( ) {
67- additionalExecProps . _observers . delete ( this . _id ) ;
68- }
69- }
70-
7167 exec . stdout . on ( "data" , ( result ) => {
72- stdoutArr . push ( result . toString ( ) ) ;
68+ const resStr = result . toString ( ) ;
69+ additionalExecProps . stdoutArr . push ( resStr ) ;
7370 additionalExecProps . _runObservers ( ) ;
74- if ( ! isReady . pending ) localResolve ( ) ;
71+ // if (isReady.pending) {
72+ console . log ( "IS READY IS READY NOW" ) ;
73+ _readyPromiseInternals . resolve ( resStr ) ;
74+ // }
7575 } ) ;
7676
77- Object . assign ( exec , additionalExecProps , { MutationObserver} ) ;
77+ exec . stdout . on ( "error" , ( result ) => {
78+ const resStr = result . toString ( ) ;
79+ if ( isReady . pending ) _readyPromiseInternals . reject ( resStr ) ;
80+ } ) ;
81+
82+ exec . stderr . on ( 'data' , result => {
83+ if ( isReady . pending ) _readyPromiseInternals . reject ( result . toString ( ) ) ;
84+ } )
85+
86+ Object . assign ( exec , additionalExecProps ) ;
87+
88+ console . log ( "BEFORE AWAIT" )
7889
7990 await isReady ;
8091
92+ console . log ( "AFTER AWAIT" )
93+
8194 return exec ;
8295 } ,
96+ MutationObserver,
8397 DOWN : "\x1B\x5B\x42" ,
8498 UP : "\x1B\x5B\x41" ,
8599 ENTER : "\x0D" ,
0 commit comments