


















@@ -298,17 +298,12 @@ export async function runWatchMain(params = {}) {
298298let shuttingDown = false;
299299let restartRequested = false;
300300let watchProcess = null;
301+let watcher = null;
301302let lockHandle = null;
302303let autoDoctorAttempted = false;
303304let onSigInt;
304305let onSigTerm;
305306306-const watcher = deps.createWatcher(deps.watchPaths, {
307-ignoreInitial: true,
308-ignored: (watchPath, stats) =>
309-isIgnoredWatchPath(watchPath, deps.cwd, deps.watchPaths, stats),
310-});
311-312307const settle = (code) => {
313308if (settled) {
314309return;
@@ -321,7 +316,7 @@ export async function runWatchMain(params = {}) {
321316deps.process.off("SIGTERM", onSigTerm);
322317}
323318releaseWatchLock(lockHandle);
324-watcher.close?.().catch?.(() => {});
319+watcher?.close?.().catch?.(() => {});
325320resolve(code);
326321};
327322@@ -354,6 +349,14 @@ export async function runWatchMain(params = {}) {
354349});
355350};
356351352+const handleWatcherError = () => {
353+shuttingDown = true;
354+if (watchProcess && typeof watchProcess.kill === "function") {
355+watchProcess.kill(WATCH_RESTART_SIGNAL);
356+}
357+settle(1);
358+};
359+357360const runAutoDoctorAndRestart = () => {
358361autoDoctorAttempted = true;
359362logWatcher(
@@ -402,16 +405,17 @@ export async function runWatchMain(params = {}) {
402405}
403406};
404407405-watcher.on("add", requestRestart);
406-watcher.on("change", requestRestart);
407-watcher.on("unlink", requestRestart);
408-watcher.on("error", () => {
409-shuttingDown = true;
410-if (watchProcess && typeof watchProcess.kill === "function") {
411-watchProcess.kill(WATCH_RESTART_SIGNAL);
412-}
413-settle(1);
414-});
408+const startWatcher = () => {
409+watcher = deps.createWatcher(deps.watchPaths, {
410+ignoreInitial: true,
411+ignored: (watchPath, stats) =>
412+isIgnoredWatchPath(watchPath, deps.cwd, deps.watchPaths, stats),
413+});
414+watcher.on("add", requestRestart);
415+watcher.on("change", requestRestart);
416+watcher.on("unlink", requestRestart);
417+watcher.on("error", handleWatcherError);
418+};
415419416420onSigInt = () => {
417421shuttingDown = true;
@@ -434,6 +438,7 @@ export async function runWatchMain(params = {}) {
434438if (deps.lockDisabled) {
435439lockHandle = { lockPath: "", pid: deps.process.pid };
436440startRunner();
441+startWatcher();
437442return;
438443}
439444@@ -445,6 +450,7 @@ export async function runWatchMain(params = {}) {
445450}
446451lockHandle = handle;
447452startRunner();
453+startWatcher();
448454})
449455.catch((error) => {
450456logWatcher(`Failed to acquire watcher lock: ${error?.message ?? "unknown error"}`, deps);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。