@@ -5,10 +5,14 @@ const figures = require('figures');
5
5
const chalk = require ( 'chalk' ) ;
6
6
const chokidar = require ( 'chokidar' ) ;
7
7
const debounce = require ( 'lodash.debounce' ) ;
8
+ const keypress = require ( 'keypress' ) ;
9
+ const StreamSnitch = require ( 'stream-snitch' ) ;
8
10
9
11
const warn = ( msg , ...args ) => {
10
- console . warn ( chalk . yellowBright ( `\n ${ figures . warning } ${ msg } ` ) , ...args ) ;
11
- console . warn ( '\n' ) ;
12
+ console . warn (
13
+ chalk . yellowBright ( `\n ${ figures . warning } ${ msg } \n` ) ,
14
+ ...args
15
+ ) ;
12
16
} ;
13
17
14
18
const gracefulExit = ( ) => {
@@ -18,6 +22,20 @@ const gracefulExit = () => {
18
22
19
23
process . on ( 'SIGINT' , gracefulExit ) ;
20
24
25
+ function afterEmit ( childProcess , regex , timeout = 5000 ) {
26
+ return new Promise ( ( resolve , reject ) => {
27
+ const timeoutId = setTimeout ( resolve , timeout ) ;
28
+ const snitch = new StreamSnitch ( regex ) ;
29
+ snitch . on ( 'match' , ( ) => {
30
+ clearTimeout ( timeoutId ) ;
31
+ resolve ( ) ;
32
+ } ) ;
33
+ childProcess . stdout . pipe ( snitch ) ;
34
+ childProcess . stderr . pipe ( snitch ) ;
35
+ childProcess . on ( 'error' , reject ) ;
36
+ } ) ;
37
+ }
38
+
21
39
function whenQuiet ( childProcess , timeout = 1000 ) {
22
40
return new Promise ( ( resolve , reject ) => {
23
41
childProcess . on ( 'error' , reject ) ;
@@ -48,12 +66,11 @@ const rootDir = path.resolve(__dirname, '..');
48
66
49
67
const localDir = path . join ( rootDir , 'node_modules/.bin' ) ;
50
68
51
- const mustBuildFirst = [ '@magento/peregrine' ] ;
52
-
53
- const mustWatch = [ '@magento/pwa-buildpack' ] ;
69
+ const mustWatch = [ '@magento/pwa-buildpack' , '@magento/peregrine' ] ;
54
70
55
71
const restartDevServerOnChange = [
56
72
'packages/pwa-buildpack/dist/**/*.js' ,
73
+ 'packages/peregrine/dist/**/*.js' ,
57
74
'packages/upward-js/lib/**/*.js' ,
58
75
'packages/venia-concept/*.{js,json,yml}' ,
59
76
'packages/venia-concept/.babelrc' ,
@@ -62,12 +79,9 @@ const restartDevServerOnChange = [
62
79
'package-lock.json'
63
80
] ;
64
81
65
- const spinner = new Multispinner (
66
- [ ...mustBuildFirst , ...mustWatch , 'webpack-dev-server' ] ,
67
- {
68
- preText : 'initial build of'
69
- }
70
- ) ;
82
+ const spinner = new Multispinner ( [ ...mustWatch , 'webpack-dev-server' ] , {
83
+ preText : 'initial build of'
84
+ } ) ;
71
85
72
86
const eventBuffer = [ ] ;
73
87
@@ -97,27 +111,32 @@ function startDevServer() {
97
111
localDir : path . join ( rootDir , 'node_modules/.bin' )
98
112
}
99
113
) ;
114
+ devServer . on ( 'exit' , ( ) => {
115
+ devServer . exited = true ;
116
+ } ) ;
100
117
devServer . stdout . pipe ( process . stdout ) ;
101
118
devServer . stderr . pipe ( process . stderr ) ;
102
- // whenQuiet(devServer, 3000).then(() => {
103
- // // make `process.stdin` begin emitting "keypress" events
104
- // keypress(process.stdin);
105
-
106
- // // listen for the "keypress" event
107
- // process.stdin.on('keypress', function(_, key) {
108
- // if (!key) {
109
- // return;
110
- // }
111
- // if (key.name === 'q' || (key.name === 'c' && key.ctrl)) {
112
- // gracefulExit();
113
- // }
114
- // });
115
-
116
- // process.stdin.setRawMode(true);
117
- // process.stdin.resume();
118
-
119
- // warn(`Press ${chalk.green.bold('q')} to exit the dev server.`);
120
- // });
119
+ afterEmit ( devServer , / C o m p i l e d s u c c e s s f u l l y / )
120
+ . then ( ( ) => whenQuiet ( devServer , 750 ) )
121
+ . then ( ( ) => {
122
+ // make `process.stdin` begin emitting "keypress" events
123
+ keypress ( process . stdin ) ;
124
+
125
+ // listen for the "keypress" event
126
+ process . stdin . on ( 'keypress' , function ( _ , key ) {
127
+ if ( ! key ) {
128
+ return ;
129
+ }
130
+ if ( key . name === 'q' || ( key . name === 'c' && key . ctrl ) ) {
131
+ gracefulExit ( ) ;
132
+ }
133
+ } ) ;
134
+
135
+ process . stdin . setRawMode ( true ) ;
136
+ process . stdin . resume ( ) ;
137
+
138
+ warn ( `Press ${ chalk . green . bold ( 'q' ) } to exit the dev server.` ) ;
139
+ } ) ;
121
140
}
122
141
123
142
let isClosing = false ;
@@ -136,16 +155,17 @@ const runVeniaWatch = debounce(() => {
136
155
)
137
156
. join ( '\n - ' ) } \n`
138
157
) ;
139
- console . log ( { isClosing } , eventBuffer ) ;
158
+ if ( devServer . exited ) {
159
+ return startDevServer ( ) ;
160
+ }
140
161
if ( ! isClosing ) {
141
- isClosing = true ;
142
162
devServer . on ( 'close' , ( ) => {
143
- console . log ( 'devServer.on(close' , eventBuffer ) ;
144
163
isClosing = false ;
164
+ devServer = false ;
145
165
startDevServer ( ) ;
146
166
} ) ;
167
+ isClosing = true ;
147
168
devServer . kill ( ) ;
148
- console . log ( 'sent kill signal' , eventBuffer ) ;
149
169
}
150
170
} , 800 ) ;
151
171
@@ -161,26 +181,6 @@ function runOnPackages(packages, cmd) {
161
181
) ;
162
182
}
163
183
164
- function buildPrerequisites ( ) {
165
- return runOnPackages ( mustBuildFirst , 'build' ) . then (
166
- ( ) => {
167
- mustBuildFirst . forEach ( dep => spinner . success ( dep ) ) ;
168
- } ,
169
- e => {
170
- const failedDeps = mustBuildFirst . filter (
171
- dep => e . toString ( ) . indexOf ( dep ) !== - 1
172
- ) ;
173
- if ( failedDeps . length === 0 ) {
174
- // something unexpected happened
175
- mustBuildFirst . forEach ( dep => spinner . error ( dep ) ) ;
176
- } else {
177
- failedDeps . forEach ( dep => spinner . error ( dep ) ) ;
178
- }
179
- throw e ;
180
- }
181
- ) ;
182
- }
183
-
184
184
function watchDependencies ( ) {
185
185
return whenQuiet ( runOnPackages ( mustWatch , 'watch' ) ) . then (
186
186
( ) => mustWatch . forEach ( dep => spinner . success ( dep ) ) ,
@@ -217,7 +217,7 @@ function watchVeniaWithRestarts() {
217
217
) ;
218
218
}
219
219
220
- Promise . all ( [ buildPrerequisites ( ) , watchDependencies ( ) ] )
220
+ watchDependencies ( )
221
221
. then ( watchVeniaWithRestarts )
222
222
. catch ( e => {
223
223
console . error ( e ) ;
0 commit comments