Skip to content

Instantly share code, notes, and snippets.

@IrakliJani
Created August 13, 2017 18:38
Show Gist options
  • Save IrakliJani/c2294fe6c7d185e2fd420e5ebef7b18c to your computer and use it in GitHub Desktop.
Save IrakliJani/c2294fe6c7d185e2fd420e5ebef7b18c to your computer and use it in GitHub Desktop.
Sergo File Watcher
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