@hypernym/utils 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -119,7 +119,7 @@ isString('@hypernym/utils') // => true
119
119
 
120
120
  ### exists
121
121
 
122
- Checks if the file or directory exists.
122
+ Checks if the `file` or `directory` exists.
123
123
 
124
124
  ```ts
125
125
  import { exists } from '@hypernym/utils/fs'
@@ -129,7 +129,7 @@ await exists('dir/file.ts') // => true
129
129
 
130
130
  ### read
131
131
 
132
- Reads the entire contents of a file.
132
+ Reads the entire contents of a `file`.
133
133
 
134
134
  ```ts
135
135
  import { read } from '@hypernym/utils/fs'
@@ -137,9 +137,19 @@ import { read } from '@hypernym/utils/fs'
137
137
  await read('dir/subdir/file.ts')
138
138
  ```
139
139
 
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
+
140
150
  ### write
141
151
 
142
- Writes data to a file recursively.
152
+ Writes data to a `file` recursively.
143
153
 
144
154
  ```ts
145
155
  import { write } from '@hypernym/utils/fs'
package/dist/fs/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { access, constants, readFile, mkdir as mkdir$1, writeFile as writeFile$1, cp, rm } from 'node:fs/promises';
1
+ import { access, constants, readFile, readdir as readdir$1, mkdir as mkdir$1, writeFile as writeFile$1, cp, rm } from 'node:fs/promises';
2
2
  import { dirname } from 'node:path';
3
3
  import { fileURLToPath } from 'node:url';
4
4
  import { isURL, isString } from '../index.mjs';
@@ -12,6 +12,15 @@ async function read(path, options = {}) {
12
12
  return await readFile(path, { encoding, ...options });
13
13
  }
14
14
 
15
+ async function readdir(path, options = {}) {
16
+ const { encoding = "utf-8", recursive = true } = options;
17
+ return await readdir$1(path, {
18
+ encoding,
19
+ recursive,
20
+ ...options
21
+ });
22
+ }
23
+
15
24
  async function write(path, data, options) {
16
25
  await mkdir$1(dirname(isURL(path) ? fileURLToPath(path) : path), {
17
26
  recursive: true
@@ -47,4 +56,4 @@ async function remove(path, options = {}) {
47
56
  }
48
57
  }
49
58
 
50
- export { copy, exists, mkdir, read, remove, write, writeFile };
59
+ export { copy, exists, mkdir, read, readdir, remove, write, writeFile };
@@ -1,7 +1,8 @@
1
+ import { Dirent } from 'node:fs';
1
2
  import { writeFile as writeFile$1 } from 'node:fs/promises';
2
3
 
3
4
  /**
4
- * Checks if the file or directory exists.
5
+ * Checks if the `file` or `directory` exists.
5
6
  *
6
7
  * @example
7
8
  *
@@ -37,7 +38,7 @@ interface ReadOptions<T extends ReadEncodingType> {
37
38
  flag?: number | string;
38
39
  }
39
40
  /**
40
- * Reads the entire contents of a file.
41
+ * Reads the entire contents of a `file`.
41
42
  *
42
43
  * @example
43
44
  *
@@ -49,9 +50,46 @@ interface ReadOptions<T extends ReadEncodingType> {
49
50
  */
50
51
  declare function read<T extends ReadEncodingType = BufferEncoding>(path: ReadPath, options?: ReadOptions<T>): Promise<ReadType<T>>;
51
52
 
53
+ type ReaddirPath = string | URL;
54
+ type ReaddirEncodingType = BufferEncoding | null | 'buffer' | undefined;
55
+ type ReaddirWithFileType = true | undefined;
56
+ type ReaddirType<E, F> = F extends true ? Dirent : E extends BufferEncoding | undefined ? string : E extends null | 'buffer' ? Buffer : never;
57
+ interface ReaddirOptions<E extends ReaddirEncodingType, F extends ReaddirWithFileType> {
58
+ /**
59
+ * If the encoding is set to `'buffer'` or `null`, the filenames returned will be passed as `Buffer` objects.
60
+ *
61
+ * @default 'utf-8'
62
+ */
63
+ encoding?: E;
64
+ /**
65
+ * If `withFileTypes` is set to `true`, the returned array will contain `fs.Dirent` objects.
66
+ *
67
+ * @default undefined
68
+ */
69
+ withFileTypes?: F;
70
+ /**
71
+ * Reads the directory recursively.
72
+ *
73
+ * @default true
74
+ */
75
+ recursive?: boolean;
76
+ }
77
+ /**
78
+ * Reads the contents of a `directory` recursively.
79
+ *
80
+ * @example
81
+ *
82
+ * ```ts
83
+ * import { readdir } from '@hypernym/utils/fs'
84
+ *
85
+ * await readdir('dir/subdir')
86
+ * ```
87
+ */
88
+ declare function readdir<E extends ReaddirEncodingType = BufferEncoding, F extends ReaddirWithFileType = undefined>(path: ReaddirPath, options?: ReaddirOptions<E, F>): Promise<ReaddirType<E, F>[]>;
89
+
52
90
  type WritePath = string | URL;
53
91
  /**
54
- * Writes data to a file recursively.
92
+ * Writes data to a `file` recursively.
55
93
  *
56
94
  * @example
57
95
  *
@@ -182,4 +220,4 @@ interface RemoveOptions {
182
220
  */
183
221
  declare function remove(path: RemovePath | RemovePath[], options?: RemoveOptions): Promise<void>;
184
222
 
185
- export { type CopyDestination, type CopyOptions, type CopySource, type MakeDirOptions, type MakeDirPath, type ReadEncodingType, type ReadOptions, type ReadPath, type ReadType, type RemoveOptions, type RemovePath, type WritePath, copy, exists, mkdir, read, remove, write, writeFile };
223
+ export { type CopyDestination, type CopyOptions, type CopySource, type MakeDirOptions, type MakeDirPath, type ReadEncodingType, type ReadOptions, type ReadPath, type ReadType, type ReaddirEncodingType, type ReaddirOptions, type ReaddirPath, type ReaddirType, type ReaddirWithFileType, type RemoveOptions, type RemovePath, type WritePath, copy, exists, mkdir, read, readdir, remove, write, writeFile };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypernym/utils",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "author": "Hypernym Studio",
5
5
  "description": "A collection of reusable utilities.",
6
6
  "license": "MIT",
@@ -45,7 +45,7 @@
45
45
  "prepublishOnly": "npm run build"
46
46
  },
47
47
  "sideEffects": false,
48
- "packageManager": "pnpm@9.10.0",
48
+ "packageManager": "pnpm@9.11.0",
49
49
  "engines": {
50
50
  "node": ">=20.0.0",
51
51
  "pnpm": ">=9.0.0"