Created
August 13, 2017 18:38
-
-
Save IrakliJani/c2294fe6c7d185e2fd420e5ebef7b18c to your computer and use it in GitHub Desktop.
Sergo File Watcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("path") | |
const fs = require("fs") | |
const endOfLine = require("os").EOL | |
// Config - | |
const fileDir = __dirname | |
const fileName = "in-file.txt" | |
const outFileDir = __dirname | |
const outFileName = "out-file.txt" | |
// - Config | |
const filePath = path.resolve(fileDir, fileName) | |
const outFilePath = path.resolve(outFileDir, outFileName) | |
fs.watchFile( | |
filePath, | |
{ interval: 1000 /* time in millisecond */ }, | |
({ mtime: currentModifiedTime }, { mtime: previousModifiedTime }) => { | |
if (currentModifiedTime !== previousModifiedTime) { | |
const fileContent = fs.readFileSync(filePath) | |
const outFileExists = fs.existsSync(outFilePath) | |
fs.appendFileSync( | |
outFilePath, | |
outFileExists ? endOfLine + fileContent : fileContent | |
) | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment