(command: Record<string, any>)
| 19 | import { createWatchHooks } from './watchHooks'; |
| 20 | |
| 21 | export async function watch(command: Record<string, any>): Promise<void> { |
| 22 | process.env.ROLLUP_WATCH = 'true'; |
| 23 | const isTTY = process.stderr.isTTY; |
| 24 | const silent = command.silent; |
| 25 | let watcher: RollupWatcher; |
| 26 | let configWatcher: FSWatcher; |
| 27 | let resetScreen: (heading: string) => void; |
| 28 | const configFile = command.config ? await getConfigPath(command.config) : null; |
| 29 | const runWatchHook = createWatchHooks(command); |
| 30 | |
| 31 | onExit(close); |
| 32 | process.on('uncaughtException', closeWithError); |
| 33 | |
| 34 | async function loadConfigFromFileAndTrack(configFile: string): Promise<void> { |
| 35 | let configFileData: string | null = null; |
| 36 | let configFileRevision = 0; |
| 37 | |
| 38 | configWatcher = chokidar.watch(configFile).on('change', reloadConfigFile); |
| 39 | await reloadConfigFile(); |
| 40 | |
| 41 | async function reloadConfigFile() { |
| 42 | try { |
| 43 | const newConfigFileData = await readFile(configFile, 'utf8'); |
| 44 | if (newConfigFileData === configFileData) { |
| 45 | return; |
| 46 | } |
| 47 | configFileRevision++; |
| 48 | const currentConfigFileRevision = configFileRevision; |
| 49 | if (configFileData) { |
| 50 | stderr(`\nReloading updated config...`); |
| 51 | } |
| 52 | configFileData = newConfigFileData; |
| 53 | const { options, warnings } = await loadConfigFile(configFile, command, true); |
| 54 | if (currentConfigFileRevision !== configFileRevision) { |
| 55 | return; |
| 56 | } |
| 57 | if (watcher) { |
| 58 | await watcher.close(); |
| 59 | } |
| 60 | start(options, warnings); |
| 61 | } catch (error: any) { |
| 62 | handleError(error, true); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (configFile) { |
| 68 | await loadConfigFromFileAndTrack(configFile); |
| 69 | } else { |
| 70 | const { options, warnings } = await loadConfigFromCommand(command, true); |
| 71 | await start(options, warnings); |
| 72 | } |
| 73 | |
| 74 | async function start(configs: MergedRollupOptions[], warnings: BatchWarnings): Promise<void> { |
| 75 | watcher = rollup.watch(configs as any); |
| 76 | |
| 77 | watcher.on('event', event => { |
| 78 | switch (event.code) { |
no test coverage detected
searching dependent graphs…