Skip to content

Commit e610ca8

Browse files
committed
vaultService: list real files
1 parent c5eae42 commit e610ca8

File tree

7 files changed

+73
-6
lines changed

7 files changed

+73
-6
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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: '^_' }],

src/lib/fs/directory.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

src/lib/fs/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { getFilesAndDirectories } from './directory';

src/lib/fs/tests/directory.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
});

src/lib/fs/tests/directoryTest/a.txt

Whitespace-only changes.

src/lib/fs/tests/directoryTest/b/c.txt

Whitespace-only changes.
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getFilesAndDirectories } from '@/lib/fs';
2+
13
export 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
}

0 commit comments

Comments
 (0)