From 1fcba43c155e89ce00d4c12ab3c2f3e403862d7c Mon Sep 17 00:00:00 2001 From: zetlen Date: Thu, 4 Oct 2018 04:31:36 -0500 Subject: [PATCH] fix crashes --- package-lock.json | 6 +++ package.json | 1 + scripts/watch-all.js | 110 +++++++++++++++++++++---------------------- 3 files changed, 62 insertions(+), 55 deletions(-) diff --git a/package-lock.json b/package-lock.json index 32d2ab10d64..177fc35db08 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23012,6 +23012,12 @@ "integrity": "sha1-1cdSgl5TZ+eG944Y5EXqIjoVWVI=", "dev": true }, + "stream-snitch": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/stream-snitch/-/stream-snitch-0.0.3.tgz", + "integrity": "sha1-iXp48TonFPqESqd74VR3qJbYUqk=", + "dev": true + }, "stream-splicer": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/stream-splicer/-/stream-splicer-1.3.2.tgz", diff --git a/package.json b/package.json index c7798c88ecd..a217220d145 100644 --- a/package.json +++ b/package.json @@ -104,6 +104,7 @@ "redux-thunk": "^2.3.0", "rimraf": "^2.6.2", "storybook-readme": "^3.3.0", + "stream-snitch": "0.0.3", "strip-ansi": "^4.0.0", "style-loader": "^0.23.0", "supertest": "^3.0.0", diff --git a/scripts/watch-all.js b/scripts/watch-all.js index 16b757f377c..bb564c4597f 100644 --- a/scripts/watch-all.js +++ b/scripts/watch-all.js @@ -5,10 +5,14 @@ const figures = require('figures'); const chalk = require('chalk'); const chokidar = require('chokidar'); const debounce = require('lodash.debounce'); +const keypress = require('keypress'); +const StreamSnitch = require('stream-snitch'); const warn = (msg, ...args) => { - console.warn(chalk.yellowBright(`\n ${figures.warning} ${msg}`), ...args); - console.warn('\n'); + console.warn( + chalk.yellowBright(`\n ${figures.warning} ${msg}\n`), + ...args + ); }; const gracefulExit = () => { @@ -18,6 +22,20 @@ const gracefulExit = () => { process.on('SIGINT', gracefulExit); +function afterEmit(childProcess, regex, timeout = 5000) { + return new Promise((resolve, reject) => { + const timeoutId = setTimeout(resolve, timeout); + const snitch = new StreamSnitch(regex); + snitch.on('match', () => { + clearTimeout(timeoutId); + resolve(); + }); + childProcess.stdout.pipe(snitch); + childProcess.stderr.pipe(snitch); + childProcess.on('error', reject); + }); +} + function whenQuiet(childProcess, timeout = 1000) { return new Promise((resolve, reject) => { childProcess.on('error', reject); @@ -48,12 +66,11 @@ const rootDir = path.resolve(__dirname, '..'); const localDir = path.join(rootDir, 'node_modules/.bin'); -const mustBuildFirst = ['@magento/peregrine']; - -const mustWatch = ['@magento/pwa-buildpack']; +const mustWatch = ['@magento/pwa-buildpack', '@magento/peregrine']; const restartDevServerOnChange = [ 'packages/pwa-buildpack/dist/**/*.js', + 'packages/peregrine/dist/**/*.js', 'packages/upward-js/lib/**/*.js', 'packages/venia-concept/*.{js,json,yml}', 'packages/venia-concept/.babelrc', @@ -62,12 +79,9 @@ const restartDevServerOnChange = [ 'package-lock.json' ]; -const spinner = new Multispinner( - [...mustBuildFirst, ...mustWatch, 'webpack-dev-server'], - { - preText: 'initial build of' - } -); +const spinner = new Multispinner([...mustWatch, 'webpack-dev-server'], { + preText: 'initial build of' +}); const eventBuffer = []; @@ -97,27 +111,32 @@ function startDevServer() { localDir: path.join(rootDir, 'node_modules/.bin') } ); + devServer.on('exit', () => { + devServer.exited = true; + }); devServer.stdout.pipe(process.stdout); devServer.stderr.pipe(process.stderr); - // whenQuiet(devServer, 3000).then(() => { - // // make `process.stdin` begin emitting "keypress" events - // keypress(process.stdin); - - // // listen for the "keypress" event - // process.stdin.on('keypress', function(_, key) { - // if (!key) { - // return; - // } - // if (key.name === 'q' || (key.name === 'c' && key.ctrl)) { - // gracefulExit(); - // } - // }); - - // process.stdin.setRawMode(true); - // process.stdin.resume(); - - // warn(`Press ${chalk.green.bold('q')} to exit the dev server.`); - // }); + afterEmit(devServer, /Compiled successfully/) + .then(() => whenQuiet(devServer, 750)) + .then(() => { + // make `process.stdin` begin emitting "keypress" events + keypress(process.stdin); + + // listen for the "keypress" event + process.stdin.on('keypress', function(_, key) { + if (!key) { + return; + } + if (key.name === 'q' || (key.name === 'c' && key.ctrl)) { + gracefulExit(); + } + }); + + process.stdin.setRawMode(true); + process.stdin.resume(); + + warn(`Press ${chalk.green.bold('q')} to exit the dev server.`); + }); } let isClosing = false; @@ -136,16 +155,17 @@ const runVeniaWatch = debounce(() => { ) .join('\n - ')}\n` ); - console.log({ isClosing }, eventBuffer); + if (devServer.exited) { + return startDevServer(); + } if (!isClosing) { - isClosing = true; devServer.on('close', () => { - console.log('devServer.on(close', eventBuffer); isClosing = false; + devServer = false; startDevServer(); }); + isClosing = true; devServer.kill(); - console.log('sent kill signal', eventBuffer); } }, 800); @@ -161,26 +181,6 @@ function runOnPackages(packages, cmd) { ); } -function buildPrerequisites() { - return runOnPackages(mustBuildFirst, 'build').then( - () => { - mustBuildFirst.forEach(dep => spinner.success(dep)); - }, - e => { - const failedDeps = mustBuildFirst.filter( - dep => e.toString().indexOf(dep) !== -1 - ); - if (failedDeps.length === 0) { - // something unexpected happened - mustBuildFirst.forEach(dep => spinner.error(dep)); - } else { - failedDeps.forEach(dep => spinner.error(dep)); - } - throw e; - } - ); -} - function watchDependencies() { return whenQuiet(runOnPackages(mustWatch, 'watch')).then( () => mustWatch.forEach(dep => spinner.success(dep)), @@ -217,7 +217,7 @@ function watchVeniaWithRestarts() { ); } -Promise.all([buildPrerequisites(), watchDependencies()]) +watchDependencies() .then(watchVeniaWithRestarts) .catch(e => { console.error(e);