Created
March 27, 2017 01:57
-
-
Save kirpalmakanga/2971f9169d071b908af962fe93e3b8dd to your computer and use it in GitHub Desktop.
Node.js recursive directory walker (SYNC)
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
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