Skip to content

Commit 99fc548

Browse files
authored
feat: add new fs readdir util (#21)
1 parent 3d75d3c commit 99fc548

File tree

6 files changed

+82
-6
lines changed

6 files changed

+82
-6
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ isString('@hypernym/utils') // => true
119119

120120
### exists
121121

122-
Checks if the file or directory exists.
122+
Checks if the `file` or `directory` exists.
123123

124124
```ts
125125
import { exists } from '@hypernym/utils/fs'
@@ -129,17 +129,27 @@ await exists('dir/file.ts') // => true
129129

130130
### read
131131

132-
Reads the entire contents of a file.
132+
Reads the entire contents of a `file`.
133133

134134
```ts
135135
import { read } from '@hypernym/utils/fs'
136136

137137
await read('dir/subdir/file.ts')
138138
```
139139

140+
### readdir
141+
142+
Reads the contents of a `directory` recursively.
143+
144+
```ts
145+
import { readdir } from '@hypernym/utils/fs'
146+
147+
await readdir('dir/subdir')
148+
```
149+
140150
### write
141151

142-
Writes data to a file recursively.
152+
Writes data to a `file` recursively.
143153

144154
```ts
145155
import { write } from '@hypernym/utils/fs'

src/fs/exists.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { access, constants } from 'node:fs/promises'
22

33
/**
4-
* Checks if the file or directory exists.
4+
* Checks if the `file` or `directory` exists.
55
*
66
* @example
77
*

src/fs/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './exists'
22
export * from './read'
3+
export * from './readdir'
34
export * from './write'
45
export * from './copy'
56
export * from './mkdir'

src/fs/read.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface ReadOptions<T extends ReadEncodingType> {
3030
}
3131

3232
/**
33-
* Reads the entire contents of a file.
33+
* Reads the entire contents of a `file`.
3434
*
3535
* @example
3636
*

src/fs/readdir.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { readdir as readDir } from 'node:fs/promises'
2+
import type { Dirent } from 'node:fs'
3+
4+
export type ReaddirPath = string | URL
5+
export type ReaddirEncodingType = BufferEncoding | null | 'buffer' | undefined
6+
export type ReaddirWithFileType = true | undefined
7+
8+
export type ReaddirType<E, F> = F extends true
9+
? Dirent
10+
: E extends BufferEncoding | undefined
11+
? string
12+
: E extends null | 'buffer'
13+
? Buffer
14+
: never
15+
16+
export interface ReaddirOptions<
17+
E extends ReaddirEncodingType,
18+
F extends ReaddirWithFileType,
19+
> {
20+
/**
21+
* If the encoding is set to `'buffer'` or `null`, the filenames returned will be passed as `Buffer` objects.
22+
*
23+
* @default 'utf-8'
24+
*/
25+
encoding?: E
26+
/**
27+
* If `withFileTypes` is set to `true`, the returned array will contain `fs.Dirent` objects.
28+
*
29+
* @default undefined
30+
*/
31+
withFileTypes?: F
32+
/**
33+
* Reads the directory recursively.
34+
*
35+
* @default true
36+
*/
37+
recursive?: boolean
38+
}
39+
40+
/**
41+
* Reads the contents of a `directory` recursively.
42+
*
43+
* @example
44+
*
45+
* ```ts
46+
* import { readdir } from '@hypernym/utils/fs'
47+
*
48+
* await readdir('dir/subdir')
49+
* ```
50+
*/
51+
export async function readdir<
52+
E extends ReaddirEncodingType = BufferEncoding,
53+
F extends ReaddirWithFileType = undefined,
54+
>(
55+
path: ReaddirPath,
56+
options: ReaddirOptions<E, F> = {},
57+
): Promise<ReaddirType<E, F>[]> {
58+
const { encoding = 'utf-8', recursive = true } = options
59+
60+
return (await readDir(path, {
61+
encoding,
62+
recursive,
63+
...options,
64+
} as any)) as ReaddirType<E, F>[]
65+
}

src/fs/write.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { isURL } from '@'
66
export type WritePath = string | URL
77

88
/**
9-
* Writes data to a file recursively.
9+
* Writes data to a `file` recursively.
1010
*
1111
* @example
1212
*

0 commit comments

Comments
 (0)