Skip to content

Instantly share code, notes, and snippets.

@kirpalmakanga
Created March 27, 2017 01:57
Show Gist options
  • Save kirpalmakanga/2971f9169d071b908af962fe93e3b8dd to your computer and use it in GitHub Desktop.
Save kirpalmakanga/2971f9169d071b908af962fe93e3b8dd to your computer and use it in GitHub Desktop.
Node.js recursive directory walker (SYNC)
function walk(dirPath) {
const stats = fs.lstatSync(dirPath)
let type = mime.lookup(dirPath)
var data = []
if(stats.isFile()) {
data.push({
path: dirPath.replace(/\\/gi, '/'),
name: path.basename(dirPath),
type
})
}
if (stats.isDirectory()) {
fs.readdirSync(dirPath).map(child => {
data.push(...walk(path.join(dirPath, child)))
})
}
return data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment