* Start the chokidar watcher for extensions on the local filesystem
()
| 472 | * Start the chokidar watcher for extensions on the local filesystem |
| 473 | */ |
| 474 | private initializeWatcher(): void { |
| 475 | const logger = useLogger(); |
| 476 | |
| 477 | logger.info('Watching extensions for changes...'); |
| 478 | |
| 479 | const extensionDirPath = pathToRelativeUrl(getExtensionsPath()); |
| 480 | |
| 481 | const resolvedExtDir = path.resolve(extensionDirPath); |
| 482 | |
| 483 | this.watcher = chokidar.watch([path.resolve('package.json'), extensionDirPath], { |
| 484 | ignoreInitial: true, |
| 485 | // Only top level watch inside extensions folder is necessary, "dist" paths are added via updateWatchedExtensions |
| 486 | depth: 1, |
| 487 | ignored: (val: string, stats?: Stats) => { |
| 488 | if (val.includes('node_modules')) return true; |
| 489 | |
| 490 | // allow directory traversal so chokidar can reach nested package.json files |
| 491 | if (!stats || stats.isDirectory()) return false; |
| 492 | |
| 493 | // allow root package.json and package.json in direct extension dirs (extensionsDir/*/package.json) |
| 494 | if (val.endsWith('package.json')) { |
| 495 | const resolvedVal = path.resolve(val); |
| 496 | |
| 497 | // root package.json |
| 498 | if (!resolvedVal.startsWith(resolvedExtDir + path.sep)) return false; |
| 499 | |
| 500 | // allow if within an extension folder dir |
| 501 | return path.dirname(resolvedVal) === resolvedExtDir; |
| 502 | } |
| 503 | |
| 504 | // allow "dist" entrypoints added via updateWatchedExtensions |
| 505 | if (this.isWatchedExtensionPath(val)) return false; |
| 506 | |
| 507 | return true; |
| 508 | }, |
| 509 | // on macOS dotdirs in linked extensions are watched too |
| 510 | followSymlinks: os.platform() === 'darwin' ? false : true, |
| 511 | }); |
| 512 | |
| 513 | this.watcher |
| 514 | .on( |
| 515 | 'add', |
| 516 | debounce(() => this.reload(), 500), |
| 517 | ) |
| 518 | .on( |
| 519 | 'change', |
| 520 | debounce(() => this.reload(), 650), |
| 521 | ) |
| 522 | .on( |
| 523 | 'unlink', |
| 524 | debounce(() => this.reload(), 2000), |
| 525 | ); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Close and destroy the local filesystem watcher if enabled |
no test coverage detected