@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.
@@ -1,5 +1,7 @@
1
- import { Dirent } from 'node:fs';
2
- import { writeFile as writeFile$1 } from 'node:fs/promises';
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') // => true
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
- * 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;
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
- * 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;
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
- * Copies files or directories recursively.
113
- *
114
- * @default true
115
- */
116
- recursive?: boolean;
117
- /**
118
- * Filters copied `files` or `directories`.
119
- *
120
- * Returns `true` to copy the item, `false` to ignore it.
121
- *
122
- * @default undefined
123
- */
124
- filter?(source: string, destination: string): boolean | Promise<boolean>;
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
- * Indicates whether parent folders should be created.
145
- *
146
- * If a folder was created, the path to the first created folder will be returned.
147
- *
148
- * @default true
149
- */
150
- recursive?: boolean;
151
- /**
152
- * A file mode. If a string is passed, it is parsed as an octal integer.
153
- *
154
- * @default 0o777
155
- */
156
- mode?: string | number;
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
- * If `true`, perform a recursive directory removal.
177
- *
178
- * In recursive mode, operations are retried on failure.
179
- *
180
- * @default true
181
- */
182
- recursive?: boolean;
183
- /**
184
- * When `true`, exceptions will be ignored if `path` does not exist.
185
- *
186
- * @default true
187
- */
188
- force?: boolean;
189
- /**
190
- * If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
191
- * `EPERM` error is encountered, Node.js will retry the operation with a linear
192
- * backoff wait of `retryDelay` ms longer on each try. This option represents the
193
- * number of retries. This option is ignored if the `recursive` option is not
194
- * `true`.
195
- *
196
- * @default 0
197
- */
198
- maxRetries?: number;
199
- /**
200
- * The amount of time in milliseconds to wait between retries.
201
- *
202
- * This option is ignored if the `recursive` option is not `true`.
203
- *
204
- * @default 100
205
- */
206
- retryDelay?: number;
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 };
@@ -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 };