Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const fs = require('fs'),
path = require('path')
function spaceAhead(n) {
let outputString = ''
for (let i = 0; i < n; i++) {
outputString += ' '
}
return outputString
}
function analyze(rootDir, dirname, identation=0) {
let outputString = ''
const files = fs.readdirSync(dirname)
let foundMarkdownFile = false
files.forEach((fileName)=> {
if (fileName.endsWith('.md')) { // is markdown file
foundMarkdownFile = true
outputString += spaceAhead(identation + 4) + '* [' + path.basename(fileName).replace(path.extname(fileName), '') + ']('+path.relative(rootDir, path.resolve(dirname, fileName)) + ')\n'
} else if (fs.lstatSync(path.resolve(dirname, fileName)).isDirectory()) { // is directory
const {out, found} = analyze(rootDir, path.resolve(dirname, fileName), identation + 4)
foundMarkdownFile = foundMarkdownFile || found
outputString += out
}
})
if (foundMarkdownFile) {
outputString = spaceAhead(identation) + '* ' + path.basename(dirname) + '\n' + outputString
}
return {
out: outputString,
found: foundMarkdownFile
}
}
const files = fs.readdirSync(__dirname)
const outputString = files.map((fileName)=> {
if (fs.lstatSync(path.resolve(__dirname, fileName)).isDirectory()) {
return analyze(__dirname, path.resolve(__dirname, fileName), 0).out
} else {
return ''
}
}).join('')
console.log(outputString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment