|
| 1 | +import path from 'path' |
| 2 | +import fs from 'fs' |
| 3 | +import { globby } from 'globby' |
| 4 | + |
| 5 | +const URL_BASE = 'https://github.com/TheAlgorithms/Javascript/blob/master' |
| 6 | + |
| 7 | +function pathPrefix (i) { |
| 8 | + if (i) { |
| 9 | + const res = ' '.repeat(i) |
| 10 | + return res + '*' |
| 11 | + } else { |
| 12 | + return '\n##' |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +function printPath (oldPath, newPath, output) { |
| 17 | + const oldParts = oldPath.split(path.sep) |
| 18 | + const newParts = newPath.split(path.sep) |
| 19 | + for (let i = 0; i < newParts.length; ++i) { |
| 20 | + const newPart = newParts[i] |
| 21 | + if (i + 1 > oldParts.length || oldParts[i] !== newPart) { |
| 22 | + if (newPart) { |
| 23 | + output.push(`${pathPrefix(i)} ${newPart.replace('_', ' ')}`) |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + return newPath |
| 28 | +} |
| 29 | + |
| 30 | +function pathsToMarkdown (filePaths) { |
| 31 | + const output = [] |
| 32 | + |
| 33 | + let oldPath = '' |
| 34 | + filePaths.sort(function (a, b) { |
| 35 | + if (a.toLowerCase() < b.toLowerCase()) return -1 |
| 36 | + if (a.toLowerCase() > b.toLowerCase()) return 1 |
| 37 | + return 0 |
| 38 | + }) |
| 39 | + for (let filepath of filePaths) { |
| 40 | + const file = filepath.split(path.sep) |
| 41 | + let filename = '' |
| 42 | + if (file.length === 1) { |
| 43 | + filepath = '' |
| 44 | + filename = file[0] |
| 45 | + } else { |
| 46 | + const total = file.length |
| 47 | + filename = file[total - 1] |
| 48 | + filepath = file.splice(0, total - 1).join(path.sep) |
| 49 | + } |
| 50 | + if (filepath !== oldPath) { |
| 51 | + oldPath = printPath(oldPath, filepath, output) |
| 52 | + } |
| 53 | + let indent = 0 |
| 54 | + for (let i = 0; i < filepath.length; ++i) { |
| 55 | + if (filepath[i] === path.sep) { |
| 56 | + ++indent |
| 57 | + } |
| 58 | + } |
| 59 | + if (filepath) { |
| 60 | + ++indent |
| 61 | + } |
| 62 | + const urls = [URL_BASE, filepath, filename] |
| 63 | + const url = urls.join('/').replace(' ', '%20') |
| 64 | + // remove extension from filename |
| 65 | + filename = filename.split('.')[0] |
| 66 | + output.push(`${pathPrefix(indent)} [${filename}](${url})`) |
| 67 | + } |
| 68 | + |
| 69 | + return output.join('\n') |
| 70 | +} |
| 71 | + |
| 72 | +// get paths of all .js files - excluding node_modules, the .github folder, tests and config stuff |
| 73 | +globby(['**/*.js', '!(node_modules|.github)/**/*', '!**/*.test.js', '!babel.config.js']) |
| 74 | + // create markdown content |
| 75 | + .then(pathsToMarkdown) |
| 76 | + // write markdown to file |
| 77 | + .then(markdown => fs.writeFileSync('DIRECTORY.md', markdown + '\n', { encoding: 'utf8' })) |
0 commit comments