Created
March 2, 2010 03:37
-
-
Save tim-smart/319105 to your computer and use it in GitHub Desktop.
This file contains hidden or 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, path; | |
| fs = require('fs'); | |
| path = require('path'); | |
| process.mixin(global, require('sys')); | |
| exports.directoryWalker = function directoryWalker(dir, callback, maxLevels, currentLevel, fromRoot) { | |
| maxLevels = 'number' === typeof maxLevels ? maxLevels : 0; | |
| currentLevel = 'number' === typeof currentLevel ? currentLevel : 1; | |
| fromRoot = 'string' === typeof fromRoot ? fromRoot : ''; | |
| return fs.readdir(dir, function(error, files) { | |
| if (error) { | |
| puts(error.message); | |
| return null; | |
| } | |
| return files.forEach(function(file) { | |
| return fs.stat(path.join(dir, file), function(error, stats) { | |
| if (error) { | |
| puts(error.message); | |
| return null; | |
| } | |
| stats.isDirectory() ? 0 === maxLevels || maxLevels > currentLevel ? directoryWalker(path.join(dir, file), callback, maxLevels, 1 + currentLevel, fromRoot + file + '/') : null : null; | |
| return callback.call(stats, file, fromRoot, path.join(dir, file), stats); | |
| }); | |
| }); | |
| }); | |
| }; |
This file contains hidden or 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
| fs: require 'fs' | |
| path: require 'path' | |
| process.mixin global, require 'sys' | |
| exports.directoryWalker: (dir, callback, maxLevels, currentLevel, fromRoot) -> | |
| maxLevels: if 'number' is typeof maxLevels then maxLevels else 0 | |
| currentLevel: if 'number' is typeof currentLevel then currentLevel else 1 | |
| fromRoot: if 'string' is typeof fromRoot then fromRoot else '' | |
| fs.readdir dir, (error, files) -> | |
| if error | |
| puts error.message | |
| return | |
| files.forEach (file) -> | |
| fs.stat path.join(dir, file), (error, stats) -> | |
| if error | |
| puts error.message | |
| return | |
| if stats.isDirectory() | |
| if 0 is maxLevels or maxLevels > currentLevel | |
| directoryWalker path.join(dir, file), callback, | |
| maxLevels, 1 + currentLevel, | |
| fromRoot + file + '/' | |
| callback.call stats, file, fromRoot, path.join(dir, file), stats |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment