Skip to content

Instantly share code, notes, and snippets.

@synsa
Forked from tim-smart/walker.js
Created April 30, 2019 21:08
Show Gist options
  • Save synsa/ac783b65b958114c6917b7e07b4c202f to your computer and use it in GitHub Desktop.
Save synsa/ac783b65b958114c6917b7e07b4c202f to your computer and use it in GitHub Desktop.
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 then puts error.message
else files.forEach (file) ->
fs.stat path.join(dir, file), (error, stats) ->
return puts error.message if error
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