/
watch.ts
209 lines (180 loc) · 4.84 KB
/
watch.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import dateTime from 'date-time';
import fs from 'fs';
import ms from 'pretty-ms';
import onExit from 'signal-exit';
import tc from 'turbocolor';
import * as rollup from '../../src/node-entry';
import {
InputOption,
RollupBuild,
RollupError,
RollupWatcher,
RollupWatchOptions,
WarningHandler,
WatcherOptions
} from '../../src/rollup/types';
import mergeOptions, { GenericConfigObject } from '../../src/utils/mergeOptions';
import relativeId from '../../src/utils/relativeId';
import { handleError, stderr } from '../logging';
import batchWarnings from './batchWarnings';
import loadConfigFile from './loadConfigFile';
import { getResetScreen } from './resetScreen';
import { printTimings } from './timings';
interface WatchEvent {
code?: string;
duration?: number;
error?: RollupError | Error;
input?: InputOption;
output?: string[];
result?: RollupBuild;
}
export default function watch(
configFile: string,
configs: GenericConfigObject[],
command: any,
silent = false
) {
const isTTY = Boolean(process.stderr.isTTY);
const warnings = batchWarnings();
const initialConfigs = processConfigs(configs);
const clearScreen = initialConfigs.every(
config => (config.watch as WatcherOptions).clearScreen !== false
);
const resetScreen = getResetScreen(isTTY && clearScreen);
let watcher: RollupWatcher;
let configWatcher: RollupWatcher;
function processConfigs(configs: GenericConfigObject[]): RollupWatchOptions[] {
return configs.map(options => {
const merged = mergeOptions({
command,
config: options,
defaultOnWarnHandler: warnings.add
});
const result: RollupWatchOptions = {
...merged.inputOptions,
output: merged.outputOptions
};
if (!result.watch) result.watch = {};
if (merged.optionError)
(merged.inputOptions.onwarn as WarningHandler)({
code: 'UNKNOWN_OPTION',
message: merged.optionError
});
return result;
});
}
function start(configs: RollupWatchOptions[]) {
watcher = rollup.watch(configs as any);
watcher.on('event', (event: WatchEvent) => {
switch (event.code) {
case 'FATAL':
handleError(event.error as RollupError, true);
process.exit(1);
break;
case 'ERROR':
warnings.flush();
handleError(event.error as RollupError, true);
break;
case 'START':
if (!silent) {
resetScreen(tc.underline(`rollup v${rollup.VERSION}`));
}
break;
case 'BUNDLE_START':
if (!silent) {
let input = event.input;
if (typeof input !== 'string') {
input = Array.isArray(input)
? input.join(', ')
: Object.keys(input as Record<string, string>)
.map(key => (input as Record<string, string>)[key])
.join(', ');
}
stderr(
tc.cyan(
`bundles ${tc.bold(input)} → ${tc.bold(
(event.output as string[]).map(relativeId).join(', ')
)}...`
)
);
}
break;
case 'BUNDLE_END':
warnings.flush();
if (!silent)
stderr(
tc.green(
`created ${tc.bold(
(event.output as string[]).map(relativeId).join(', ')
)} in ${tc.bold(ms(event.duration as number))}`
)
);
if (event.result && event.result.getTimings) {
printTimings(event.result.getTimings());
}
break;
case 'END':
if (!silent && isTTY) {
stderr(`\n[${dateTime()}] waiting for changes...`);
}
}
});
}
// catch ctrl+c, kill, and uncaught errors
const removeOnExit = onExit(close);
process.on('uncaughtException', close);
// only listen to stdin if it is a pipe
if (!process.stdin.isTTY) {
process.stdin.on('end', close); // in case we ever support stdin!
}
function close(err: Error) {
removeOnExit();
process.removeListener('uncaughtException', close);
// removing a non-existent listener is a no-op
process.stdin.removeListener('end', close);
if (watcher) watcher.close();
if (configWatcher) configWatcher.close();
if (err) {
console.error(err);
process.exit(1);
}
}
try {
start(initialConfigs);
} catch (err) {
close(err);
return;
}
if (configFile && !configFile.startsWith('node:')) {
let restarting = false;
let aborted = false;
let configFileData = fs.readFileSync(configFile, 'utf-8');
const restart = () => {
const newConfigFileData = fs.readFileSync(configFile, 'utf-8');
if (newConfigFileData === configFileData) return;
configFileData = newConfigFileData;
if (restarting) {
aborted = true;
return;
}
restarting = true;
loadConfigFile(configFile, command)
.then(() => {
restarting = false;
if (aborted) {
aborted = false;
restart();
} else {
watcher.close();
start(initialConfigs);
}
})
.catch((err: Error) => {
handleError(err, true);
});
};
configWatcher = fs.watch(configFile, (event: string) => {
if (event === 'change') restart();
});
}
}