forked from magento/pwa-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch-all.js
131 lines (116 loc) · 3.51 KB
/
watch-all.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require('events').EventEmitter.defaultMaxListeners = 100;
const chalk = require('chalk');
const chokidar = require('chokidar');
const execa = require('execa');
const figures = require('figures');
const debounce = require('lodash.debounce');
const path = require('path');
const warn = (msg, ...args) => {
console.warn(
chalk.yellowBright(`\n ${figures.warning} ${msg}\n`),
...args
);
};
const gracefulExit = () => {
warn('Exiting watch mode.');
process.exit(0);
};
process.on('SIGINT', gracefulExit);
const rootDir = path.resolve(__dirname, '..');
const restartDevServerOnChange = [
'packages/pwa-buildpack/lib/**/*.js',
'packages/upward-js/lib/**/*.js',
'packages/venia-*/*.{js,json,yml}',
'packages/venia-*/.env',
'packages/venia-*/static/**/*',
'packages/venia-*/templates/**/*',
'packages/venia-*/static/**/*',
'yarn.lock'
];
const eventBuffer = [];
/**
* Summarizes a buffer of event objects into an object of event object format.
*
* @returns [Array] like [{ name: "change", file: "10 file(s)"}, { name: "unlink", file: "2 file(s)"]
*/
function summarizeEvents() {
const typeMap = eventBuffer.reduce((summaries, { name }) => {
summaries[name] = (summaries[name] || 0) + 1;
return summaries;
}, {});
return Object.entries(typeMap).map(([name, value]) => ({
name,
file: `${value} file(s)`
}));
}
let devServer;
function startDevServer() {
eventBuffer.length = 0;
devServer = execa(
'webpack-dev-server',
['--stdin', '--progress', '--color', '--env.mode', 'development'],
{
cwd: path.join(rootDir, 'packages/venia-concept'),
localDir: path.join(rootDir, 'node_modules/.bin')
}
);
devServer.on('exit', () => {
devServer.exited = true;
});
devServer.stdout.pipe(process.stdout);
devServer.stderr.pipe(process.stderr);
}
let isClosing = false;
const runVeniaWatch = debounce(() => {
if (!devServer) {
warn('Launching webpack-dev-server');
return startDevServer();
}
const fileSummary =
eventBuffer.length > 20 ? summarizeEvents() : eventBuffer;
warn(
`Relaunching webpack-dev-server due to: \n - ${fileSummary
.map(
({ name, file }) =>
`${chalk.yellow(name)} ${chalk.whiteBright(file)}`
)
.join('\n - ')}\n`
);
if (devServer.exited) {
return startDevServer();
}
if (!isClosing) {
devServer.on('close', () => {
isClosing = false;
devServer = false;
startDevServer();
});
isClosing = true;
devServer.stdout.unpipe(process.stdout);
devServer.stderr.unpipe(process.stderr);
devServer.kill();
}
}, 800);
function watchRestartRequirements() {
return chokidar.watch(restartDevServerOnChange, {
ignored: '**/__*__/**/*'
});
}
function watchVeniaWithRestarts() {
const eventsToListenTo = ['add', 'change', 'unlink'];
const watcher = watchRestartRequirements();
const enqueue = (name, file) => {
eventBuffer.push({ name, file });
runVeniaWatch();
};
// chokidar appears not to have `.removeEventListener`, so this is the next
// best thing: just reassign functions.
let handler = debounce(() => {
handler = enqueue;
runVeniaWatch();
}, 900);
eventsToListenTo.forEach(name =>
watcher.on(name, file => handler(name, file))
);
}
watchVeniaWithRestarts();