Skip to content

Commit a69d9a8

Browse files
committed
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.
1 parent 460fd57 commit a69d9a8

File tree

5 files changed

+208
-109
lines changed

5 files changed

+208
-109
lines changed

.github/workflows/UpdateDirectory.js

-108
This file was deleted.

.github/workflows/UpdateDirectory.mjs

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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' }))

.github/workflows/update_directory_md.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
node-version: '14'
1414

1515
- name: 🗄️ Create Directory from JS files
16-
run: node .github/workflows/UpdateDirectory.js
16+
run: node .github/workflows/UpdateDirectory.mjs
1717

1818
- name: 🤓 Commit & push new Directory (if needed)
1919
run: |

package-lock.json

+129
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"devDependencies": {
2828
"babel-jest": "^26.3.0",
2929
"doctest": "^0.17.1",
30+
"globby": "^12.0.2",
3031
"jest": "^26.4.2",
3132
"standard": "^14.3.4"
3233
}

0 commit comments

Comments
 (0)