Created
June 19, 2017 07:02
-
-
Save faforty/6040308c1b124e06e8d1c931b067ba0d 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
'use strict' | |
const fs = require('fs') | |
const path = require('path') | |
const rootFolder = __dirname // Your folder | |
const spider = (folder = rootFolder, state = []) => { | |
fs.readdirSync(folder).forEach(file => { | |
let dir = path.join(folder, file), | |
isDir = fs.lstatSync(dir).isDirectory() | |
let children = isDir ? spider(dir) : null | |
state.push({ | |
file: file, | |
isDir: isDir, | |
chidlren: children | |
}) | |
}) | |
state.sort(a => a.isDir ? -1 : 1) | |
return state | |
} | |
/* | |
Индексируем все папки | |
*/ | |
const tree = spider() | |
console.log(tree) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment