File tree Expand file tree Collapse file tree 7 files changed +73
-6
lines changed Expand file tree Collapse file tree 7 files changed +73
-6
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ module.exports = {
1212 'react/jsx-props-no-spreading' : 'off' ,
1313 'react/require-default-props' : 'off' ,
1414 'no-await-in-loop' : 'off' ,
15+ 'no-restricted-syntax' : 'off' ,
1516 // Allow unused vars if they are prefixed by an underscore
1617 '@typescript-eslint/no-unused-vars' : [ 'error' , { argsIgnorePattern : '^_' } ] ,
1718 'no-unused-vars' : [ 'error' , { argsIgnorePattern : '^_' } ] ,
Original file line number Diff line number Diff line change 1+ import fs from 'fs' ;
2+ import path from 'path' ;
3+
4+ interface Entry {
5+ name : string ;
6+ isDirectory : boolean ;
7+ }
8+
9+ /**
10+ * Lists all files and directories in a directory, recursively.
11+ * The base directory is omitted from the returned paths.
12+ */
13+ export async function getFilesAndDirectories (
14+ folderPath : string
15+ ) : Promise < Entry [ ] > {
16+ const dirEnts = await fs . promises . readdir ( folderPath , {
17+ withFileTypes : true ,
18+ } ) ;
19+
20+ const entries = dirEnts . map ( ( dirEnt ) => {
21+ return {
22+ name : dirEnt . name ,
23+ isDirectory : dirEnt . isDirectory ( ) ,
24+ } ;
25+ } ) ;
26+
27+ const folders = dirEnts . filter ( ( dirEnt ) => dirEnt . isDirectory ( ) ) ;
28+
29+ for ( const folder of folders ) {
30+ const folderEntries = await getFilesAndDirectories (
31+ path . join ( folderPath , folder . name )
32+ ) ;
33+ entries . push (
34+ ...folderEntries . map ( ( folderEntry ) => {
35+ return {
36+ name : path . join ( folder . name , folderEntry . name ) ,
37+ isDirectory : folderEntry . isDirectory ,
38+ } ;
39+ } )
40+ ) ;
41+ }
42+
43+ return entries ;
44+ }
Original file line number Diff line number Diff line change 1+ export { getFilesAndDirectories } from './directory' ;
Original file line number Diff line number Diff line change 1+ import path from 'path' ;
2+ import { getFilesAndDirectories } from '../directory' ;
3+
4+ test ( 'getFiles returns all files recursively' , async ( ) => {
5+ const files = await getFilesAndDirectories (
6+ path . join ( __dirname , 'directoryTest' )
7+ ) ;
8+ expect ( files ) . toEqual ( [
9+ {
10+ name : 'a.txt' ,
11+ isDirectory : false ,
12+ } ,
13+ {
14+ name : 'b' ,
15+ isDirectory : true ,
16+ } ,
17+ {
18+ name : 'b/c.txt' ,
19+ isDirectory : false ,
20+ } ,
21+ ] ) ;
22+ } ) ;
Original file line number Diff line number Diff line change 1+ import { getFilesAndDirectories } from '@/lib/fs' ;
2+
13export class Vault {
24 private readonly path : string ;
35
@@ -6,11 +8,8 @@ export class Vault {
68 }
79
810 async getFiles ( ) : Promise < string [ ] > {
9- return [
10- 'journals/day1.md' ,
11- 'pages/meetings/alex.md' ,
12- 'pages/meetings/fidji.md' ,
13- 'pages/how-to-take-nodes.md' ,
14- ] ;
11+ return ( await getFilesAndDirectories ( this . path ) )
12+ . filter ( ( e ) => ! e . isDirectory )
13+ . map ( ( e ) => e . name ) ;
1514 }
1615}
You can’t perform that action at this time.
0 commit comments