Clean up and speed up UpdateDirectory.js

Uses globby to find all algorithm files. This is way quicker and less error-prone than the previous implementation. Plus, it allows us to fine-tune excludes (e.g. test files and the babel config).

This also removes the need for setTimeouts which greatly speeds up the whole thing.
This commit is contained in:
Roland Hummel
2021-10-04 00:24:40 +02:00
parent 460fd57c6b
commit a69d9a8c49
5 changed files with 208 additions and 109 deletions

View File

@ -1,108 +0,0 @@
// requiring path and fs modules
const path = require('path');
const fs = require('fs');
let URL_BASE = "https://github.com/TheAlgorithms/Javascript/blob/master";
let g_output = [];
let filepaths = [];
function good_filepaths(top_dir = ".") {
fs.readdir(top_dir, function(err, list) {
if (err) {
console.log(err);
return;
}
list.forEach(function(file) {
let path = top_dir + "/" + file;
if (!file.startsWith(".")) {
fs.stat(path, function(err, stat) {
if (stat && stat.isDirectory()) {
good_filepaths(path);
} else {
if (file.toLowerCase().endsWith(".js")) {
filepaths.push(path.slice(2));
}
}
});
}
});
})
}
function md_prefix(i) {
if (i) {
let res = ' '.repeat(i);
return res + "*";
} else {
return "\n##"
}
}
function print_path(old_path, new_path) {
let old_parts = old_path.split(path.sep);
let new_parts = new_path.split(path.sep);
for (let i = 0; i < new_parts.length; ++i) {
let new_part = new_parts[i];
if (i + 1 > old_parts.len || old_parts[i] != new_part) {
if (new_part) {
g_output.push(`${md_prefix(i)} ${new_part.replace('_', ' ')}`);
}
}
}
return new_path;
}
function build_directory_md(top_dir = ".") {
old_path = "";
filepaths.sort(function(a, b) {
if (a.toLowerCase() < b.toLowerCase()) return -1;
if (a.toLowerCase() > b.toLowerCase()) return 1;
return 0;
});
for (let filepath of filepaths) {
file = filepath.split(path.sep);
if (file.length == 1) {
filepath = "";
filename = file[0];
} else {
let total = file.length;
filename = file[total - 1];
filepath = file.splice(0, total - 1).join(path.sep);
}
if (filepath != old_path) {
old_path = print_path(old_path, filepath);
}
let indent = 0;
for (let i = 0; i < filepath.length; ++i) {
if (filepath[i] == path.sep) {
++indent;
}
}
if (filepath) {
++indent;
}
let urls = [URL_BASE, filepath, filename];
let url = urls.join("/").replace(" ", "%20");
// remove extension from filename
filename = filename.split(".")[0];
g_output.push(`${md_prefix(indent)} [${filename}](${url})`);
}
g_output = g_output.join('\n');
return g_output;
}
good_filepaths();
setTimeout(() => {
// once the filepaths have been computed
build_directory_md();
// console.log(filepaths);
}, 1000);
setTimeout(() => {
// once the g_output has been constructed, write to the file
fs.writeFile('DIRECTORY.md', g_output + '\n', (err) => {
if (err) {
console.log(err);
}
})
// console.log(g_output);
}, 1000);

77
.github/workflows/UpdateDirectory.mjs vendored Normal file
View File

