@gjsify/fs 0.3.21 → 0.4.3

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.
Files changed (69) hide show
  1. package/lib/esm/_virtual/_rolldown/runtime.js +1 -1
  2. package/lib/esm/callback.js +1 -1
  3. package/lib/esm/cp.js +1 -1
  4. package/lib/esm/dir.js +1 -1
  5. package/lib/esm/dirent.js +1 -1
  6. package/lib/esm/encoding.js +1 -1
  7. package/lib/esm/errors.js +1 -1
  8. package/lib/esm/fd-ops.js +1 -1
  9. package/lib/esm/file-handle.js +1 -1
  10. package/lib/esm/fs-watcher.js +1 -1
  11. package/lib/esm/glob.js +1 -1
  12. package/lib/esm/read-stream.js +1 -1
  13. package/lib/esm/stat-watcher.js +1 -1
  14. package/lib/esm/statfs.js +1 -1
  15. package/lib/esm/stats.js +1 -1
  16. package/lib/esm/sync.js +1 -1
  17. package/lib/esm/utils.js +1 -1
  18. package/lib/esm/utimes.js +1 -1
  19. package/lib/esm/write-stream.js +1 -1
  20. package/package.json +51 -48
  21. package/src/callback.spec.ts +0 -296
  22. package/src/callback.ts +0 -684
  23. package/src/cp.spec.ts +0 -181
  24. package/src/cp.ts +0 -328
  25. package/src/dir.spec.ts +0 -204
  26. package/src/dir.ts +0 -199
  27. package/src/dirent.ts +0 -165
  28. package/src/encoding.ts +0 -45
  29. package/src/errors.spec.ts +0 -389
  30. package/src/errors.ts +0 -19
  31. package/src/extended.spec.ts +0 -706
  32. package/src/fd-ops.spec.ts +0 -234
  33. package/src/fd-ops.ts +0 -251
  34. package/src/file-handle.spec.ts +0 -115
  35. package/src/file-handle.ts +0 -856
  36. package/src/fs-watcher.ts +0 -198
  37. package/src/glob.spec.ts +0 -201
  38. package/src/glob.ts +0 -205
  39. package/src/index.ts +0 -313
  40. package/src/new-apis.spec.ts +0 -505
  41. package/src/promises.spec.ts +0 -812
  42. package/src/promises.ts +0 -686
  43. package/src/read-stream.ts +0 -128
  44. package/src/stat-watcher.ts +0 -116
  45. package/src/stat.spec.ts +0 -87
  46. package/src/statfs.spec.ts +0 -67
  47. package/src/statfs.ts +0 -92
  48. package/src/stats.ts +0 -207
  49. package/src/streams.spec.ts +0 -513
  50. package/src/symlink.spec.ts +0 -188
  51. package/src/sync.spec.ts +0 -377
  52. package/src/sync.ts +0 -562
  53. package/src/test.mts +0 -27
  54. package/src/types/encoding-option.ts +0 -3
  55. package/src/types/file-read-options.ts +0 -15
  56. package/src/types/file-read-result.ts +0 -4
  57. package/src/types/flag-and-open-mode.ts +0 -6
  58. package/src/types/index.ts +0 -6
  59. package/src/types/open-flags.ts +0 -14
  60. package/src/types/read-options.ts +0 -9
  61. package/src/utils.ts +0 -31
  62. package/src/utimes.spec.ts +0 -113
  63. package/src/utimes.ts +0 -97
  64. package/src/watch.spec.ts +0 -171
  65. package/src/watchfile.spec.ts +0 -185
  66. package/src/write-stream.ts +0 -142
  67. package/test/file.txt +0 -1
  68. package/tsconfig.json +0 -29
  69. package/tsconfig.tsbuildinfo +0 -1
