|
| 1 | +// requiring path and fs modules |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | + |
| 5 | +let URL_BASE = "https://github.com/TheAlgorithms/Javascript/blob/master"; |
| 6 | +let g_output = []; |
| 7 | + |
| 8 | +let filepaths = []; |
| 9 | +function good_filepaths(top_dir = ".") { |
| 10 | + fs.readdir(top_dir, function(err, list) { |
| 11 | + if (err) { |
| 12 | + console.log(err); |
| 13 | + return; |
| 14 | + } |
| 15 | + list.forEach(function(file) { |
| 16 | + let path = top_dir + "/" + file; |
| 17 | + if (!file.startsWith(".")) { |
| 18 | + fs.stat(path, function(err, stat) { |
| 19 | + if (stat && stat.isDirectory()) { |
| 20 | + good_filepaths(path); |
| 21 | + } else { |
| 22 | + if (file.toLowerCase().endsWith(".js")) { |
| 23 | + filepaths.push(path.slice(2)); |
| 24 | + } |
| 25 | + } |
| 26 | + }); |
| 27 | + } |
| 28 | + }); |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +function md_prefix(i) { |
| 33 | + if (i) { |
| 34 | + let res = ' '.repeat(i); |
| 35 | + return res + "*"; |
| 36 | + } else { |
| 37 | + return "\n##" |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function print_path(old_path, new_path) { |
| 42 | + let old_parts = old_path.split(path.sep); |
| 43 | + let new_parts = new_path.split(path.sep); |
| 44 | + for (let i = 0; i < new_parts.length; ++i) { |
| 45 | + let new_part = new_parts[i]; |
| 46 | + if (i + 1 > old_parts.len || old_parts[i] != new_part) { |
| 47 | + if (new_part) { |
| 48 | + g_output.push(`${md_prefix(i)} ${new_part.replace('_', ' ')}`); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + return new_path; |
| 53 | +} |
| 54 | + |
| 55 | +function build_directory_md(top_dir = ".") { |
| 56 | + old_path = ""; |
| 57 | + filepaths.sort(function(a, b) { |
| 58 | + if (a.toLowerCase() < b.toLowerCase()) return -1; |
| 59 | + if (a.toLowerCase() > b.toLowerCase()) return 1; |
| 60 | + return 0; |
| 61 | + }); |
| 62 | + for (let filepath of filepaths) { |
| 63 | + file = filepath.split(path.sep); |
| 64 | + if (file.length == 1) { |
| 65 | + filepath = ""; |
| 66 | + filename = file[0]; |
| 67 | + } else { |
| 68 | + let total = file.length; |
| 69 | + filename = file[total - 1]; |
| 70 | + filepath = file.splice(0, total - 1).join(path.sep); |
| 71 | + } |
| 72 | + if (filepath != old_path) { |
| 73 | + old_path = print_path(old_path, filepath); |
| 74 | + } |
| 75 | + let indent = 0; |
| 76 | + for (let i = 0; i < filepath.length; ++i) { |
| 77 | + if (filepath[i] == path.sep) { |
| 78 | + ++indent; |
| 79 | + } |
| 80 | + } |
| 81 | + if (filepath) { |
| 82 | + ++indent; |
| 83 | + } |
| 84 | + let urls = [URL_BASE, filepath, filename]; |
| 85 | + let url = urls.join("/").replace(" ", "%20"); |
| 86 | + // remove extension from filename |
| 87 | + filename = filename.split(".")[0]; |
| 88 | + g_output.push(`${md_prefix(indent)} [${filename}](${url})`); |
| 89 | + } |
| 90 | + g_output = g_output.join('\n'); |
| 91 | + return g_output; |
| 92 | +} |
| 93 | + |
| 94 | +good_filepaths(); |
| 95 | +setTimeout(() => { |
| 96 | + // once the filepaths have been computed |
| 97 | + build_directory_md(); |
| 98 | + // console.log(filepaths); |
| 99 | +}, 1000); |
| 100 | +setTimeout(() => { |
| 101 | + // once the g_output has been constructed, write to the file |
| 102 | + fs.writeFile('DIRECTORY.md', g_output + '\n', (err) => { |
| 103 | + if (err) { |
| 104 | + console.log(err); |
| 105 | + } |
| 106 | + }) |
| 107 | + // console.log(g_output); |
| 108 | +}, 1000); |
0 commit comments