Skip to content

Commit

Permalink
fix crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
zetlen committed Oct 4, 2018
1 parent 11a3f06 commit 1fcba43
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 55 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
110 changes: 55 additions & 55 deletions scripts/watch-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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);
Expand Down Expand Up @@ -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',
Expand All @@ -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 = [];

Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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)),
Expand Down Expand Up @@ -217,7 +217,7 @@ function watchVeniaWithRestarts() {
);
}

Promise.all([buildPrerequisites(), watchDependencies()])
watchDependencies()
.then(watchVeniaWithRestarts)
.catch(e => {
console.error(e);
Expand Down

0 comments on commit 1fcba43

Please sign in to comment.