MCPcopy Create free account
hub / github.com/Snapchat/Valdi / startWatching

Function startWatching

npm_modules/cli/src/commands/log.ts:31–53  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

29 };
30
31 const startWatching = () => {
32 isWatching = true;
33 fs.watch(filePath, { signal: abortController.signal }, eventType => {
34 if (eventType === 'change') {
35 const newFileSize = fs.statSync(filePath).size;
36 if (newFileSize > fileSize) {
37 const readStream = fs.createReadStream(filePath, { encoding: 'utf8', start: fileSize });
38 readStream.on('data', chunk => {
39 process.stdout.write(chunk);
40 });
41 fileSize = newFileSize;
42 }
43 } else if (eventType === 'rename') {
44 isWatching = false;
45 if (fs.existsSync(filePath)) {
46 // When files is written, sometimes the first reported event
47 // occurs when the write is incomplete, so verifying the file exists
48 // before reading and restarting the watch
49 readInitialContent();
50 }
51 }
52 });
53 };
54
55 readInitialContent();
56

Callers 1

readInitialContentFunction · 0.85

Calls 4

readInitialContentFunction · 0.85
watchMethod · 0.80
onMethod · 0.65
writeMethod · 0.65

Tested by

no test coverage detected