Skip to content

Commit e744151

Browse files
fixed readme generator
1 parent 23e2943 commit e744151

File tree

2 files changed

+61
-59
lines changed

2 files changed

+61
-59
lines changed

README.md

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,29 @@
1-
# Code Snippets
2-
### Bcz I care about my fellow developers' precious time (◔◡◔)
1+
# Code Snippets
2+
### Because I care about my fellow developers' precious time (◔◡◔)
33

4+
Actually it's for my personal needs ✪ ω ✪
5+
But yes, you all are welcome anytime :D
46

5-
actually its for my personal needs ✪ ω ✪
6-
##
7-
but yes you guys are also welcomed anytime :D
8-
9-
10-
# 📚 DSA Snippets Repository
7+
---
118

12-
Automatically generated file index.
9+
# 📚 DSA Snippets Repository
10+
Auto-generated file index.
1311

1412
---
1513

14+
[cheetsheet.md](cheetsheet.md)
15+
1616
- **Algorithms/**
17-
- [cycleDetectionDirected.js](Algorithms/cycleDetectionDirected.js)
18-
- [dijkstra.js](Algorithms/dijkstra.js)
19-
- [kadane.js](Algorithms/kadane.js)
20-
- [topoSort.js](Algorithms/topoSort.js)
21-
- [cheetsheet.md](cheetsheet.md)
17+
[cycleDetectionDirected.js](Algorithms/cycleDetectionDirected.js), [dijkstra.js](Algorithms/dijkstra.js), [kadane.js](Algorithms/kadane.js), [topoSort.js](Algorithms/topoSort.js)
2218
- **Data Structures/**
2319
- **graph/**
24-
- [trie.js](Data%20Structures/graph/trie.js)
25-
- [unionFind.js](Data%20Structures/graph/unionFind.js)
20+
[trie.js](Data%20Structures/graph/trie.js), [unionFind.js](Data%20Structures/graph/unionFind.js)
2621
- **heap/**
27-
- [minHeap.js](Data%20Structures/heap/minHeap.js)
22+
[minHeap.js](Data%20Structures/heap/minHeap.js)
2823
- **stack/**
29-
- [stack.js](Data%20Structures/stack/stack.js)
24+
[stack.js](Data%20Structures/stack/stack.js)
3025
- **DSA patterns/**
31-
- [binarySearchOnAnswer.js](DSA%20patterns/binarySearchOnAnswer.js)
32-
- [monotonicStack.js](DSA%20patterns/monotonicStack.js)
33-
- [prefixSumPattern.js](DSA%20patterns/prefixSumPattern.js)
34-
- [twoPointers.js](DSA%20patterns/twoPointers.js)
35-
- [README.md](README.md)
26+
[binarySearchOnAnswer.js](DSA%20patterns/binarySearchOnAnswer.js), [monotonicStack.js](DSA%20patterns/monotonicStack.js), [prefixSumPattern.js](DSA%20patterns/prefixSumPattern.js), [twoPointers.js](DSA%20patterns/twoPointers.js)
3627
- **templates/**
37-
- [bfs.js](templates/bfs.js)
38-
- [binarySearch.js](templates/binarySearch.js)
39-
- [dfs.js](templates/dfs.js)
28+
[bfs.js](templates/bfs.js), [binarySearch.js](templates/binarySearch.js), [dfs.js](templates/dfs.js)
4029

generateReadme.js

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,68 @@ const path = require('path')
33

44
const ROOT = __dirname
55

6-
const IGNORE = new Set([
7-
'.git',
8-
'.gitignore',
9-
'node_modules',
10-
'.DS_Store',
11-
'.github',
12-
'generateReadme.js',
13-
])
14-
15-
function generateTree(dir, prefix = '') {
6+
const IGNORE = new Set(['.git', '.gitignore', 'node_modules', '.DS_Store', '.github', 'generateReadme.js', 'README.md'])
7+
8+
function makeLinks(dir, files) {
9+
return files
10+
.map((file) => {
11+
const fullPath = path.join(dir, file.name)
12+
let relPath = path.relative(ROOT, fullPath).replace(/\\/g, '/')
13+
relPath = relPath.split(' ').join('%20') // encode spaces
14+
return `[${file.name}](${relPath})`
15+
})
16+
.join(', ')
17+
}
18+
19+
function generateTree(dir, depth = 0) {
1620
const items = fs.readdirSync(dir, { withFileTypes: true })
1721

18-
let out = ''
22+
const folders = []
23+
const files = []
1924

2025
for (const item of items) {
21-
if (IGNORE.has(item.name)) continue;
22-
const fullPath = path.join(dir, item.name)
23-
let relPath = path.relative(ROOT, fullPath).replace(/\\/g, '/')
24-
25-
relPath = relPath.split(' ').join('%20')
26-
27-
if (item.isDirectory()) {
28-
out += `${prefix}- **${item.name}/**\n`
29-
out += generateTree(fullPath, prefix + ' ')
30-
} else {
31-
out += `${prefix}- [${item.name}](${relPath})\n`
32-
}
26+
if (IGNORE.has(item.name)) continue
27+
if (item.isDirectory()) folders.push(item)
28+
else files.push(item)
29+
}
30+
31+
let out = ''
32+
33+
if (depth === 0 && files.length > 0) {
34+
out += makeLinks(dir, files) + '\n\n'
35+
}
36+
37+
for (const folder of folders) {
38+
const indent = ' '.repeat(depth)
39+
out += `${indent}- **${folder.name}/**\n`
40+
41+
const fullPath = path.join(dir, folder.name)
42+
out += generateTree(fullPath, depth + 1)
3343
}
34-
return out
35-
}
3644

37-
const content = `# Code Snippets
38-
### Bcz I care about my fellow developers' precious time (◔◡◔)
45+
if (depth > 0 && files.length > 0) {
46+
const indent = ' '.repeat(depth)
47+
out += `${indent}${makeLinks(dir, files)}\n`
48+
}
3949

50+
return out
51+
}
4052

41-
actually its for my personal needs ✪ ω ✪
42-
##
43-
but yes you guys are also welcomed anytime :D
53+
const content = `# Code Snippets
54+
### Because I care about my fellow developers' precious time (◔◡◔)
4455
56+
Actually it's for my personal needs ✪ ω ✪
57+
But yes, you all are welcome anytime :D
4558
46-
# 📚 DSA Snippets Repository
59+
---
4760
48-
Automatically generated file index.
61+
# 📚 DSA Snippets Repository
62+
Auto-generated file index.
4963
5064
---
5165
5266
${generateTree(ROOT)}
5367
`
5468

5569
fs.writeFileSync(path.join(ROOT, 'README.md'), content)
56-
5770
console.log('README.md updated!')

0 commit comments

Comments
 (0)