/
watch-cli.ts
159 lines (142 loc) · 4.49 KB
/
watch-cli.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
import { promises as fs, type FSWatcher } from 'node:fs';
import process from 'node:process';
import chokidar from 'chokidar';
import dateTime from 'date-time';
import ms from 'pretty-ms';
import onExit from 'signal-exit';
import * as rollup from '../../src/node-entry';
import type { MergedRollupOptions, RollupWatcher } from '../../src/rollup/types';
import { bold, cyan, green, underline } from '../../src/utils/colors';
import relativeId from '../../src/utils/relativeId';
import { handleError, stderr } from '../logging';
import type { BatchWarnings } from './batchWarnings';
import { getConfigPath } from './getConfigPath';
import { loadConfigFile } from './loadConfigFile';
import loadConfigFromCommand from './loadConfigFromCommand';
import { getResetScreen } from './resetScreen';
import { printTimings } from './timings';
import { createWatchHooks } from './watchHooks';
export async function watch(command: Record<string, any>): Promise<void> {
process.env.ROLLUP_WATCH = 'true';
const isTTY = process.stderr.isTTY;
const silent = command.silent;
let watcher: RollupWatcher;
let configWatcher: FSWatcher;
let resetScreen: (heading: string) => void;
const configFile = command.config ? await getConfigPath(command.config) : null;
const runWatchHook = createWatchHooks(command);
onExit(close);
process.on('uncaughtException', close);
if (!process.stdin.isTTY) {
process.stdin.on('end', close);
process.stdin.resume();
}
async function loadConfigFromFileAndTrack(configFile: string): Promise<void> {
let configFileData: string | null = null;
let configFileRevision = 0;
configWatcher = chokidar.watch(configFile).on('change', reloadConfigFile);
await reloadConfigFile();
async function reloadConfigFile() {
try {
const newConfigFileData = await fs.readFile(configFile, 'utf8');
if (newConfigFileData === configFileData) {
return;
}
configFileRevision++;
const currentConfigFileRevision = configFileRevision;
if (configFileData) {
stderr(`\nReloading updated config...`);
}
configFileData = newConfigFileData;
const { options, warnings } = await loadConfigFile(configFile, command);
if (currentConfigFileRevision !== configFileRevision) {
return;
}
if (watcher) {
await watcher.close();
}
start(options, warnings);
} catch (err: any) {
handleError(err, true);
}
}
}
if (configFile) {
await loadConfigFromFileAndTrack(configFile);
} else {
const { options, warnings } = await loadConfigFromCommand(command);
start(options, warnings);
}
function start(configs: MergedRollupOptions[], warnings: BatchWarnings): void {
try {
watcher = rollup.watch(configs as any);
} catch (err: any) {
return handleError(err);
}
watcher.on('event', event => {
switch (event.code) {
case 'ERROR':
warnings.flush();
handleError(event.error, true);
runWatchHook('onError');
break;
case 'START':
if (!silent) {
if (!resetScreen) {
resetScreen = getResetScreen(configs, isTTY);
}
resetScreen(underline(`rollup v${rollup.VERSION}`));
}
runWatchHook('onStart');
break;
case 'BUNDLE_START':
if (!silent) {
let input = event.input;
if (typeof input !== 'string') {
input = Array.isArray(input)
? input.join(', ')
: Object.values(input as Record<string, string>).join(', ');
}
stderr(
cyan(`bundles ${bold(input)} → ${bold(event.output.map(relativeId).join(', '))}...`)
);
}
runWatchHook('onBundleStart');
break;
case 'BUNDLE_END':
warnings.flush();
if (!silent)
stderr(
green(
`created ${bold(event.output.map(relativeId).join(', '))} in ${bold(
ms(event.duration)
)}`
)
);
runWatchHook('onBundleEnd');
if (event.result && event.result.getTimings) {
printTimings(event.result.getTimings());
}
break;
case 'END':
runWatchHook('onEnd');
if (!silent && isTTY) {
stderr(`\n[${dateTime()}] waiting for changes...`);
}
}
if ('result' in event && event.result) {
event.result.close().catch(error => handleError(error, true));
}
});
}
async function close(code: number | null): Promise<void> {
process.removeListener('uncaughtException', close);
// removing a non-existent listener is a no-op
process.stdin.removeListener('end', close);
if (watcher) await watcher.close();
if (configWatcher) configWatcher.close();
if (code) {
process.exit(code);
}
}
}