()
| 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 |
no test coverage detected