package/src/callback.ts DELETED
@@ -1,684 +0,0 @@
1
- // Reference: Node.js lib/fs.js (callback API)
2
- // Reimplemented for GJS using Gio.File async operations
3
-
4
- import GLib from '@girs/glib-2.0';
5
- import Gio from '@girs/gio-2.0';
6
- import { open as openP, rm as rmP } from './promises.js'
7
- import { PathLike, OpenMode, Mode, ReadPosition, ReadAsyncOptions, NoParamCallback, RmOptions, RmDirOptions, MakeDirectoryOptions } from 'node:fs';
8
- import { FileHandle } from './file-handle.js';
9
- import { Buffer } from 'node:buffer';
10
- import { Stats, BigIntStats, STAT_ATTRIBUTES } from './stats.js';
11
- import { createNodeError } from './errors.js';
12
- import { realpathSync, readdirSync, renameSync, copyFileSync, accessSync, appendFileSync, readlinkSync, truncateSync, chmodSync, chownSync, mkdirSync, rmdirSync, readFileSync, writeFileSync } from './sync.js';
13
- import { normalizePath } from './utils.js';
14
- // encoding helpers available if needed in future
15
-
16
- import type { OpenFlags } from './types/index.js';
17
-
18
- // --- helpers ---
19
-
20
- function parseOptsCb(optionsOrCallback: unknown, maybeCallback?: Function): { options: Record<string, unknown>; callback: Function } {
21
- return typeof optionsOrCallback === 'function'
22
- ? { options: {}, callback: optionsOrCallback }
23
- : { options: (optionsOrCallback ?? {}) as Record<string, unknown>, callback: maybeCallback! };
24
- }
25
-
26
- function statImpl(path: PathLike, flags: Gio.FileQueryInfoFlags, syscall: string, options: Record<string, unknown>, callback: Function): void {
27
- const pathStr = normalizePath(path);
28
- const file = Gio.File.new_for_path(pathStr);
29
- file.query_info_async(STAT_ATTRIBUTES, flags, GLib.PRIORITY_DEFAULT, null, (_s: Gio.File, res: Gio.AsyncResult) => {
30
- try {
31
- const info = file.query_info_finish(res);
32
- callback(null, options?.bigint ? new BigIntStats(info, pathStr) : new Stats(info, pathStr));
33
- } catch (err: unknown) {
34
- callback(createNodeError(err, syscall, pathStr));
35
- }
36
- });
37
- }
38
-
39
- // --- stat / lstat ---
40
-
41
- export function stat(path: PathLike, callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void): void;
42
- export function stat(path: PathLike, options: { bigint?: boolean }, callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void): void;
43
- export function stat(path: PathLike, optionsOrCallback: { bigint?: boolean } | ((err: NodeJS.ErrnoException | null, stats: Stats) => void), maybeCallback?: Function): void {
44
- const { options, callback } = parseOptsCb(optionsOrCallback, maybeCallback);
45
- statImpl(path, Gio.FileQueryInfoFlags.NONE, 'stat', options, callback);
46
- }
47
-
48
- export function lstat(path: PathLike, callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void): void;
49
- export function lstat(path: PathLike, options: { bigint?: boolean }, callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void): void;
50
- export function lstat(path: PathLike, optionsOrCallback: { bigint?: boolean } | ((err: NodeJS.ErrnoException | null, stats: Stats) => void), maybeCallback?: Function): void {
51
- const { options, callback } = parseOptsCb(optionsOrCallback, maybeCallback);
52
- statImpl(path, Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, 'lstat', options, callback);
53
- }
54
-
55
- // --- readdir ---
56
-
57
- export function readdir(path: PathLike, callback: (err: NodeJS.ErrnoException | null, files: string[]) => void): void;
58
- export function readdir(path: PathLike, options: { withFileTypes?: boolean; encoding?: string; recursive?: boolean }, callback: (err: NodeJS.ErrnoException | null, files: string[] | unknown[]) => void): void;
59
- export function readdir(path: PathLike, optionsOrCallback: { withFileTypes?: boolean; encoding?: string; recursive?: boolean } | ((err: NodeJS.ErrnoException | null, files: string[]) => void), maybeCallback?: Function): void {
60
- const { options, callback } = parseOptsCb(optionsOrCallback, maybeCallback);
61
- Promise.resolve().then(() => {
62
- try {
63
- callback(null, readdirSync(path, options as { withFileTypes?: boolean; encoding?: string; recursive?: boolean }));
64
- } catch (err: unknown) {
65
- callback(createNodeError(err, 'readdir', path));
66
- }
67
- });
68
- }
69
-
70
- // --- realpath ---
71
-
72
- export function realpath(path: PathLike, callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void): void;
73
- export function realpath(path: PathLike, options: { encoding?: BufferEncoding }, callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void): void;
74
- export function realpath(path: PathLike, optionsOrCallback: { encoding?: BufferEncoding } | ((err: NodeJS.ErrnoException | null, resolvedPath: string) => void), maybeCallback?: Function): void {
75
- const { callback } = parseOptsCb(optionsOrCallback, maybeCallback);
76
- Promise.resolve().then(() => {
77
- try {
78
- callback(null, realpathSync(path));
79
- } catch (err: unknown) {
80
- callback(err as NodeJS.ErrnoException);
81
- }
82
- });
83
- }
84
-
85
- // --- symlink ---
86
-
87
- export function symlink(target: PathLike, path: PathLike, callback: NoParamCallback): void;
88
- export function symlink(target: PathLike, path: PathLike, type: string | null, callback: NoParamCallback): void;
89
- export function symlink(target: PathLike, path: PathLike, typeOrCallback: string | null | NoParamCallback, maybeCallback?: NoParamCallback): void {
90
- const callback: NoParamCallback = typeof typeOrCallback === 'function' ? typeOrCallback : maybeCallback!;
91
- if (typeof callback !== 'function') {
92
- throw new TypeError('Callback must be a function. Received ' + typeof callback);
93
- }
94
- const pathStr = normalizePath(path);
95
- const targetStr = normalizePath(target);
96
- const file = Gio.File.new_for_path(pathStr);
97
- file.make_symbolic_link_async(targetStr, GLib.PRIORITY_DEFAULT, null, (_s: Gio.File, res: Gio.AsyncResult) => {
98
- try {
99
- file.make_symbolic_link_finish(res);
100
- callback(null);
101
- } catch (err: unknown) {
102
- callback(createNodeError(err, 'symlink', targetStr, pathStr));
103
- }
104
- });
105
- }
106
-
107
- type OpenCallback = (err: NodeJS.ErrnoException | null, fd: number) => void;
108
-
109
- type WriteStrCallback = (err: NodeJS.ErrnoException | null, written: number, str: string) => void;
110
- type WriteBufCallback = <TBuffer extends NodeJS.ArrayBufferView> (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void;
111
-
112
- type ReadCallback = (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: NodeJS.ArrayBufferView) => void;
113
-
114
- /**
115
- * Asynchronous file open. See the POSIX [`open(2)`](http://man7.org/linux/man-pages/man2/open.2.html) documentation for more details.
116
- *
117
- * `mode` sets the file mode (permission and sticky bits), but only if the file was
118
- * created. On Windows, only the write permission can be manipulated; see {@link chmod}.
119
- *
120
- * The callback gets two arguments `(err, fd)`.
121
- *
122
- * Some characters (`< > : " / \ | ? *`) are reserved under Windows as documented
123
- * by [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). Under NTFS, if the filename contains
124
- * a colon, Node.js will open a file system stream, as described by [this MSDN page](https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams).
125
- *
126
- * Functions based on `fs.open()` exhibit this behavior as well:`fs.writeFile()`, `fs.readFile()`, etc.
127
- * @since v0.0.2
128
- * @param [flags='r'] See `support of file system `flags``.
129
- * @param [mode=0o666]
130
- */
131
- export function open(path: PathLike, flags: OpenMode | undefined, mode: Mode | undefined | null, callback: OpenCallback): void;
132
- /**
133
- * Asynchronous open(2) - open and possibly create a file. If the file is created, its mode will be `0o666`.
134
- * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
135
- * @param [flags='r'] See `support of file system `flags``.
136
- */
137
- export function open(path: PathLike, flags: OpenMode | undefined, callback: OpenCallback): void;
138
- /**
139
- * Asynchronous open(2) - open and possibly create a file. If the file is created, its mode will be `0o666`.
140
- * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
141
- */
142
- export function open(path: PathLike, callback: OpenCallback): void;
143
-
144
- export function open(path: PathLike, ...args: (OpenMode | Mode | OpenCallback | undefined | null)[]): void {
145
- let flags: OpenMode | undefined;
146
- let mode: Mode | undefined | null;
147
- let callback: OpenCallback
148
-
149
- switch (args.length) {
150
- case 1:
151
- callback = args[0] as OpenCallback
152
- break;
153
- case 2:
154
- flags = args[0] as OpenMode | undefined
155
- callback = args[1] as OpenCallback
156
- break;
157
- case 3:
158
- flags = args[0] as OpenMode | undefined
159
- mode = args[1] as Mode | undefined | null
160
- callback = args[2] as OpenCallback
161
- break;
162
- default:
163
- break;
164
- }
165
-
166
- openP(path, flags as OpenFlags | number | undefined, mode)
167
- .then((fileHandle) => {
168
- callback(null, fileHandle.fd);
169
- })
170
- .catch((err) => {
171
- callback(err, -1);
172
- })
173
- }
174
-
175
- /**
176
- * Write `buffer` to the file specified by `fd`.
177
- *
178
- * `offset` determines the part of the buffer to be written, and `length` is
179
- * an integer specifying the number of bytes to write.
180
- *
181
- * `position` refers to the offset from the beginning of the file where this data
182
- * should be written. If `typeof position !== 'number'`, the data will be written
183
- * at the current position. See [`pwrite(2)`](http://man7.org/linux/man-pages/man2/pwrite.2.html).
184
- *
185
- * The callback will be given three arguments `(err, bytesWritten, buffer)` where`bytesWritten` specifies how many _bytes_ were written from `buffer`.
186
- *
187
- * If this method is invoked as its `util.promisify()` ed version, it returns
188
- * a promise for an `Object` with `bytesWritten` and `buffer` properties.
189
- *
190
- * It is unsafe to use `fs.write()` multiple times on the same file without waiting
191
- * for the callback. For this scenario, {@link createWriteStream} is
192
- * recommended.
193
- *
194
- * On Linux, positional writes don't work when the file is opened in append mode.
195
- * The kernel ignores the position argument and always appends the data to
196
- * the end of the file.
197
- * @since v0.0.2
198
- */
199
- export function write<TBuffer extends NodeJS.ArrayBufferView>(
200
- fd: number,
201
- buffer: TBuffer,
202
- offset: number | undefined | null,
203
- length: number | undefined | null,
204
- position: number | undefined | null,
205
- callback: WriteBufCallback
206
- ): void;
207
- /**
208
- * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
209
- * @param fd A file descriptor.
210
- * @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
211
- * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`.
212
- */
213
- export function write<TBuffer extends NodeJS.ArrayBufferView>(
214
- fd: number,
215
- buffer: TBuffer,
216
- offset: number | undefined | null,
217
- length: number | undefined | null,
218
- callback: WriteBufCallback
219
- ): void;
220
- /**
221
- * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
222
- * @param fd A file descriptor.
223
- * @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
224
- */
225
- export function write<TBuffer extends NodeJS.ArrayBufferView>(
226
- fd: number,
227
- buffer: TBuffer,
228
- offset: number | undefined | null,
229
- callback: WriteBufCallback
230
- ): void;
231
- /**
232
- * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
233
- * @param fd A file descriptor.
234
- */
235
- export function write<TBuffer extends NodeJS.ArrayBufferView>(fd: number, buffer: TBuffer, callback: WriteBufCallback): void;
236
- /**
237
- * Asynchronously writes `string` to the file referenced by the supplied file descriptor.
238
- * @param fd A file descriptor.
239
- * @param string A string to write.
240
- * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
241
- * @param encoding The expected string encoding.
242
- */
243
- export function write(
244
- fd: number,
245
- string: string,
246
- position: number | undefined | null,
247
- encoding: BufferEncoding | undefined | null,
248
- callback: WriteStrCallback
249
- ): void;
250
- /**
251
- * Asynchronously writes `string` to the file referenced by the supplied file descriptor.
252
- * @param fd A file descriptor.
253
- * @param string A string to write.
254
- * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
255
- */
256
- export function write(fd: number, string: string, position: number | undefined | null, callback: WriteStrCallback): void;
257
- /**
258
- * Asynchronously writes `string` to the file referenced by the supplied file descriptor.
259
- * @param fd A file descriptor.
260
- * @param string A string to write.
261
- */
262
- export function write(fd: number, string: string, callback: WriteStrCallback): void;
263
-
264
- export function write<TBuffer extends NodeJS.ArrayBufferView>(fd: number, data: string | TBuffer, ...args: (number | string | BufferEncoding | WriteStrCallback | WriteBufCallback | undefined | null)[]): void {
265
-
266
- const fileHandle = FileHandle.getInstance(fd);
267
-
268
- if (typeof data === 'string') {
269
- const callback = args.pop() as WriteStrCallback;
270
- const position = args[0] as number | undefined | null;
271
- const encoding = args[1] as BufferEncoding | undefined | null;
272
-
273
- fileHandle.write(data, position, encoding)
274
- .then((res) => {
275
- callback(null, res.bytesWritten, res.buffer);
276
- })
277
- .catch((err) => {
278
- callback(err, 0, '');
279
- });
280
-
281
- return;
282
- }
283
-
284
- const callback = args[args.length -1] as WriteBufCallback;
285
- const offset = args[0] as number | undefined;
286
- const length = args[1] as number | undefined;
287
- const position = args[2] as number | undefined;
288
-
289
- fileHandle.write(data, offset, length, position)
290
- .then((res) => {
291
- callback(null, res.bytesWritten, res.buffer);
292
- })
293
- .catch((err) => {
294
- callback(err, 0, Buffer.from([]));
295
- });
296
- }
297
-
298
- /**
299
- * Read data from the file specified by `fd`.
300
- *
301
- * The callback is given the three arguments, `(err, bytesRead, buffer)`.
302
- *
303
- * If the file is not modified concurrently, the end-of-file is reached when the
304
- * number of bytes read is zero.
305
- *
306
- * If this method is invoked as its `util.promisify()` ed version, it returns
307
- * a promise for an `Object` with `bytesRead` and `buffer` properties.
308
- * @since v0.0.2
309
- * @param buffer The buffer that the data will be written to.
310
- * @param offset The position in `buffer` to write the data to.
311
- * @param length The number of bytes to read.
312
- * @param position Specifies where to begin reading from in the file. If `position` is `null` or `-1 `, data will be read from the current file position, and the file position will be updated. If
313
- * `position` is an integer, the file position will be unchanged.
314
- */
315
- export function read<TBuffer extends NodeJS.ArrayBufferView>(
316
- fd: number,
317
- buffer: TBuffer,
318
- offset: number,
319
- length: number,
320
- position: ReadPosition | null,
321
- callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void
322
- ): void;
323
- /**
324
- * Similar to the above `fs.read` function, this version takes an optional `options` object.
325
- * If not otherwise specified in an `options` object,
326
- * `buffer` defaults to `Buffer.alloc(16384)`,
327
- * `offset` defaults to `0`,
328
- * `length` defaults to `buffer.byteLength`, `- offset` as of Node 17.6.0
329
- * `position` defaults to `null`
330
- * @since v12.17.0, 13.11.0
331
- */
332
- export function read<TBuffer extends NodeJS.ArrayBufferView>(
333
- fd: number,
334
- options: ReadAsyncOptions<TBuffer>,
335
- callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void
336
- ): void;
337
- export function read(fd: number, callback: ReadCallback): void;
338
-
339
- /**
340
- * Read data from the file specified by `fd`.
341
- *
342
- * The callback is given the three arguments, `(err, bytesRead, buffer)`.
343
- *
344
- * If the file is not modified concurrently, the end-of-file is reached when the
345
- * number of bytes read is zero.
346
- *
347
- * If this method is invoked as its `util.promisify()` ed version, it returns
348
- * a promise for an `Object` with `bytesRead` and `buffer` properties.
349
- * @since v0.0.2
350
- * @param buffer The buffer that the data will be written to.
351
- * @param offset The position in `buffer` to write the data to.
352
- * @param length The number of bytes to read.
353
- * @param position Specifies where to begin reading from in the file. If `position` is `null` or `-1 `, data will be read from the current file position, and the file position will be updated. If
354
- * `position` is an integer, the file position will be unchanged.
355
- */
356
- export function read<TBuffer extends NodeJS.ArrayBufferView>(
357
- fd: number,
358
- buffer: TBuffer,
359
- offset: number,
360
- length: number,
361
- position: ReadPosition | null,
362
- callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void
363
- ): void;
364
- /**
365
- * Similar to the above `fs.read` function, this version takes an optional `options` object.
366
- * If not otherwise specified in an `options` object,
367
- * `buffer` defaults to `Buffer.alloc(16384)`,
368
- * `offset` defaults to `0`,
369
- * `length` defaults to `buffer.byteLength`, `- offset` as of Node 17.6.0
370
- * `position` defaults to `null`
371
- * @since v12.17.0, 13.11.0
372
- */
373
- export function read<TBuffer extends NodeJS.ArrayBufferView>(
374
- fd: number,
375
- options: ReadAsyncOptions<TBuffer>,
376
- callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void
377
- ): void;
378
- export function read(fd: number, callback: ReadCallback): void;
379
-
380
- export function read(fd: number, ...args: unknown[]): void {
381
-
382
- const fileHandle = FileHandle.getInstance(fd);
383
-
384
- const callback: ReadCallback = args[args.length -1] as ReadCallback;
385
-
386
- let buffer: NodeJS.ArrayBufferView | undefined;
387
- let offset: number | null | undefined;
388
- let length: number | null | undefined;
389
- let position: ReadPosition | null | undefined;
390
-
391
- if (args.length <= 1) {
392
- // read(fd, callback) — use defaults
393
- } else if (typeof args[0] === 'object' && !ArrayBuffer.isView(args[0])) {
394
- const options = args[0] as ReadAsyncOptions<NodeJS.ArrayBufferView>;
395
- buffer = options.buffer;
396
- offset = options.offset;
397
- length = options.length;
398
- position = options.position;
399
- } else {
400
- buffer = args[0] as NodeJS.ArrayBufferView | undefined;
401
- offset = args[1] as number | null | undefined;
402
- length = args[2] as number | null | undefined;
403
- position = args[3] as ReadPosition | null | undefined;
404
- }
405
-
406
- fileHandle.read(buffer, offset, length, position)
407
- .then((res) => {
408
- callback(null, res.bytesRead, res.buffer);
409
- })
410
- .catch((err) => {
411
- callback(err, 0, Buffer.from([]));
412
- });
413
- }
414
-
415
- /**
416
- * Closes the file descriptor. No arguments other than a possible exception are
417
- * given to the completion callback.
418
- *
419
- * Calling `fs.close()` on any file descriptor (`fd`) that is currently in use
420
- * through any other `fs` operation may lead to undefined behavior.
421
- *
422
- * See the POSIX [`close(2)`](http://man7.org/linux/man-pages/man2/close.2.html) documentation for more detail.
423
- * @since v0.0.2
424
- */
425
- export function close(fd: number, callback?: NoParamCallback): void {
426
- FileHandle.getInstance(fd).close()
427
- .then(() => {
428
- callback(null);
429
- })
430
- .catch((err) => callback(err))
431
- }
432
-
433
- /**
434
- * Asynchronously removes files and directories (modeled on the standard POSIX `rm`utility). No arguments other than a possible exception are given to the
435
- * completion callback.
436
- * @since v14.14.0
437
- */
438
- export function rm(path: PathLike, callback: NoParamCallback): void;
439
- export function rm(path: PathLike, options: RmOptions, callback: NoParamCallback): void;
440
-
441
- export function rm(path: PathLike, ...args: (RmOptions | NoParamCallback)[]): void {
442
-
443
- let options: RmOptions = {};
444
- let callback: NoParamCallback = args[args.length -1] as NoParamCallback;
445
-
446
- if (args.length >= 2) {
447
- options = args[0] as RmOptions;
448
- }
449
-
450
- rmP(path, options)
451
- .then(() => {
452
- callback(null);
453
- })
454
- .catch((err) => {
455
- callback(err);
456
- });
457
- }
458
-
459
- // --- rename ---
460
-
461
- export function rename(oldPath: PathLike, newPath: PathLike, callback: NoParamCallback): void {
462
- Promise.resolve().then(() => {
463
- try {
464
- renameSync(oldPath, newPath);
465
- callback(null);
466
- } catch (err: unknown) {
467
- callback(err as NodeJS.ErrnoException);
468
- }
469
- });
470
- }
471
-
472
- // --- copyFile ---
473
-
474
- export function copyFile(src: PathLike, dest: PathLike, callback: NoParamCallback): void;
475
- export function copyFile(src: PathLike, dest: PathLike, mode: number, callback: NoParamCallback): void;
476
- export function copyFile(src: PathLike, dest: PathLike, modeOrCb: number | NoParamCallback, maybeCb?: NoParamCallback): void {
477
- const mode = typeof modeOrCb === 'function' ? 0 : modeOrCb;
478
- const callback = typeof modeOrCb === 'function' ? modeOrCb : maybeCb!;
479
- Promise.resolve().then(() => {
480
- try {
481
- copyFileSync(src, dest, mode);
482
- callback(null);
483
- } catch (err: unknown) {
484
- callback(err as NodeJS.ErrnoException);
485
- }
486
- });
487
- }
488
-
489
- // --- access ---
490
-
491
- export function access(path: PathLike, callback: NoParamCallback): void;
492
- export function access(path: PathLike, mode: number, callback: NoParamCallback): void;
493
- export function access(path: PathLike, modeOrCb: number | NoParamCallback, maybeCb?: NoParamCallback): void {
494
- const mode = typeof modeOrCb === 'function' ? undefined : modeOrCb;
495
- const callback = typeof modeOrCb === 'function' ? modeOrCb : maybeCb!;
496
- Promise.resolve().then(() => {
497
- try {
498
- accessSync(path, mode);
499
- callback(null);
500
- } catch (err: unknown) {
501
- callback(err as NodeJS.ErrnoException);
502
- }
503
- });
504
- }
505
-
506
- // --- appendFile ---
507
-
508
- export function appendFile(path: PathLike, data: string | Uint8Array, callback: NoParamCallback): void;
509
- export function appendFile(path: PathLike, data: string | Uint8Array, options: { encoding?: string; mode?: number; flag?: string } | string, callback: NoParamCallback): void;
510
- export function appendFile(path: PathLike, data: string | Uint8Array, optsOrCb: { encoding?: string; mode?: number; flag?: string } | string | NoParamCallback, maybeCb?: NoParamCallback): void {
511
- const callback = typeof optsOrCb === 'function' ? optsOrCb : maybeCb!;
512
- const options = typeof optsOrCb === 'function' ? undefined : optsOrCb;
513
- Promise.resolve().then(() => {
514
- try {
515
- appendFileSync(path, data, options);
516
- callback(null);
517
- } catch (err: unknown) {
518
- callback(err as NodeJS.ErrnoException);
519
- }
520
- });
521
- }
522
-
523
- // --- readlink ---
524
-
525
- export function readlink(path: PathLike, callback: (err: NodeJS.ErrnoException | null, linkString: string) => void): void;
526
- export function readlink(path: PathLike, options: { encoding?: string } | string, callback: (err: NodeJS.ErrnoException | null, linkString: string | Buffer) => void): void;
527
- export function readlink(path: PathLike, optsOrCb: { encoding?: string } | string | ((err: NodeJS.ErrnoException | null, linkString: string | Buffer) => void), maybeCb?: (err: NodeJS.ErrnoException | null, linkString: string | Buffer) => void): void {
528
- const callback = typeof optsOrCb === 'function' ? optsOrCb : maybeCb!;
529
- const options = typeof optsOrCb === 'function' ? undefined : optsOrCb;
530
- Promise.resolve().then(() => {
531
- try {
532
- callback(null, readlinkSync(path, options));
533
- } catch (err: unknown) {
534
- callback(err as NodeJS.ErrnoException, '');
535
- }
536
- });
537
- }
538
-
539
- // --- truncate ---
540
-
541
- export function truncate(path: PathLike, callback: NoParamCallback): void;
542
- export function truncate(path: PathLike, len: number, callback: NoParamCallback): void;
543
- export function truncate(path: PathLike, lenOrCb: number | NoParamCallback, maybeCb?: NoParamCallback): void {
544
- const len = typeof lenOrCb === 'function' ? 0 : lenOrCb;
545
- const callback = typeof lenOrCb === 'function' ? lenOrCb : maybeCb!;
546
- Promise.resolve().then(() => {
547
- try {
548
- truncateSync(path, len);
549
- callback(null);
550
- } catch (err: unknown) {
551
- callback(err as NodeJS.ErrnoException);
552
- }
553
- });
554
- }
555
-
556
- // --- chmod ---
557
-
558
- export function chmod(path: PathLike, mode: Mode, callback: NoParamCallback): void {
559
- Promise.resolve().then(() => {
560
- try {
561
- chmodSync(path, mode);
562
- callback(null);
563
- } catch (err: unknown) {
564
- callback(err as NodeJS.ErrnoException);
565
- }
566
- });
567
- }
568
-
569
- // --- chown ---
570
-
571
- export function chown(path: PathLike, uid: number, gid: number, callback: NoParamCallback): void {
572
- Promise.resolve().then(() => {
573
- try {
574
- chownSync(path, uid, gid);
575
- callback(null);
576
- } catch (err: unknown) {
577
- callback(err as NodeJS.ErrnoException);
578
- }
579
- });
580
- }
581
-
582
- // --- mkdir (callback) ---
583
-
584
- export function mkdir(path: PathLike, callback: NoParamCallback): void;
585
- export function mkdir(path: PathLike, options: MakeDirectoryOptions | Mode, callback: NoParamCallback): void;
586
- export function mkdir(path: PathLike, optsOrCb: MakeDirectoryOptions | Mode | NoParamCallback, maybeCb?: NoParamCallback): void {
587
- const callback = typeof optsOrCb === 'function' ? optsOrCb : maybeCb!;
588
- const options = typeof optsOrCb === 'function' ? undefined : optsOrCb;
589
- Promise.resolve().then(() => {
590
- try {
591
- mkdirSync(path, options as MakeDirectoryOptions | Mode | undefined);
592
- callback(null);
593
- } catch (err: unknown) {
594
- callback(err as NodeJS.ErrnoException);
595
- }
596
- });
597
- }
598
-
599
- // --- rmdir (callback) ---
600
-
601
- export function rmdir(path: PathLike, callback: NoParamCallback): void;
602
- export function rmdir(path: PathLike, options: RmDirOptions, callback: NoParamCallback): void;
603
- export function rmdir(path: PathLike, optsOrCb: RmDirOptions | NoParamCallback, maybeCb?: NoParamCallback): void {
604
- const callback = typeof optsOrCb === 'function' ? optsOrCb : maybeCb!;
605
- const options = typeof optsOrCb === 'function' ? undefined : optsOrCb;
606
- Promise.resolve().then(() => {
607
- try {
608
- rmdirSync(path, options);
609
- callback(null);
610
- } catch (err: unknown) {
611
- callback(err as NodeJS.ErrnoException);
612
- }
613
- });
614
- }
615
-
616
- // --- readFile (callback) ---
617
-
618
- export function readFile(path: PathLike, callback: (err: NodeJS.ErrnoException | null, data: Buffer) => void): void;
619
- export function readFile(path: PathLike, options: { encoding?: string; flag?: string } | string, callback: (err: NodeJS.ErrnoException | null, data: string | Buffer) => void): void;
620
- export function readFile(path: PathLike, optsOrCb: { encoding?: string; flag?: string } | string | ((err: NodeJS.ErrnoException | null, data: Buffer) => void), maybeCb?: (err: NodeJS.ErrnoException | null, data: string | Buffer | null) => void): void {
621
- const callback = typeof optsOrCb === 'function' ? optsOrCb : maybeCb!;
622
- const options = typeof optsOrCb === 'function' ? undefined : optsOrCb;
623
- const pathStr = normalizePath(path);
624
- Promise.resolve().then(() => {
625
- try {
626
- const readOpts = typeof options === 'string' ? { encoding: options as string | null, flag: 'r' } : { encoding: (options?.encoding ?? null) as string | null, flag: options?.flag ?? 'r' };
627
- callback(null, readFileSync(pathStr, readOpts) as unknown as Buffer);
628
- } catch (err: unknown) {
629
- callback(err as NodeJS.ErrnoException, null as unknown as Buffer);
630
- }
631
- });
632
- }
633
-
634
- // --- writeFile (callback) ---
635
-
636
- export function writeFile(path: PathLike, data: string | Uint8Array, callback: NoParamCallback): void;
637
- export function writeFile(path: PathLike, data: string | Uint8Array, options: { encoding?: string; mode?: number; flag?: string } | string, callback: NoParamCallback): void;
638
- export function writeFile(path: PathLike, data: string | Uint8Array, optsOrCb: { encoding?: string; mode?: number; flag?: string } | string | NoParamCallback, maybeCb?: NoParamCallback): void {
639
- const callback = typeof optsOrCb === 'function' ? optsOrCb : maybeCb!;
640
- const pathStr = normalizePath(path);
641
- Promise.resolve().then(() => {
642
- try {
643
- writeFileSync(pathStr, data);
644
- callback(null);
645
- } catch (err: unknown) {
646
- callback(err as NodeJS.ErrnoException);
647
- }
648
- });
649
- }
650
-
651
- // --- link (callback) ---
652
-
653
- export function link(existingPath: PathLike, newPath: PathLike, callback: NoParamCallback): void {
654
- const existingStr = normalizePath(existingPath);
655
- const newStr = normalizePath(newPath);
656
- Promise.resolve().then(() => {
657
- try {
658
- const result = GLib.spawn_command_line_sync(`ln ${existingStr} ${newStr}`);
659
- if (!result[0]) {
660
- throw Object.assign(new Error(`EPERM: operation not permitted, link '${existingStr}' -> '${newStr}'`), {
661
- code: 'EPERM', errno: -1, syscall: 'link',
662
- path: existingStr, dest: newStr
663
- });
664
- }
665
- callback(null);
666
- } catch (err: unknown) {
667
- callback(err as NodeJS.ErrnoException);
668
- }
669
- });
670
- }
671
-
672
- // --- unlink (callback) ---
673
-
674
- export function unlink(path: PathLike, callback: NoParamCallback): void {
675
- const pathStr = normalizePath(path);
676
- Promise.resolve().then(() => {
677
- try {
678
- GLib.unlink(pathStr);
679
- callback(null);
680
- } catch (err: unknown) {
681
- callback(err as NodeJS.ErrnoException);
682
- }
683
- });
684
- }