Last active
December 16, 2015 23:49
-
-
Save cristiandouce/5515997 to your computer and use it in GitHub Desktop.
Files inspector
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
var fs = require('fs') | |
, path = require('path') | |
, EventEmitter = require('events').EventEmitter; | |
function Inspector () { | |
var _self = this; | |
_self.on('dir', function(dir) { | |
fs.readdir(dir, function(err, files) { | |
if (err) { | |
return console.log(err, 'at', dir); | |
} | |
_self.emit('dir lecture', files); | |
}); | |
}) | |
_self.on('dir lecture', function(files) { | |
files.forEach(function(file) { | |
_self.emit('file', file); | |
}) | |
}); | |
_self.on('file', function(file) { | |
fs.stat(file, function(err, stat) { | |
if (err) { | |
return console.log(err); | |
} | |
if (stat.isDirectory()) { | |
_self.emit('dir', file); | |
} | |
if (stat.isFile()) { | |
_self.emit('file lecture', file, stat); | |
}; | |
}) | |
}); | |
this.on('file lecture', function(file, stats) { | |
// should filter and build file object | |
// for file location saving to db | |
console.log(file); | |
}); | |
} | |
/** | |
* Inherits from EventEmitter. | |
*/ | |
Inspector.prototype.__proto__ = EventEmitter.prototype; | |
Inspector.prototype.init = function(start) { | |
if (!this._initialized) { | |
this.emit('file', start); | |
} | |
this._initialized = true; | |
return this; | |
} | |
var inspector = new Inspector(); | |
inspector.init(path.resolve('/')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment