-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
286 lines (270 loc) · 6.79 KB
/
types.ts
File metadata and controls
286 lines (270 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* @fileoverview Public type surface for `fs/*` modules — option
* shapes, encoding union, and result records. Pure types only; no
* runtime side effects so this module stays cheap to import everywhere.
*/
import type { Abortable } from 'node:events'
import type { ObjectEncodingOptions, OpenMode } from 'node:fs'
import type { JsonReviver } from '../json/types'
import type { Remap } from '../objects/types'
/**
* Supported text encodings for Node.js Buffers.
* Includes ASCII, UTF-8/16, base64, binary, and hexadecimal encodings.
*/
export type BufferEncoding =
| 'ascii'
| 'utf8'
| 'utf-8'
| 'utf16le'
| 'ucs2'
| 'ucs-2'
| 'base64'
| 'base64url'
| 'latin1'
| 'binary'
| 'hex'
/**
* Options for asynchronous `findUp` operations.
*/
export interface FindUpOptions {
/**
* Starting directory for the search.
* @default process.cwd()
*/
cwd?: string | undefined
/**
* Only match directories, not files.
* @default false
*/
onlyDirectories?: boolean | undefined
/**
* Only match files, not directories.
* @default true
*/
onlyFiles?: boolean | undefined
/**
* Abort signal to cancel the search operation.
*/
signal?: AbortSignal | undefined
}
/**
* Options for synchronous `findUpSync` operations.
*/
export interface FindUpSyncOptions {
/**
* Starting directory for the search.
* @default process.cwd()
*/
cwd?: string | undefined
/**
* Directory to stop searching at (inclusive).
* When provided, search will stop at this directory even if the root hasn't been reached.
*/
stopAt?: string | undefined
/**
* Only match directories, not files.
* @default false
*/
onlyDirectories?: boolean | undefined
/**
* Only match files, not directories.
* @default true
*/
onlyFiles?: boolean | undefined
}
/**
* Options for checking if a directory is empty.
*/
export interface IsDirEmptyOptions {
/**
* Glob patterns for files to ignore when checking emptiness.
* Files matching these patterns are not counted.
* @default defaultIgnore
*/
ignore?: string[] | readonly string[] | undefined
}
/**
* Represents any valid JSON content type.
*/
export type JsonContent = unknown
/**
* Options for reading directories with filtering and sorting.
*/
export interface ReadDirOptions {
/**
* Glob patterns for directories to ignore.
* @default undefined
*/
ignore?: string[] | readonly string[] | undefined
/**
* Include empty directories in results.
* When `false`, empty directories are filtered out.
* @default true
*/
includeEmpty?: boolean | undefined
/**
* Sort directory names alphabetically using natural sort order.
* @default true
*/
sort?: boolean | undefined
}
/**
* Options for reading files with encoding and abort support.
* Can be either an options object, an encoding string, or null.
*/
export type ReadFileOptions =
| Remap<
ObjectEncodingOptions &
Abortable & {
flag?: OpenMode | undefined
}
>
| BufferEncoding
| null
/**
* Options for reading and parsing JSON files.
*/
export type ReadJsonOptions = Remap<
ReadFileOptions & {
/**
* Whether to throw errors on parse failure.
* When `false`, returns `undefined` on error instead of throwing.
* @default true
*/
throws?: boolean | undefined
/**
* JSON reviver function to transform parsed values.
* Same as the second parameter to `JSON.parse()`.
*/
reviver?: Parameters<typeof JSON.parse>[1] | undefined
}
>
/**
* Options for read operations with abort support.
*/
export interface ReadOptions extends Abortable {
/**
* Character encoding to use for reading.
* @default 'utf8'
*/
encoding?: BufferEncoding | string | undefined
/**
* File system flag for reading behavior.
* @default 'r'
*/
flag?: string | undefined
}
/**
* Options for file/directory removal operations.
*/
export interface RemoveOptions {
/**
* Force deletion even outside normally safe directories.
* When `false`, prevents deletion outside temp, cacache, and ~/.socket.
* @default true for safe directories, false otherwise
*/
force?: boolean | undefined
/**
* Maximum number of retry attempts on failure.
* @default 3
*/
maxRetries?: number | undefined
/**
* Recursively delete directories and contents.
* @default true
*/
recursive?: boolean | undefined
/**
* Delay in milliseconds between retry attempts.
* @default 200
*/
retryDelay?: number | undefined
/**
* Abort signal to cancel the operation.
*/
signal?: AbortSignal | undefined
}
/**
* Options for safe read operations that don't throw on errors.
*/
export interface SafeReadOptions extends ReadOptions {
/**
* Default value to return on read failure.
* If not provided, `undefined` is returned on error.
*/
defaultValue?: unknown | undefined
}
/**
* Result of file readability validation.
* Contains lists of valid and invalid file paths.
*/
export interface ValidateFilesResult {
/**
* File paths that passed validation and are readable.
*/
validPaths: string[]
/**
* File paths that failed validation (unreadable, permission denied, or non-existent).
* Common with Yarn Berry PnP virtual filesystem, pnpm symlinks, or filesystem race conditions.
*/
invalidPaths: string[]
}
/**
* Options for writing JSON files with formatting control.
*/
export interface WriteJsonOptions extends WriteOptions {
/**
* End-of-line sequence to use.
* @default '\n'
* @example
* ```ts
* // Windows-style line endings
* writeJson('data.json', data, { EOL: '\r\n' })
* ```
*/
EOL?: string | undefined
/**
* Whether to add a final newline at end of file.
* @default true
*/
finalEOL?: boolean | undefined
/**
* JSON replacer function to transform values during stringification.
* Same as the second parameter to `JSON.stringify()`.
*/
replacer?: JsonReviver | undefined
/**
* Number of spaces for indentation, or string to use for indentation.
* @default 2
* @example
* ```ts
* // Use tabs instead of spaces
* writeJson('data.json', data, { spaces: '\t' })
*
* // Use 4 spaces for indentation
* writeJson('data.json', data, { spaces: 4 })
* ```
*/
spaces?: number | string | undefined
}
/**
* Options for write operations with encoding and mode control.
*/
export interface WriteOptions extends Abortable {
/**
* Character encoding for writing.
* @default 'utf8'
*/
encoding?: BufferEncoding | string | undefined
/**
* File mode (permissions) to set.
* Uses standard Unix permission bits (e.g., 0o644).
* @default 0o666 (read/write for all, respecting umask)
*/
mode?: number | undefined
/**
* File system flag for write behavior.
* @default 'w' (create or truncate)
*/
flag?: string | undefined
}