Skip to content

Instantly share code, notes, and snippets.

@soldair
Last active February 8, 2016 15:12
Show Gist options
  • Save soldair/e8d95fba17c7ac4e84da to your computer and use it in GitHub Desktop.
Save soldair/e8d95fba17c7ac4e84da to your computer and use it in GitHub Desktop.
tailfd directory example
var fs =require('fs')
var path = require('path')
// these will need to be in the package.json
var tailfd = require('tailfd')
var through = require('through2')
// use:
// var taildir = require('./')
// var stream = taildir(dir)
// stream.on('data',function(data){
// console.log(data.line)
// console.log(data.info.filePath,'the full path to the file')
// })
module.exports = function(dir){
var watchers = {}
var stream = through.obj()
var ended = false;
setInterval(function(){
fs.readdir(dir,function(err,files){
if(err) return console.log('error reading dir',err)
var add = []
// look at all files and find ones that we are not watching
files.forEach(function(file){
if(file === '.') return;
var fullPath = path.join(dir,file)
if(!watchers[fullPath]) add.push(fullPath)
})
if(!add.length) return
add.forEach(function(fullPath){
fs.stat(fullPath,function(err,stat){
if(err) return console.log('error stat ',err)
if(!stat.isFile()) return; // only follow files. probably want to ignore more ethan just this
watchers[fullPath] = tailfd(fullPath,function(line,fdInfo){
// set the file path in fdInfo
fdInfo.filePath = fullPath
// send data event with line and info so it can be a normal stream
if(!ended) stream.write({line:line,info:fdInfo})
})
})
})
})
},5000).unref()
// support pause and resume essential for making programs the don't blow up.
stream.on('pause',function(){
Object.keys(watchers).forEach(function(pause){
watchers[k].pause()
})
})
stream.on('drain',function(){
Object.keys(watchers).forEach(function(pause){
watchers[k].resume()
})
})
// cleanup when you are done with the stream
stream.on('end',cleanup).on('error',cleanup)
return stream
function cleanup(){
ended = true
Object.keys(watchers).forEach(function(k){
watchers[k].close()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment