/* 监听模式 */ startWatch(): void { let config = this.config; let watchFile: Array<string> = []; if (config.file || config.file.length) { for(let file of config.file) { file = file.replace(/\\/g, '/'); watchFile.push(path.resolve(this.basePath, file)); } if (this.configFile) { // 命令行模式没有配置文件 watchFile.push(this.configFile); } if (config.browserSync) { if (!this.browserSync) { this.browserSync = browserSync.create(); } if (config.browserSync.init) { this.browserSync.init(config.browserSync.init); } else { this.browserSync.init(); } this.watcher = this.browserSync.watch(watchFile).on('change', this.compileCallback.bind(this)); } else { console.log('watch model,files:\n'+watchFile.join('\n')); this.watcher = chokidar.watch(watchFile).on('change', this.compileCallback.bind(this)); } } }
.then(() => { if (program['watch']) { let watcher = chokidar.watch('*', { cwd: workingDirectory }); watcher .on('add', path => { if (match.test(path)) { processFile(pathUtils.join(workingDirectory, path)); } }) .on('change', path => { if (match.test(path)) { processFile(pathUtils.join(workingDirectory, path)); } }) .on('unlink', path => { if (match.test(path)) { let destFileName = getDestFileName(pathUtils.join(workingDirectory, path)); if (fs.existsSync(destFileName)) { fs.unlinkSync(destFileName) } } }); let watched = watcher.getWatched(); } });
chan = yield eventChannel((emit) => { const watcher = chokidar.watch(allUris); watcher.on("ready", () => { const emitChange = (path) => { const mtime = fs.lstatSync(path).mtime; const fileCacheItem = initialFileCache.find((item) => item.filePath === path); if (fileCacheItem && fileCacheItem.mtime === mtime) { return; } const newContent = fs.readFileSync(path, "utf8"); const publicPath = getPublicFilePath(path, state); // for internal -- do not want this being sent over the network since it is slow emit(fileContentChanged(path, publicPath, newContent, mtime)); } watcher.on("add", emitChange); watcher.on("change", emitChange); watcher.on("unlink", (path) => { emit(fileRemoved(path, getPublicFilePath(path, state))); }); }); return () => { watcher.close(); }; });
private watchFiles(watchPaths: string[]) { this.clearModifyTimeout(); if (watchPaths.length > 0) { let theThis = this; this.fileWatcher = fs_watcher.watch(watchPaths); this.fileWatcher.on('change', function (path, stats) { theThis.clearModifyTimeout(); theThis.reloadTimer = setTimeout( function () { theThis.reloadActiveModel(); theThis.notifyModelUpdate(); theThis.reloadTimer = undefined; }, 400); }); } }
export function createFsWatcher(events: d.BuildEvents, paths: string, opts: any) { const chokidar = require('chokidar'); const watcher = chokidar.watch(paths, opts); watcher .on('change', (path: string) => { events.emit('fileUpdate', path); }) .on('add', (path: string) => { events.emit('fileAdd', path); }) .on('unlink', (path: string) => { events.emit('fileDelete', path); }) .on('addDir', (path: string) => { events.emit('dirAdd', path); }) .on('unlinkDir', (path: string) => { events.emit('dirDelete', path); }) .on('error', (err: any) => { console.error(err); }); return watcher; }
onFileChange: (options, listener, ready: () => void) => { if (!options.basePath) { reportDiagnostics([{ category: ts.DiagnosticCategory.Error, messageText: 'Invalid configuration option. baseDir not specified', source: api.SOURCE, code: api.DEFAULT_ERROR_CODE }]); return {close: () => {}}; } const watcher = chokidar.watch(options.basePath, { // ignore .dotfiles, .js and .map files. // can't ignore other files as we e.g. want to recompile if an `.html` file changes as well. ignored: /((^[\/\\])\..)|(\.js$)|(\.map$)|(\.metadata\.json)/, ignoreInitial: true, persistent: true, }); watcher.on('all', (event: string, path: string) => { switch (event) { case 'change': listener(FileChangeEvent.Change, path); break; case 'unlink': case 'add': listener(FileChangeEvent.CreateDelete, path); break; case 'unlinkDir': case 'addDir': listener(FileChangeEvent.CreateDeleteDir, path); break; } }); watcher.on('ready', ready); return {close: () => watcher.close(), ready}; },
export function fileReload(path: string, callback: ContentReadyCallback) { const watcher: fs.FSWatcher = watch(path); watcher.on('add', file=>readContent(file, callback)); watcher.on('change', file=>readContent(file, callback)); watcher.on('error', error=>console.log(`Watcher error: ${error}`)); }
constructor(path: string, handleUpdate: any) { this.currentPath = path this.handleUpdate = handleUpdate this.watcher = chokidar.watch(this.currentPath) this.watcher.on('add', this.onUpdate.bind(this)) this.watcher.on('change', this.onUpdate.bind(this)) }
export function watch(watchDirectory: FilePath, sourcePath: FilePath, outputFolder: FilePath) { let watcher = chokidar.watch([watchDirectory + globSuffix]) .on('add', () => build(sourcePath, outputFolder)) .on('change', () => build(sourcePath, outputFolder)); return watcher; }
private watchConfigFiles() { const watcher = chokidar.watch([configFile, secretsFile, webpackFile], { cwd: this.appDir, ignoreInitial: true }) watcher.on("add", this.onConfigFileUpdate.bind(this, "add")) watcher.on("change", this.onConfigFileUpdate.bind(this, "change")) }