@hypernym/utils 3.2.1 → 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
@@ -77,7 +77,7 @@ Also, it is possible to download files manually and serve them accordingly.
77
77
 
78
78
  ### isBrowser
79
79
 
80
- Checks if the code is running in the browser.
80
+ Checks if the code is running in the `browser`.
81
81
 
82
82
  ```ts
83
83
  import { isBrowser } from '@hypernym/utils'
@@ -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'
@@ -127,9 +127,29 @@ import { exists } from '@hypernym/utils/fs'
127
127
  await exists('dir/file.ts') // => true
128
128
  ```
129
129
 
130
+ ### read
131
+
132
+ Reads the entire contents of a `file`.
133
+
134
+ ```ts
135
+ import { read } from '@hypernym/utils/fs'
136
+
137
+ await read('dir/subdir/file.ts')
138
+ ```
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
+
130
150
  ### write
131
151
 
132
- Writes data to a file recursively.
152
+ Writes data to a `file` recursively.
133
153
 
134
154
  ```ts
135
155
  import { write } from '@hypernym/utils/fs'
package/dist/fs/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { access, constants, 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';
@@ -7,6 +7,20 @@ async function exists(path) {
7
7
  return await access(path, constants.F_OK).then(() => true).catch(() => false);
8
8
  }
9
9
 
10
+ async function read(path, options = {}) {
11
+ const { encoding = "utf-8" } = options;
12
+ return await readFile(path, { encoding, ...options });
13
+ }
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
+
10
24
  async function write(path, data, options) {
11
25
  await mkdir$1(dirname(isURL(path) ? fileURLToPath(path) : path), {
12
26
  recursive: true
@@ -42,4 +56,4 @@ async function remove(path, options = {}) {
42
56
  }
43
57
  }
44
58
 
45
- export { copy, exists, mkdir, 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
  *
@@ -13,9 +14,82 @@ import { writeFile as writeFile$1 } from 'node:fs/promises';
13
14
  */
14
15
  declare function exists(path: string): Promise<boolean>;
15
16
 
17
+ type ReadPath = string | URL;
18
+ type ReadEncodingType = BufferEncoding | null | undefined;
19
+ type ReadType<T> = T extends BufferEncoding | undefined ? string : T extends null ? Buffer : never;
20
+ interface ReadOptions<T extends ReadEncodingType> {
21
+ /**
22
+ * If no encoding is specified, the data is returned as a `Buffer` object. Otherwise, the data will be a string.
23
+ *
24
+ * @default 'utf-8'
25
+ */
26
+ encoding?: T;
27
+ /**
28
+ * When provided the corresponding `AbortController` can be used to cancel an asynchronous action.
29
+ *
30
+ * @default undefined
31
+ */
32
+ signal?: AbortSignal;
33
+ /**
34
+ * If a flag is not provided, it defaults to `'r'`.
35
+ *
36
+ * @default 'r'
37
+ */
38
+ flag?: number | string;
39
+ }
40
+ /**
41
+ * Reads the entire contents of a `file`.
42
+ *
43
+ * @example
44
+ *
45
+ * ```ts
46
+ * import { read } from '@hypernym/utils/fs'
47
+ *
48
+ * await read('dir/subdir/file.ts')
49
+ * ```
50
+ */
51
+ declare function read<T extends ReadEncodingType = BufferEncoding>(path: ReadPath, options?: ReadOptions<T>): Promise<ReadType<T>>;
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
+
16
90
  type WritePath = string | URL;
17
91
  /**
18
- * Writes data to a file recursively.
92
+ * Writes data to a `file` recursively.
19
93
  *
20
94
  * @example
21
95
  *
@@ -146,4 +220,4 @@ interface RemoveOptions {
146
220
  */
147
221
  declare function remove(path: RemovePath | RemovePath[], options?: RemoveOptions): Promise<void>;
148
222
 
149
- export { type CopyDestination, type CopyOptions, type CopySource, type MakeDirOptions, type MakeDirPath, type RemoveOptions, type RemovePath, type WritePath, copy, exists, mkdir, 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.2.1",
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"
@@ -66,7 +66,7 @@
66
66
  "@hypernym/bundler": "^0.11.0",
67
67
  "@hypernym/eslint-config": "^3.5.0",
68
68
  "@hypernym/prettier-config": "^3.2.0",
69
- "@hypernym/tsconfig": "^2.3.0",
69
+ "@hypernym/tsconfig": "^2.4.0",
70
70
  "@types/node": "^22.5.5",
71
71
  "eslint": "^9.10.0",
72
72
  "prettier": "^3.3.3",