@ -0,0 +1,77 @@
import path from 'path'
import fs from 'fs'
import { globby } from 'globby'
const URL_BASE = 'https://github.com/TheAlgorithms/Javascript/blob/master'
function pathPrefix (i) {
if (i) {
const res = ' '.repeat(i)
return res + '*'
} else {
return '\n##'
}
}
function printPath (oldPath, newPath, output) {
const oldParts = oldPath.split(path.sep)
const newParts = newPath.split(path.sep)
for (let i = 0; i < newParts.length; ++i) {
const newPart = newParts[i]
if (i + 1 > oldParts.length || oldParts[i] !== newPart) {
if (newPart) {
output.push(`${pathPrefix(i)} ${newPart.replace('_', ' ')}`)
}
}
}
return newPath
}
function pathsToMarkdown (filePaths) {
const output = []
let oldPath = ''
filePaths.sort(function (a, b) {
if (a.toLowerCase() < b.toLowerCase()) return -1
if (a.toLowerCase() > b.toLowerCase()) return 1
return 0
})
for (let filepath of filePaths) {
const file = filepath.split(path.sep)
let filename = ''
if (file.length === 1) {
filepath = ''
filename = file[0]
} else {
const total = file.length
filename = file[total - 1]
filepath = file.splice(0, total - 1).join(path.sep)
}
if (filepath !== oldPath) {
oldPath = printPath(oldPath, filepath, output)
}
let indent = 0
for (let i = 0; i < filepath.length; ++i) {
if (filepath[i] === path.sep) {
++indent
}
}
if (filepath) {
++indent
}
const urls = [URL_BASE, filepath, filename]
const url = urls.join('/').replace(' ', '%20')
// remove extension from filename
filename = filename.split('.')[0]
output.push(`${pathPrefix(indent)} [${filename}](${url})`)
}
return output.join('\n')
}
// get paths of all .js files - excluding node_modules, the .github folder, tests and config stuff
globby(['**/*.js', '!(node_modules|.github)/**/*', '!**/*.test.js', '!babel.config.js'])
// create markdown content
.then(pathsToMarkdown)
// write markdown to file
.then(markdown => fs.writeFileSync('DIRECTORY.md', markdown + '\n', { encoding: 'utf8' }))

View File

@ -13,7 +13,7 @@ jobs:
node-version: '14' node-version: '14'
- name: 🗄️ Create Directory from JS files - name: 🗄️ Create Directory from JS files
run: node .github/workflows/UpdateDirectory.js run: node .github/workflows/UpdateDirectory.mjs
- name: 🤓 Commit & push new Directory (if needed) - name: 🤓 Commit & push new Directory (if needed)
run: | run: |

129
package-lock.json generated
View File

