@@ -3,55 +3,68 @@ const path = require('path')
33
44const 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
5569fs . writeFileSync ( path . join ( ROOT , 'README.md' ) , content )
56-
5770console . log ( 'README.md updated!' )
0 commit comments