Created
January 11, 2018 21:38
-
-
Save shd101wyy/cd21508dd886636a7bbbc67f69f17caa 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
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