Guest User

Untitled

a guest
Jan 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. const fs = require('fs'),
  2. path = require('path')
  3. function spaceAhead(n) {
  4. let outputString = ''
  5. for (let i = 0; i < n; i++) {
  6. outputString += ' '
  7. }
  8. return outputString
  9. }
  10.  
  11. function analyze(rootDir, dirname, identation=0) {
  12. let outputString = ''
  13. const files = fs.readdirSync(dirname)
  14. let foundMarkdownFile = false
  15. files.forEach((fileName)=> {
  16. if (fileName.endsWith('.md')) { // is markdown file
  17. foundMarkdownFile = true
  18. outputString += spaceAhead(identation + 4) + '* [' + path.basename(fileName).replace(path.extname(fileName), '') + ']('+path.relative(rootDir, path.resolve(dirname, fileName)) + ')\n'
  19. } else if (fs.lstatSync(path.resolve(dirname, fileName)).isDirectory()) { // is directory
  20. const {out, found} = analyze(rootDir, path.resolve(dirname, fileName), identation + 4)
  21. foundMarkdownFile = foundMarkdownFile || found
  22. outputString += out
  23. }
  24. })
  25.  
  26. if (foundMarkdownFile) {
  27. outputString = spaceAhead(identation) + '* ' + path.basename(dirname) + '\n' + outputString
  28. }
  29.  
  30. return {
  31. out: outputString,
  32. found: foundMarkdownFile
  33. }
  34. }
  35.  
  36. const files = fs.readdirSync(__dirname)
  37. const outputString = files.map((fileName)=> {
  38. if (fs.lstatSync(path.resolve(__dirname, fileName)).isDirectory()) {
  39. return analyze(__dirname, path.resolve(__dirname, fileName), 0).out
  40. } else {
  41. return ''
  42. }
  43. }).join('')
  44.  
  45. console.log(outputString)
Add Comment
Please, Sign In to add comment