@hypernym/utils 3.4.4 → 3.4.6
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/LICENSE.txt +2 -1
- package/README.md +371 -13
- package/dist/fs/{index.d.mts → index.d.ts} +115 -108
- package/dist/fs/index.js +161 -0
- package/dist/{index.d.mts → index.d.ts} +139 -66
- package/dist/index.iife.js +1 -1
- package/dist/index.js +437 -0
- package/dist/index.min.js +1 -0
- package/dist/index.umd.js +1 -1
- package/package.json +24 -25
- package/dist/fs/index.mjs +0 -59
- package/dist/index.min.mjs +0 -1
- package/dist/index.mjs +0 -40
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { Dirent } from
|
|
2
|
-
import { writeFile as writeFile$1 } from
|
|
1
|
+
import { Dirent } from "node:fs";
|
|
2
|
+
import { writeFile as writeFile$1 } from "node:fs/promises";
|
|
3
|
+
|
|
4
|
+
//#region src/fs/exists.d.ts
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Checks if the `file` or `directory` exists.
|
|
@@ -9,33 +11,34 @@ import { writeFile as writeFile$1 } from 'node:fs/promises';
|
|
|
9
11
|
* ```ts
|
|
10
12
|
* import { exists } from '@hypernym/utils/fs'
|
|
11
13
|
*
|
|
12
|
-
* await exists('dir/file.ts') //
|
|
14
|
+
* await exists('dir/file.ts') // true
|
|
13
15
|
* ```
|
|
14
16
|
*/
|
|
15
17
|
declare function exists(path: string): Promise<boolean>;
|
|
16
|
-
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/fs/read.d.ts
|
|
17
20
|
type ReadPath = string | URL;
|
|
18
21
|
type ReadEncodingType = BufferEncoding | null | undefined;
|
|
19
22
|
type ReadType<T> = T extends BufferEncoding | undefined ? string : T extends null ? Buffer : never;
|
|
20
23
|
interface ReadOptions<T extends ReadEncodingType> {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
24
|
+
/**
|
|
25
|
+
* If no encoding is specified, the data is returned as a `Buffer` object. Otherwise, the data will be a string.
|
|
26
|
+
*
|
|
27
|
+
* @default 'utf-8'
|
|
28
|
+
*/
|
|
29
|
+
encoding?: T;
|
|
30
|
+
/**
|
|
31
|
+
* When provided the corresponding `AbortController` can be used to cancel an asynchronous action.
|
|
32
|
+
*
|
|
33
|
+
* @default undefined
|
|
34
|
+
*/
|
|
35
|
+
signal?: AbortSignal;
|
|
36
|
+
/**
|
|
37
|
+
* If a flag is not provided, it defaults to `'r'`.
|
|
38
|
+
*
|
|
39
|
+
* @default 'r'
|
|
40
|
+
*/
|
|
41
|
+
flag?: number | string;
|
|
39
42
|
}
|
|
40
43
|
/**
|
|
41
44
|
* Reads the entire contents of a `file`.
|
|
@@ -49,30 +52,31 @@ interface ReadOptions<T extends ReadEncodingType> {
|
|
|
49
52
|
* ```
|
|
50
53
|
*/
|
|
51
54
|
declare function read<T extends ReadEncodingType = BufferEncoding>(path: ReadPath, options?: ReadOptions<T>): Promise<ReadType<T>>;
|
|
52
|
-
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/fs/readdir.d.ts
|
|
53
57
|
type ReaddirPath = string | URL;
|
|
54
58
|
type ReaddirEncodingType = BufferEncoding | null | 'buffer' | undefined;
|
|
55
59
|
type ReaddirWithFileType = true | undefined;
|
|
56
60
|
type ReaddirType<E, F> = F extends true ? Dirent : E extends BufferEncoding | undefined ? string : E extends null | 'buffer' ? Buffer : never;
|
|
57
61
|
interface ReaddirOptions<E extends ReaddirEncodingType, F extends ReaddirWithFileType> {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
62
|
+
/**
|
|
63
|
+
* If the encoding is set to `'buffer'` or `null`, the filenames returned will be passed as `Buffer` objects.
|
|
64
|
+
*
|
|
65
|
+
* @default 'utf-8'
|
|
66
|
+
*/
|
|
67
|
+
encoding?: E;
|
|
68
|
+
/**
|
|
69
|
+
* If `withFileTypes` is set to `true`, the returned array will contain `fs.Dirent` objects.
|
|
70
|
+
*
|
|
71
|
+
* @default undefined
|
|
72
|
+
*/
|
|
73
|
+
withFileTypes?: F;
|
|
74
|
+
/**
|
|
75
|
+
* Reads the directory recursively.
|
|
76
|
+
*
|
|
77
|
+
* @default true
|
|
78
|
+
*/
|
|
79
|
+
recursive?: boolean;
|
|
76
80
|
}
|
|
77
81
|
/**
|
|
78
82
|
* Reads the contents of a `directory` recursively.
|
|
@@ -86,7 +90,8 @@ interface ReaddirOptions<E extends ReaddirEncodingType, F extends ReaddirWithFil
|
|
|
86
90
|
* ```
|
|
87
91
|
*/
|
|
88
92
|
declare function readdir<E extends ReaddirEncodingType = BufferEncoding, F extends ReaddirWithFileType = undefined>(path: ReaddirPath, options?: ReaddirOptions<E, F>): Promise<ReaddirType<E, F>[]>;
|
|
89
|
-
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/fs/write.d.ts
|
|
90
95
|
type WritePath = string | URL;
|
|
91
96
|
/**
|
|
92
97
|
* Writes data to a `file` recursively.
|
|
@@ -104,24 +109,25 @@ declare function write(path: WritePath, data: Parameters<typeof writeFile$1>[1],
|
|
|
104
109
|
* @deprecated Use `write` instead.
|
|
105
110
|
*/
|
|
106
111
|
declare const writeFile: typeof write;
|
|
107
|
-
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/fs/copy.d.ts
|
|
108
114
|
type CopySource = string | URL;
|
|
109
115
|
type CopyDestination = string | URL;
|
|
110
116
|
interface CopyOptions {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
117
|
+
/**
|
|
118
|
+
* Copies files or directories recursively.
|
|
119
|
+
*
|
|
120
|
+
* @default true
|
|
121
|
+
*/
|
|
122
|
+
recursive?: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Filters copied `files` or `directories`.
|
|
125
|
+
*
|
|
126
|
+
* Returns `true` to copy the item, `false` to ignore it.
|
|
127
|
+
*
|
|
128
|
+
* @default undefined
|
|
129
|
+
*/
|
|
130
|
+
filter?(source: string, destination: string): boolean | Promise<boolean>;
|
|
125
131
|
}
|
|
126
132
|
/**
|
|
127
133
|
* Copies `files` or `directories` recursively.
|
|
@@ -137,23 +143,24 @@ interface CopyOptions {
|
|
|
137
143
|
* ```
|
|
138
144
|
*/
|
|
139
145
|
declare function copy(source: CopySource | CopySource[], destination: CopyDestination, options?: CopyOptions): Promise<void>;
|
|
140
|
-
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/fs/mkdir.d.ts
|
|
141
148
|
type MakeDirPath = string | URL;
|
|
142
149
|
interface MakeDirOptions {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
150
|
+
/**
|
|
151
|
+
* Indicates whether parent folders should be created.
|
|
152
|
+
*
|
|
153
|
+
* If a folder was created, the path to the first created folder will be returned.
|
|
154
|
+
*
|
|
155
|
+
* @default true
|
|
156
|
+
*/
|
|
157
|
+
recursive?: boolean;
|
|
158
|
+
/**
|
|
159
|
+
* A file mode. If a string is passed, it is parsed as an octal integer.
|
|
160
|
+
*
|
|
161
|
+
* @default 0o777
|
|
162
|
+
*/
|
|
163
|
+
mode?: string | number;
|
|
157
164
|
}
|
|
158
165
|
/**
|
|
159
166
|
* Creates a `directory` recursively.
|
|
@@ -169,41 +176,42 @@ interface MakeDirOptions {
|
|
|
169
176
|
* ```
|
|
170
177
|
*/
|
|
171
178
|
declare function mkdir(path: MakeDirPath | MakeDirPath[], options?: MakeDirOptions): Promise<void>;
|
|
172
|
-
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region src/fs/remove.d.ts
|
|
173
181
|
type RemovePath = string | URL;
|
|
174
182
|
interface RemoveOptions {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
183
|
+
/**
|
|
184
|
+
* If `true`, perform a recursive directory removal.
|
|
185
|
+
*
|
|
186
|
+
* In recursive mode, operations are retried on failure.
|
|
187
|
+
*
|
|
188
|
+
* @default true
|
|
189
|
+
*/
|
|
190
|
+
recursive?: boolean;
|
|
191
|
+
/**
|
|
192
|
+
* When `true`, exceptions will be ignored if `path` does not exist.
|
|
193
|
+
*
|
|
194
|
+
* @default true
|
|
195
|
+
*/
|
|
196
|
+
force?: boolean;
|
|
197
|
+
/**
|
|
198
|
+
* If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
|
|
199
|
+
* `EPERM` error is encountered, Node.js will retry the operation with a linear
|
|
200
|
+
* backoff wait of `retryDelay` ms longer on each try. This option represents the
|
|
201
|
+
* number of retries. This option is ignored if the `recursive` option is not
|
|
202
|
+
* `true`.
|
|
203
|
+
*
|
|
204
|
+
* @default 0
|
|
205
|
+
*/
|
|
206
|
+
maxRetries?: number;
|
|
207
|
+
/**
|
|
208
|
+
* The amount of time in milliseconds to wait between retries.
|
|
209
|
+
*
|
|
210
|
+
* This option is ignored if the `recursive` option is not `true`.
|
|
211
|
+
*
|
|
212
|
+
* @default 100
|
|
213
|
+
*/
|
|
214
|
+
retryDelay?: number;
|
|
207
215
|
}
|
|
208
216
|
/**
|
|
209
217
|
* Removes `files` and `directories` recursively.
|
|
@@ -219,6 +227,5 @@ interface RemoveOptions {
|
|
|
219
227
|
* ```
|
|
220
228
|
*/
|
|
221
229
|
declare function remove(path: RemovePath | RemovePath[], options?: RemoveOptions): Promise<void>;
|
|
222
|
-
|
|
223
|
-
export { copy, exists, mkdir, read, readdir, remove, write, writeFile };
|
|
224
|
-
export type { CopyDestination, CopyOptions, CopySource, MakeDirOptions, MakeDirPath, ReadEncodingType, ReadOptions, ReadPath, ReadType, ReaddirEncodingType, ReaddirOptions, ReaddirPath, ReaddirType, ReaddirWithFileType, RemoveOptions, RemovePath, WritePath };
|
|
230
|
+
//#endregion
|
|
231
|
+
export { CopyDestination, CopyOptions, CopySource, MakeDirOptions, MakeDirPath, ReadEncodingType, ReadOptions, ReadPath, ReadType, ReaddirEncodingType, ReaddirOptions, ReaddirPath, ReaddirType, ReaddirWithFileType, RemoveOptions, RemovePath, WritePath, copy, exists, mkdir, read, readdir, remove, write, writeFile };
|
package/dist/fs/index.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { access, constants, cp, mkdir as mkdir$1, readFile, readdir as readdir$1, rm, writeFile as writeFile$1 } from "node:fs/promises";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { isString, isURL } from "../index.js";
|
|
5
|
+
|
|
6
|
+
//#region src/fs/exists.ts
|
|
7
|
+
/**
|
|
8
|
+
* Checks if the `file` or `directory` exists.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { exists } from '../index.js'
|
|
14
|
+
*
|
|
15
|
+
* await exists('dir/file.ts') // true
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
async function exists(path) {
|
|
19
|
+
return await access(path, constants.F_OK).then(() => true).catch(() => false);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/fs/read.ts
|
|
24
|
+
/**
|
|
25
|
+
* Reads the entire contents of a `file`.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { read } from '../index.js'
|
|
31
|
+
*
|
|
32
|
+
* await read('dir/subdir/file.ts')
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
async function read(path, options = {}) {
|
|
36
|
+
const { encoding = "utf-8" } = options;
|
|
37
|
+
return await readFile(path, {
|
|
38
|
+
encoding,
|
|
39
|
+
...options
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/fs/readdir.ts
|
|
45
|
+
/**
|
|
46
|
+
* Reads the contents of a `directory` recursively.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { readdir } from '../index.js'
|
|
52
|
+
*
|
|
53
|
+
* await readdir('dir/subdir')
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
async function readdir(path, options = {}) {
|
|
57
|
+
const { encoding = "utf-8", recursive = true } = options;
|
|
58
|
+
return await readdir$1(path, {
|
|
59
|
+
encoding,
|
|
60
|
+
recursive,
|
|
61
|
+
...options
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/fs/write.ts
|
|
67
|
+
/**
|
|
68
|
+
* Writes data to a `file` recursively.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
*
|
|
72
|
+
* ```ts
|
|
73
|
+
* import { write } from '../index.js'
|
|
74
|
+
*
|
|
75
|
+
* await write('dir/subdir/file.ts', `console.log('Hello World!')`)
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
async function write(path, data, options) {
|
|
79
|
+
await mkdir$1(dirname(isURL(path) ? fileURLToPath(path) : path), { recursive: true });
|
|
80
|
+
await writeFile$1(path, data, options);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* @deprecated Use `write` instead.
|
|
84
|
+
*/
|
|
85
|
+
const writeFile = write;
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/fs/copy.ts
|
|
89
|
+
/**
|
|
90
|
+
* Copies `files` or `directories` recursively.
|
|
91
|
+
*
|
|
92
|
+
* Accepts a single source or a range of sources.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
*
|
|
96
|
+
* ```ts
|
|
97
|
+
* import { copy } from '../index.js'
|
|
98
|
+
*
|
|
99
|
+
* await copy('src/subdir/file.ts', './dist/subdir')
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
async function copy(source, destination, options = {}) {
|
|
103
|
+
const { recursive = true, filter } = options;
|
|
104
|
+
const sources = isString(source) || isURL(source) ? [source] : source;
|
|
105
|
+
for (const src of sources) await cp(src, destination, {
|
|
106
|
+
recursive,
|
|
107
|
+
filter
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/fs/mkdir.ts
|
|
113
|
+
/**
|
|
114
|
+
* Creates a `directory` recursively.
|
|
115
|
+
*
|
|
116
|
+
* Accepts a single path or a range of paths.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* import { mkdir } from '../index.js'
|
|
122
|
+
*
|
|
123
|
+
* await mkdir('src/subdir')
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
async function mkdir(path, options = {}) {
|
|
127
|
+
const { recursive = true, mode } = options;
|
|
128
|
+
const paths = isString(path) || isURL(path) ? [path] : path;
|
|
129
|
+
for (const p of paths) await mkdir$1(p, {
|
|
130
|
+
recursive,
|
|
131
|
+
mode
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
//#endregion
|
|
136
|
+
//#region src/fs/remove.ts
|
|
137
|
+
/**
|
|
138
|
+
* Removes `files` and `directories` recursively.
|
|
139
|
+
*
|
|
140
|
+
* Accepts a single path or a range of paths.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
*
|
|
144
|
+
* ```ts
|
|
145
|
+
* import { remove } from '../index.js'
|
|
146
|
+
*
|
|
147
|
+
* await remove('src/subdir/file.ts')
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
async function remove(path, options = {}) {
|
|
151
|
+
const { recursive = true, force = true } = options;
|
|
152
|
+
const paths = isString(path) || isURL(path) ? [path] : path;
|
|
153
|
+
for (const p of paths) await rm(p, {
|
|
154
|
+
recursive,
|
|
155
|
+
force,
|
|
156
|
+
...options
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
//#endregion
|
|
161
|
+
export { copy, exists, mkdir, read, readdir, remove, write, writeFile };
|