@hypernym/utils 3.2.1 → 3.3.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 +11 -1
- package/dist/fs/index.mjs +7 -2
- package/dist/types/fs/index.d.ts +37 -1
- package/package.json +2 -2
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'
|
|
@@ -127,6 +127,16 @@ 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
|
+
|
|
130
140
|
### write
|
|
131
141
|
|
|
132
142
|
Writes data to a file recursively.
|
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, 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,11 @@ 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
|
+
|
|
10
15
|
async function write(path, data, options) {
|
|
11
16
|
await mkdir$1(dirname(isURL(path) ? fileURLToPath(path) : path), {
|
|
12
17
|
recursive: true
|
|
@@ -42,4 +47,4 @@ async function remove(path, options = {}) {
|
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
49
|
|
|
45
|
-
export { copy, exists, mkdir, remove, write, writeFile };
|
|
50
|
+
export { copy, exists, mkdir, read, remove, write, writeFile };
|
package/dist/types/fs/index.d.ts
CHANGED
|
@@ -13,6 +13,42 @@ import { writeFile as writeFile$1 } from 'node:fs/promises';
|
|
|
13
13
|
*/
|
|
14
14
|
declare function exists(path: string): Promise<boolean>;
|
|
15
15
|
|
|
16
|
+
type ReadPath = string | URL;
|
|
17
|
+
type ReadEncodingType = BufferEncoding | null | undefined;
|
|
18
|
+
type ReadType<T> = T extends BufferEncoding | undefined ? string : T extends null ? Buffer : never;
|
|
19
|
+
interface ReadOptions<T extends ReadEncodingType> {
|
|
20
|
+
/**
|
|
21
|
+
* If no encoding is specified, the data is returned as a `Buffer` object. Otherwise, the data will be a string.
|
|
22
|
+
*
|
|
23
|
+
* @default 'utf-8'
|
|
24
|
+
*/
|
|
25
|
+
encoding?: T;
|
|
26
|
+
/**
|
|
27
|
+
* When provided the corresponding `AbortController` can be used to cancel an asynchronous action.
|
|
28
|
+
*
|
|
29
|
+
* @default undefined
|
|
30
|
+
*/
|
|
31
|
+
signal?: AbortSignal;
|
|
32
|
+
/**
|
|
33
|
+
* If a flag is not provided, it defaults to `'r'`.
|
|
34
|
+
*
|
|
35
|
+
* @default 'r'
|
|
36
|
+
*/
|
|
37
|
+
flag?: number | string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Reads the entire contents of a file.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { read } from '@hypernym/utils/fs'
|
|
46
|
+
*
|
|
47
|
+
* await read('dir/subdir/file.ts')
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare function read<T extends ReadEncodingType = BufferEncoding>(path: ReadPath, options?: ReadOptions<T>): Promise<ReadType<T>>;
|
|
51
|
+
|
|
16
52
|
type WritePath = string | URL;
|
|
17
53
|
/**
|
|
18
54
|
* Writes data to a file recursively.
|
|
@@ -146,4 +182,4 @@ interface RemoveOptions {
|
|
|
146
182
|
*/
|
|
147
183
|
declare function remove(path: RemovePath | RemovePath[], options?: RemoveOptions): Promise<void>;
|
|
148
184
|
|
|
149
|
-
export { type CopyDestination, type CopyOptions, type CopySource, type MakeDirOptions, type MakeDirPath, type RemoveOptions, type RemovePath, type WritePath, copy, exists, mkdir, remove, write, writeFile };
|
|
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 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hypernym/utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"author": "Hypernym Studio",
|
|
5
5
|
"description": "A collection of reusable utilities.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -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.
|
|
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",
|