@ -2173,6 +2173,32 @@
} }
} }
}, },
"@nodelib/fs.scandir": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
"integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==",
"dev": true,
"requires": {
"@nodelib/fs.stat": "2.0.5",
"run-parallel": "^1.1.9"
}
},
"@nodelib/fs.stat": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz",
"integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==",
"dev": true
},
"@nodelib/fs.walk": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz",
"integrity": "sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==",
"dev": true,
"requires": {
"@nodelib/fs.scandir": "2.1.5",
"fastq": "^1.6.0"
}
},
"@sinonjs/commons": { "@sinonjs/commons": {
"version": "1.8.1", "version": "1.8.1",
"resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.8.1.tgz", "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.8.1.tgz",
@ -2430,6 +2456,12 @@
"is-string": "^1.0.5" "is-string": "^1.0.5"
} }
}, },
"array-union": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/array-union/-/array-union-3.0.1.tgz",
"integrity": "sha512-1OvF9IbWwaeiM9VhzYXVQacMibxpXOMYVNIvMtKRyX9SImBXpKcFr8XvFDeEslCyuH/t6KRt7HEO94AlP8Iatw==",
"dev": true
},
"array-unique": { "array-unique": {
"version": "0.3.2", "version": "0.3.2",
"resolved": "https://registry.npmjs.org/array-unique/-/array-unique-0.3.2.tgz", "resolved": "https://registry.npmjs.org/array-unique/-/array-unique-0.3.2.tgz",
@ -3199,6 +3231,23 @@
"integrity": "sha512-ZXx86srb/iYy6jG71k++wBN9P9J05UNQ5hQHQd9MtMPvcqXPx/vKU69jfHV637D00Q2gSgPk2D+jSx3l1lDW/Q==", "integrity": "sha512-ZXx86srb/iYy6jG71k++wBN9P9J05UNQ5hQHQd9MtMPvcqXPx/vKU69jfHV637D00Q2gSgPk2D+jSx3l1lDW/Q==",
"dev": true "dev": true
}, },
"dir-glob": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/dir-glob/-/dir-glob-3.0.1.tgz",
"integrity": "sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA==",
"dev": true,
"requires": {
"path-type": "^4.0.0"
},
"dependencies": {
"path-type": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz",
"integrity": "sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==",
"dev": true
}
}
},
"doctest": { "doctest": {
"version": "0.17.1", "version": "0.17.1",
"resolved": "https://registry.npmjs.org/doctest/-/doctest-0.17.1.tgz", "resolved": "https://registry.npmjs.org/doctest/-/doctest-0.17.1.tgz",
@ -3909,6 +3958,37 @@
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="
}, },
"fast-glob": {
"version": "3.2.7",
"resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.7.tgz",
"integrity": "sha512-rYGMRwip6lUMvYD3BTScMwT1HtAs2d71SMv66Vrxs0IekGZEjhM0pcMfjQPnknBt2zeCwQMEupiN02ZP4DiT1Q==",
"dev": true,
"requires": {
"@nodelib/fs.stat": "^2.0.2",
"@nodelib/fs.walk": "^1.2.3",
"glob-parent": "^5.1.2",
"merge2": "^1.3.0",
"micromatch": "^4.0.4"
},
"dependencies": {
"micromatch": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.4.tgz",
"integrity": "sha512-pRmzw/XUcwXGpD9aI9q/0XOwLNygjETJ8y0ao0wdqprrzDa4YnxLcz7fQRZr8voh8V10kGhABbNcHVk5wHgWwg==",
"dev": true,
"requires": {
"braces": "^3.0.1",
"picomatch": "^2.2.3"
}
},
"picomatch": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.0.tgz",
"integrity": "sha512-lY1Q/PiJGC2zOv/z391WOTD+Z02bCgsFfvxoXXf6h7kv9o+WmsmzYqrAwY63sNgOxE4xEdq0WyUnXfKeBrSvYw==",
"dev": true
}
}
},
"fast-json-stable-stringify": { "fast-json-stable-stringify": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz",
@ -3919,6 +3999,15 @@
"resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz", "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz",
"integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=" "integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc="
}, },
"fastq": {
"version": "1.13.0",
"resolved": "https://registry.npmjs.org/fastq/-/fastq-1.13.0.tgz",
"integrity": "sha512-YpkpUnK8od0o1hmeSc7UUs/eB/vIPWJYjKck2QKIzAf71Vm1AAQ3EbuZB3g2JIy+pg+ERD0vqI79KyZiB2e2Nw==",
"dev": true,
"requires": {
"reusify": "^1.0.4"
}
},
"fb-watchman": { "fb-watchman": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/fb-watchman/-/fb-watchman-2.0.1.tgz", "resolved": "https://registry.npmjs.org/fb-watchman/-/fb-watchman-2.0.1.tgz",
@ -4119,6 +4208,34 @@
"type-fest": "^0.8.1" "type-fest": "^0.8.1"
} }
}, },
"globby": {
"version": "12.0.2",
"resolved": "https://registry.npmjs.org/globby/-/globby-12.0.2.tgz",
"integrity": "sha512-lAsmb/5Lww4r7MM9nCCliDZVIKbZTavrsunAsHLr9oHthrZP1qi7/gAnHOsUs9bLvEt2vKVJhHmxuL7QbDuPdQ==",
"dev": true,
"requires": {
"array-union": "^3.0.1",
"dir-glob": "^3.0.1",
"fast-glob": "^3.2.7",
"ignore": "^5.1.8",
"merge2": "^1.4.1",
"slash": "^4.0.0"
},
"dependencies": {
"ignore": {
"version": "5.1.8",
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.1.8.tgz",
"integrity": "sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==",
"dev": true
},
"slash": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-4.0.0.tgz",
"integrity": "sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew==",
"dev": true
}
}
},
"graceful-fs": { "graceful-fs": {
"version": "4.2.4", "version": "4.2.4",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.4.tgz", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.4.tgz",
@ -7245,6 +7362,12 @@
"integrity": "sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==", "integrity": "sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==",
"dev": true "dev": true
}, },
"merge2": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz",
"integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==",
"dev": true
},
"micromatch": { "micromatch": {
"version": "4.0.2", "version": "4.0.2",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.2.tgz", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.2.tgz",
@ -8278,6 +8401,12 @@
"integrity": "sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==", "integrity": "sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==",
"dev": true "dev": true
}, },
"reusify": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz",
"integrity": "sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==",
"dev": true
},
"rimraf": { "rimraf": {
"version": "2.6.3", "version": "2.6.3",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.3.tgz", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.3.tgz",

View File

@ -27,6 +27,7 @@
"devDependencies": { "devDependencies": {
"babel-jest": "^26.3.0", "babel-jest": "^26.3.0",
"doctest": "^0.17.1", "doctest": "^0.17.1",
"globby": "^12.0.2",
"jest": "^26.4.2", "jest": "^26.4.2",
"standard": "^14.3.4" "standard": "^14.3.4"
} }