Skip to content

Commit 1fcba43

Browse files
author
zetlen
committed
fix crashes
1 parent 11a3f06 commit 1fcba43

File tree

3 files changed

+62
-55
lines changed

3 files changed

+62
-55
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
"redux-thunk": "^2.3.0",
105105
"rimraf": "^2.6.2",
106106
"storybook-readme": "^3.3.0",
107+
"stream-snitch": "0.0.3",
107108
"strip-ansi": "^4.0.0",
108109
"style-loader": "^0.23.0",
109110
"supertest": "^3.0.0",

scripts/watch-all.js

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ const figures = require('figures');
55
const chalk = require('chalk');
66
const chokidar = require('chokidar');
77
const debounce = require('lodash.debounce');
8+
const keypress = require('keypress');
9+
const StreamSnitch = require('stream-snitch');
810

911
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+
);
1216
};
1317

1418
const gracefulExit = () => {
@@ -18,6 +22,20 @@ const gracefulExit = () => {
1822

1923
process.on('SIGINT', gracefulExit);
2024

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+
2139
function whenQuiet(childProcess, timeout = 1000) {
2240
return new Promise((resolve, reject) => {
2341
childProcess.on('error', reject);
@@ -48,12 +66,11 @@ const rootDir = path.resolve(__dirname, '..');
4866

4967
const localDir = path.join(rootDir, 'node_modules/.bin');
5068

51-
const mustBuildFirst = ['@magento/peregrine'];
52-
53-
const mustWatch = ['@magento/pwa-buildpack'];
69+
const mustWatch = ['@magento/pwa-buildpack', '@magento/peregrine'];
5470

5571
const restartDevServerOnChange = [
5672
'packages/pwa-buildpack/dist/**/*.js',
73+
'packages/peregrine/dist/**/*.js',
5774
'packages/upward-js/lib/**/*.js',
5875
'packages/venia-concept/*.{js,json,yml}',
5976
'packages/venia-concept/.babelrc',
@@ -62,12 +79,9 @@ const restartDevServerOnChange = [
6279
'package-lock.json'
6380
];
6481

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+
});
7185

7286
const eventBuffer = [];
7387

@@ -97,27 +111,32 @@ function startDevServer() {
97111
localDir: path.join(rootDir, 'node_modules/.bin')
98112
}
99113
);
114+
devServer.on('exit', () => {
115+
devServer.exited = true;
116+
});
100117
devServer.stdout.pipe(process.stdout);
101118
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, /Compiled successfully/)
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+
});
121140
}
122141

123142
let isClosing = false;
@@ -136,16 +155,17 @@ const runVeniaWatch = debounce(() => {
136155
)
137156
.join('\n - ')}\n`
138157
);
139-
console.log({ isClosing }, eventBuffer);
158+
if (devServer.exited) {
159+
return startDevServer();
160+
}
140161
if (!isClosing) {
141-
isClosing = true;
142162
devServer.on('close', () => {
143-
console.log('devServer.on(close', eventBuffer);
144163
isClosing = false;
164+
devServer = false;
145165
startDevServer();
146166
});
167+
isClosing = true;
147168
devServer.kill();
148-
console.log('sent kill signal', eventBuffer);
149169
}
150170
}, 800);
151171

@@ -161,26 +181,6 @@ function runOnPackages(packages, cmd) {
161181
);
162182
}
163183

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-
184184
function watchDependencies() {
185185
return whenQuiet(runOnPackages(mustWatch, 'watch')).then(
186186
() => mustWatch.forEach(dep => spinner.success(dep)),
@@ -217,7 +217,7 @@ function watchVeniaWithRestarts() {
217217
);
218218
}
219219

220-
Promise.all([buildPrerequisites(), watchDependencies()])
220+
watchDependencies()
221221
.then(watchVeniaWithRestarts)
222222
.catch(e => {
223223
console.error(e);

0 commit comments

Comments
 (0)