@capsule-run/sdk 0.7.1 → 0.8.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.
Files changed (62) hide show
  1. package/README.md +5 -1
  2. package/dist/app.d.ts +7 -0
  3. package/dist/app.d.ts.map +1 -1
  4. package/dist/app.js +7 -0
  5. package/dist/app.js.map +1 -1
  6. package/dist/index.d.ts +1 -2
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +1 -2
  9. package/dist/index.js.map +1 -1
  10. package/dist/polyfills/fs.d.ts +148 -10
  11. package/dist/polyfills/fs.d.ts.map +1 -1
  12. package/dist/polyfills/fs.js +497 -17
  13. package/dist/polyfills/fs.js.map +1 -1
  14. package/dist/polyfills/process.d.ts +7 -2
  15. package/dist/polyfills/process.d.ts.map +1 -1
  16. package/dist/polyfills/process.js +78 -6
  17. package/dist/polyfills/process.js.map +1 -1
  18. package/dist/run.d.ts +4 -0
  19. package/dist/run.d.ts.map +1 -1
  20. package/dist/run.js +3 -2
  21. package/dist/run.js.map +1 -1
  22. package/dist/task.d.ts +11 -2
  23. package/dist/task.d.ts.map +1 -1
  24. package/dist/task.js +6 -3
  25. package/dist/task.js.map +1 -1
  26. package/package.json +4 -10
  27. package/src/app.ts +8 -0
  28. package/src/index.ts +1 -2
  29. package/src/polyfills/fs.ts +572 -20
  30. package/src/polyfills/process.ts +80 -6
  31. package/src/run.ts +7 -2
  32. package/src/task.ts +32 -14
  33. package/dist/polyfills/buffer.d.ts +0 -8
  34. package/dist/polyfills/buffer.d.ts.map +0 -1
  35. package/dist/polyfills/buffer.js +0 -9
  36. package/dist/polyfills/buffer.js.map +0 -1
  37. package/dist/polyfills/events.d.ts +0 -8
  38. package/dist/polyfills/events.d.ts.map +0 -1
  39. package/dist/polyfills/events.js +0 -9
  40. package/dist/polyfills/events.js.map +0 -1
  41. package/dist/polyfills/path.d.ts +0 -8
  42. package/dist/polyfills/path.d.ts.map +0 -1
  43. package/dist/polyfills/path.js +0 -8
  44. package/dist/polyfills/path.js.map +0 -1
  45. package/dist/polyfills/stream-web.d.ts +0 -74
  46. package/dist/polyfills/stream-web.d.ts.map +0 -1
  47. package/dist/polyfills/stream-web.js +0 -23
  48. package/dist/polyfills/stream-web.js.map +0 -1
  49. package/dist/polyfills/stream.d.ts +0 -16
  50. package/dist/polyfills/stream.d.ts.map +0 -1
  51. package/dist/polyfills/stream.js +0 -16
  52. package/dist/polyfills/stream.js.map +0 -1
  53. package/dist/polyfills/url.d.ts +0 -47
  54. package/dist/polyfills/url.d.ts.map +0 -1
  55. package/dist/polyfills/url.js +0 -68
  56. package/dist/polyfills/url.js.map +0 -1
  57. package/src/polyfills/buffer.ts +0 -11
  58. package/src/polyfills/events.ts +0 -11
  59. package/src/polyfills/path.ts +0 -21
  60. package/src/polyfills/stream-web.ts +0 -24
  61. package/src/polyfills/stream.ts +0 -28
  62. package/src/polyfills/url.ts +0 -73
@@ -8,11 +8,28 @@ declare const globalThis: {
8
8
  'wasi:filesystem/preopens': any;
9
9
  };
10
10
 
11
+ interface DescriptorStat {
12
+ size: bigint;
13
+ type?: string;
14
+ }
15
+
16
+ interface DirectoryEntry {
17
+ name: string;
18
+ type?: string;
19
+ }
20
+
21
+ interface DirectoryStream {
22
+ readDirectoryEntry(): DirectoryEntry | null;
23
+ }
24
+
11
25
  interface Descriptor {
12
26
  read(length: bigint, offset: bigint): [Uint8Array, boolean];
13
27
  write(buffer: Uint8Array, offset: bigint): bigint;
14
- stat(): { size: bigint };
15
- readDirectory(): any;
28
+ stat(): DescriptorStat;
29
+ readDirectory(): DirectoryStream;
30
+ unlinkFileAt(path: string): void;
31
+ removeDirectoryAt(path: string): void;
32
+ createDirectoryAt(path: string): void;
16
33
  openAt(
17
34
  pathFlags: { symlinkFollow?: boolean },
18
35
  path: string,
@@ -65,11 +82,14 @@ function resolvePath(path: string): { dir: Descriptor; relativePath: string } |
65
82
 
66
83
  const normalizedPath = normalizePath(path);
67
84
 
85
+ let catchAll: { dir: Descriptor; relativePath: string } | null = null;
86
+
68
87
  for (const { descriptor, guestPath } of preopens) {
69
88
  const normalizedGuest = normalizePath(guestPath);
70
89
 
71
90
  if (normalizedGuest === '.' || normalizedGuest === '') {
72
- return { dir: descriptor, relativePath: normalizedPath };
91
+ if (!catchAll) catchAll = { dir: descriptor, relativePath: normalizedPath };
92
+ continue;
73
93
  }
74
94
 
75
95
  if (normalizedPath.startsWith(normalizedGuest + '/')) {
@@ -82,7 +102,7 @@ function resolvePath(path: string): { dir: Descriptor; relativePath: string } |
82
102
  }
83
103
  }
84
104
 
85
- return { dir: preopens[0].descriptor, relativePath: normalizedPath };
105
+ return catchAll ?? { dir: preopens[0].descriptor, relativePath: normalizedPath };
86
106
  }
87
107
 
88
108
  /**
@@ -99,7 +119,7 @@ export async function readText(path: string): Promise<string> {
99
119
  export async function readBytes(path: string): Promise<Uint8Array> {
100
120
  const resolved = resolvePath(path);
101
121
  if (!resolved) {
102
- throw new Error("Filesystem not available.");
122
+ throw new Error("File not found.");
103
123
  }
104
124
 
105
125
  try {
@@ -130,13 +150,13 @@ export async function writeText(path: string, content: string): Promise<void> {
130
150
  export async function writeBytes(path: string, data: Uint8Array): Promise<void> {
131
151
  const resolved = resolvePath(path);
132
152
  if (!resolved) {
133
- throw new Error("Filesystem not available.");
153
+ throw new Error("File not found.");
134
154
  }
135
155
 
136
156
  try {
137
157
  const pathFlags = { symlinkFollow: false };
138
158
  const openFlags = { create: true, truncate: true };
139
- const descriptorFlags = { write: true };
159
+ const descriptorFlags = { write: true, mutateDirectory: true };
140
160
 
141
161
  const fd = resolved.dir.openAt(pathFlags, resolved.relativePath, openFlags, descriptorFlags);
142
162
  fd.write(data, BigInt(0));
@@ -151,7 +171,7 @@ export async function writeBytes(path: string, data: Uint8Array): Promise<void>
151
171
  export async function list(path: string = "."): Promise<string[]> {
152
172
  const resolved = resolvePath(path);
153
173
  if (!resolved) {
154
- throw new Error("Filesystem not available.");
174
+ throw new Error("Path not found.");
155
175
  }
156
176
 
157
177
  try {
@@ -276,12 +296,499 @@ export function readdir(
276
296
  .catch((err) => cb?.(err instanceof Error ? err : new Error(String(err))));
277
297
  }
278
298
 
299
+ // ---------------------------------------------------------------------------
300
+ // Sync implementations
301
+ // wasi:filesystem/types@0.2.0 descriptor methods (openAt, read, write, stat,
302
+ // readDirectory, etc.) are direct component-model imports — they are
303
+ // synchronous from JS's perspective. No asyncify, no event-loop blocking.
304
+ // ---------------------------------------------------------------------------
305
+
306
+ function enoent(path: string): Error {
307
+ return Object.assign(
308
+ new Error(`ENOENT: no such file or directory, open '${path}'`),
309
+ { code: 'ENOENT' }
310
+ );
311
+ }
312
+
313
+ /**
314
+ * Read file contents synchronously.
315
+ * Backed by wasi:filesystem/types@0.2.0 — fully synchronous.
316
+ */
317
+ export function readFileSync(path: string, options?: ReadFileOptions | Encoding): string | Uint8Array {
318
+ const resolved = resolvePath(path);
319
+ if (!resolved) throw enoent(path);
320
+
321
+ try {
322
+ const fd = resolved.dir.openAt({ symlinkFollow: false }, resolved.relativePath, {}, { read: true });
323
+ const stat = fd.stat();
324
+ const [data] = fd.read(stat.size, BigInt(0));
325
+ const encoding = typeof options === 'string' ? options : options?.encoding;
326
+ return (encoding === 'utf8' || encoding === 'utf-8') ? new TextDecoder().decode(data) : data;
327
+ } catch (e) {
328
+ if (e instanceof Error && (e as any).code) throw e;
329
+ throw Object.assign(new Error(`ENOENT: no such file or directory, open '${path}'`), { code: 'ENOENT' });
330
+ }
331
+ }
332
+
333
+ /**
334
+ * Write data to a file synchronously.
335
+ * Backed by wasi:filesystem/types@0.2.0 — fully synchronous.
336
+ */
337
+ export function writeFileSync(path: string, data: string | Uint8Array, _options?: WriteFileOptions | Encoding): void {
338
+ const resolved = resolvePath(path);
339
+ if (!resolved) throw enoent(path);
340
+
341
+ const bytes = typeof data === 'string' ? new TextEncoder().encode(data) : data;
342
+ try {
343
+ const fd = resolved.dir.openAt(
344
+ { symlinkFollow: false },
345
+ resolved.relativePath,
346
+ { create: true, truncate: true },
347
+ { write: true, mutateDirectory: true }
348
+ );
349
+ fd.write(bytes, BigInt(0));
350
+ } catch (e) {
351
+ throw new Error(`ENOENT: no such file or directory, open '${path}'`);
352
+ }
353
+ }
354
+
355
+ /**
356
+ * Append data to a file synchronously, creating it if it doesn't exist.
357
+ * Backed by wasi:filesystem/types@0.2.0 — fully synchronous.
358
+ */
359
+ export function appendFileSync(path: string, data: string | Uint8Array, _options?: WriteFileOptions | Encoding): void {
360
+ const resolved = resolvePath(path);
361
+ if (!resolved) throw enoent(path);
362
+
363
+ const bytes = typeof data === 'string' ? new TextEncoder().encode(data) : data;
364
+ try {
365
+ const fd = resolved.dir.openAt(
366
+ { symlinkFollow: false },
367
+ resolved.relativePath,
368
+ { create: true },
369
+ { write: true, mutateDirectory: true }
370
+ );
371
+ const stat = fd.stat();
372
+ fd.write(bytes, stat.size);
373
+ } catch (e) {
374
+ throw new Error(`Failed to append to file '${path}': ${e}`);
375
+ }
376
+ }
377
+
378
+ /**
379
+ * Read directory contents synchronously.
380
+ * Backed by wasi:filesystem/types@0.2.0 — fully synchronous.
381
+ */
382
+ export function readdirSync(path: string, _options?: any): string[] {
383
+ const resolved = resolvePath(path);
384
+ if (!resolved) throw enoent(path);
385
+
386
+ try {
387
+ let targetDir = resolved.dir;
388
+ if (resolved.relativePath !== '.') {
389
+ targetDir = resolved.dir.openAt(
390
+ { symlinkFollow: false },
391
+ resolved.relativePath,
392
+ { directory: true },
393
+ { read: true }
394
+ );
395
+ }
396
+ const stream = targetDir.readDirectory();
397
+ const entries: string[] = [];
398
+ let entry;
399
+ while ((entry = stream.readDirectoryEntry()) && entry) {
400
+ if (entry.name) entries.push(entry.name);
401
+ }
402
+ return entries;
403
+ } catch (e) {
404
+ throw Object.assign(new Error(`ENOENT: no such file or directory, scandir '${path}'`), { code: 'ENOENT' });
405
+ }
406
+ }
407
+
408
+ export interface StatResult {
409
+ isFile: () => boolean;
410
+ isDirectory: () => boolean;
411
+ isSymbolicLink: () => boolean;
412
+ size: number;
413
+ mtimeMs: number;
414
+ atimeMs: number;
415
+ ctimeMs: number;
416
+ mode: number;
417
+ }
418
+
419
+ function makeStatResult(type: 'file' | 'directory' | 'notfound', size: bigint = BigInt(0)): StatResult {
420
+ return {
421
+ isFile: () => type === 'file',
422
+ isDirectory: () => type === 'directory',
423
+ isSymbolicLink: () => false,
424
+ size: Number(size),
425
+ mtimeMs: 0,
426
+ atimeMs: 0,
427
+ ctimeMs: 0,
428
+ mode: type === 'directory' ? 0o40755 : 0o100644,
429
+ };
430
+ }
431
+
432
+ /**
433
+ * Get file stats synchronously.
434
+ * Backed by wasi:filesystem/types@0.2.0 — fully synchronous.
435
+ */
436
+ export function statSync(path: string): StatResult {
437
+ const resolved = resolvePath(path);
438
+ if (!resolved) throw enoent(path);
439
+
440
+ try {
441
+ const fd = resolved.dir.openAt({ symlinkFollow: false }, resolved.relativePath, {}, { read: true });
442
+ const s = fd.stat();
443
+ const type = s.type === 'directory' ? 'directory' : 'file';
444
+ return makeStatResult(type, s.size);
445
+ } catch (e) {
446
+ throw Object.assign(new Error(`ENOENT: no such file or directory, stat '${path}'`), { code: 'ENOENT' });
447
+ }
448
+ }
449
+
450
+ /**
451
+ * Get file stats synchronously (no symlink follow — same as stat in WASI 0.2).
452
+ */
453
+ export function lstatSync(path: string): StatResult {
454
+ return statSync(path);
455
+ }
456
+
457
+ /**
458
+ * Create a directory synchronously.
459
+ * Backed by wasi:filesystem/types@0.2.0 — fully synchronous.
460
+ */
461
+ export function mkdirSync(path: string, options?: MkdirOptions): void {
462
+ if (options?.recursive) {
463
+ const normalized = normalizePath(path);
464
+ const parts = normalized.split('/').filter(Boolean);
465
+ for (let i = 1; i <= parts.length; i++) {
466
+ const partial = parts.slice(0, i).join('/');
467
+ const resolved = resolvePath(partial);
468
+ if (!resolved) continue;
469
+ try { resolved.dir.createDirectoryAt(resolved.relativePath); } catch { /* already exists */ }
470
+ }
471
+ } else {
472
+ const resolved = resolvePath(path);
473
+ if (!resolved) throw enoent(path);
474
+ try {
475
+ resolved.dir.createDirectoryAt(resolved.relativePath);
476
+ } catch (e) {
477
+ throw Object.assign(new Error(`EEXIST: file already exists, mkdir '${path}'`), { code: 'EEXIST' });
478
+ }
479
+ }
480
+ }
481
+
482
+ /**
483
+ * Remove a directory synchronously.
484
+ * Backed by wasi:filesystem/types@0.2.0 — fully synchronous.
485
+ */
486
+ export function rmdirSync(path: string, _options?: RmdirOptions): void {
487
+ const resolved = resolvePath(path);
488
+ if (!resolved) throw enoent(path);
489
+ try {
490
+ resolved.dir.removeDirectoryAt(resolved.relativePath);
491
+ } catch (e) {
492
+ throw Object.assign(new Error(`ENOENT: no such file or directory, rmdir '${path}'`), { code: 'ENOENT' });
493
+ }
494
+ }
495
+
496
+ /**
497
+ * Remove a file or directory synchronously.
498
+ * Backed by wasi:filesystem/types@0.2.0 — fully synchronous.
499
+ */
500
+ export function rmSync(path: string, options?: RmOptions): void {
501
+ const resolved = resolvePath(path);
502
+ if (!resolved) {
503
+ if (options?.force) return;
504
+ throw enoent(path);
505
+ }
506
+
507
+ try {
508
+ const fd = resolved.dir.openAt({ symlinkFollow: false }, resolved.relativePath, {}, { read: true });
509
+ const s = fd.stat();
510
+ if (s.type === 'directory') {
511
+ if (!options?.recursive) {
512
+ throw Object.assign(
513
+ new Error(`EISDIR: illegal operation on a directory, rm '${path}'`),
514
+ { code: 'EISDIR' }
515
+ );
516
+ }
517
+ resolved.dir.removeDirectoryAt(resolved.relativePath);
518
+ } else {
519
+ resolved.dir.unlinkFileAt(resolved.relativePath);
520
+ }
521
+ } catch (e) {
522
+ if (options?.force) return;
523
+ if (e instanceof Error && (e as any).code) throw e;
524
+ throw enoent(path);
525
+ }
526
+ }
527
+
528
+ /**
529
+ * Remove a file synchronously.
530
+ * Backed by wasi:filesystem/types@0.2.0 — fully synchronous.
531
+ */
532
+ export function unlinkSync(path: string): void {
533
+ const resolved = resolvePath(path);
534
+ if (!resolved) throw enoent(path);
535
+ try {
536
+ resolved.dir.unlinkFileAt(resolved.relativePath);
537
+ } catch (e) {
538
+ throw Object.assign(new Error(`ENOENT: no such file or directory, unlink '${path}'`), { code: 'ENOENT' });
539
+ }
540
+ }
541
+
542
+ /**
543
+ * Copy a file synchronously.
544
+ * Backed by wasi:filesystem/types@0.2.0 — fully synchronous.
545
+ */
546
+ export function copyFileSync(src: string, dest: string): void {
547
+ const data = readFileSync(src) as Uint8Array;
548
+ writeFileSync(dest, data);
549
+ }
550
+
551
+ /**
552
+ * Rename a file or directory synchronously.
553
+ * Falls back to copy+delete since wasi:filesystem/types@0.2.0
554
+ * does not expose a rename/link-at in the current WIT binding.
555
+ */
556
+ export function renameSync(oldPath: string, newPath: string): void {
557
+ try {
558
+ const data = readFileSync(oldPath);
559
+ writeFileSync(newPath, data as Uint8Array);
560
+ unlinkSync(oldPath);
561
+ } catch (e) {
562
+ throw Object.assign(
563
+ new Error(`ENOENT: no such file or directory, rename '${oldPath}' -> '${newPath}'`),
564
+ { code: 'ENOENT' }
565
+ );
566
+ }
567
+ }
568
+
569
+ /**
570
+ * Check file accessibility synchronously.
571
+ * Throws if the path does not exist.
572
+ */
573
+ export function accessSync(path: string, _mode?: number): void {
574
+ const resolved = resolvePath(path);
575
+ if (!resolved) throw enoent(path);
576
+ try {
577
+ resolved.dir.openAt({ symlinkFollow: false }, resolved.relativePath, {}, { read: true });
578
+ } catch (e) {
579
+ throw Object.assign(new Error(`ENOENT: no such file or directory, access '${path}'`), { code: 'ENOENT' });
580
+ }
581
+ }
582
+
583
+ /**
584
+ * Check if a path exists synchronously.
585
+ * Backed by wasi:filesystem/types@0.2.0 — fully synchronous.
586
+ */
587
+ export function existsSync(path: string): boolean {
588
+ const resolved = resolvePath(path);
589
+ if (!resolved) return false;
590
+ try {
591
+ resolved.dir.openAt({ symlinkFollow: false }, resolved.relativePath, {}, { read: true });
592
+ return true;
593
+ } catch {
594
+ return false;
595
+ }
596
+ }
597
+
598
+ /**
599
+ * Delete a file.
600
+ */
601
+ export async function unlink(path: string): Promise<void> {
602
+ const resolved = resolvePath(path);
603
+ if (!resolved) {
604
+ throw new Error("File not found.");
605
+ }
606
+
607
+ const fs = getFsBindings();
608
+ if (!fs) {
609
+ throw new Error("File not found.");
610
+ }
611
+
612
+ try {
613
+ resolved.dir.unlinkFileAt(resolved.relativePath);
614
+ } catch (e) {
615
+ throw new Error(`Failed to delete file '${path}': ${e}`);
616
+ }
617
+ }
618
+
619
+ /**
620
+ * Returns 'file', 'directory', or 'notfound' for a given path.
621
+ */
622
+ async function statPath(path: string): Promise<'file' | 'directory' | 'notfound'> {
623
+ const resolved = resolvePath(path);
624
+ if (!resolved) return 'notfound';
625
+
626
+ try {
627
+ const fd = resolved.dir.openAt({ symlinkFollow: false }, resolved.relativePath, {}, { read: true });
628
+ const s = fd.stat();
629
+ if (s.type === 'directory') return 'directory';
630
+ return 'file';
631
+ } catch {
632
+ return 'notfound';
633
+ }
634
+ }
635
+
636
+ /**
637
+ * Recursively delete a directory and all its contents.
638
+ */
639
+ async function removeRecursive(path: string): Promise<void> {
640
+ const resolved = resolvePath(path);
641
+ if (!resolved) throw new Error(`Path not found: '${path}'`);
642
+
643
+ const fd = resolved.dir.openAt(
644
+ { symlinkFollow: false },
645
+ resolved.relativePath,
646
+ { directory: true },
647
+ { read: true, mutateDirectory: true }
648
+ );
649
+
650
+ const stream = fd.readDirectory();
651
+ let entry: DirectoryEntry | null | undefined;
652
+
653
+ while ((entry = stream.readDirectoryEntry()) && entry) {
654
+ if (!entry.name) continue;
655
+ const childPath = path.replace(/\/$/, '') + '/' + entry.name;
656
+ if (entry.type === 'directory') {
657
+ await removeRecursive(childPath);
658
+ } else {
659
+ await unlink(childPath);
660
+ }
661
+ }
662
+
663
+ resolved.dir.removeDirectoryAt(resolved.relativePath);
664
+ }
665
+
666
+ export interface RmdirOptions {
667
+ recursive?: boolean;
668
+ }
669
+
670
+ /**
671
+ * Delete a directory. Pass `{ recursive: true }` to remove it and all its contents.
672
+ */
673
+ export async function rmdir(path: string, options?: RmdirOptions): Promise<void> {
674
+ const resolved = resolvePath(path);
675
+ if (!resolved) {
676
+ throw new Error("Folder not found.");
677
+ }
678
+
679
+ try {
680
+ if (options?.recursive) {
681
+ await removeRecursive(path);
682
+ } else {
683
+ resolved.dir.removeDirectoryAt(resolved.relativePath);
684
+ }
685
+ } catch (e) {
686
+ if (e instanceof Error && e.message.startsWith('Failed to remove')) throw e;
687
+ throw new Error(`Failed to remove directory '${path}': ${e}`);
688
+ }
689
+ }
690
+
691
+ export interface RmOptions {
692
+ recursive?: boolean;
693
+ force?: boolean;
694
+ }
695
+
696
+ /**
697
+ * Remove a file or directory. Supports `{ recursive, force }` options.
698
+ */
699
+ export async function rm(path: string, options?: RmOptions): Promise<void> {
700
+ const kind = await statPath(path);
701
+
702
+ if (kind === 'notfound') {
703
+ if (options?.force) return;
704
+ throw new Error(`ENOENT: no such file or directory, rm '${path}'`);
705
+ }
706
+
707
+ if (kind === 'directory') {
708
+ if (!options?.recursive) {
709
+ throw new Error(`EISDIR: illegal operation on a directory, rm '${path}' (use { recursive: true })`);
710
+ }
711
+ await removeRecursive(path);
712
+ } else {
713
+ await unlink(path);
714
+ }
715
+ }
716
+
717
+ export interface MkdirOptions {
718
+ recursive?: boolean;
719
+ }
720
+
279
721
  /**
280
- * Check if file/directory exists (sync-style, limited in WASM)
722
+ * Create a directory. Pass `{ recursive: true }` to create intermediate directories.
281
723
  */
282
- export function existsSync(_path: string): boolean {
283
- console.warn('fs.existsSync: Cannot implement true sync in WASM. Use fs.access instead.');
284
- return false;
724
+ export async function mkdir(path: string, options?: MkdirOptions): Promise<void> {
725
+ if (options?.recursive) {
726
+ const normalized = normalizePath(path);
727
+ const parts = normalized.split('/').filter(Boolean);
728
+ for (let i = 1; i <= parts.length; i++) {
729
+ const partial = parts.slice(0, i).join('/');
730
+ const resolved = resolvePath(partial);
731
+ if (!resolved) continue;
732
+ try {
733
+ resolved.dir.createDirectoryAt(resolved.relativePath);
734
+ } catch {
735
+ // Directory may already exist, continue
736
+ }
737
+ }
738
+ } else {
739
+ const resolved = resolvePath(path);
740
+ if (!resolved) throw new Error(`Cannot resolve path: '${path}'`);
741
+ try {
742
+ resolved.dir.createDirectoryAt(resolved.relativePath);
743
+ } catch (e) {
744
+ throw new Error(`Failed to create directory '${path}': ${e}`);
745
+ }
746
+ }
747
+ }
748
+
749
+ /**
750
+ * Copy a file from src to dest.
751
+ */
752
+ export async function copyFile(src: string, dest: string): Promise<void> {
753
+ const data = await readBytes(src);
754
+ await writeBytes(dest, data);
755
+ }
756
+
757
+ async function copyDirRecursive(src: string, dest: string): Promise<void> {
758
+ await mkdir(dest, { recursive: true });
759
+ const entries = await list(src);
760
+ for (const entry of entries) {
761
+ const srcEntry = src.replace(/\/$/, '') + '/' + entry;
762
+ const destEntry = dest.replace(/\/$/, '') + '/' + entry;
763
+ const kind = await statPath(srcEntry);
764
+ if (kind === 'directory') {
765
+ await copyDirRecursive(srcEntry, destEntry);
766
+ } else {
767
+ await copyFile(srcEntry, destEntry);
768
+ }
769
+ }
770
+ }
771
+
772
+ export interface CpOptions {
773
+ recursive?: boolean;
774
+ }
775
+
776
+ /**
777
+ * Copy a file or directory. Pass `{ recursive: true }` to copy directories.
778
+ */
779
+ export async function cp(src: string, dest: string, options?: CpOptions): Promise<void> {
780
+ const kind = await statPath(src);
781
+ if (kind === 'notfound') {
782
+ throw new Error(`ENOENT: no such file or directory '${src}'`);
783
+ }
784
+ if (kind === 'directory') {
785
+ if (!options?.recursive) {
786
+ throw new Error(`EISDIR: illegal operation on a directory '${src}' (use { recursive: true })`);
787
+ }
788
+ await copyDirRecursive(src, dest);
789
+ } else {
790
+ await copyFile(src, dest);
791
+ }
285
792
  }
286
793
 
287
794
  /**
@@ -313,23 +820,68 @@ export const promises = {
313
820
  }
314
821
  },
315
822
 
316
- async stat(path: string): Promise<{ isFile: () => boolean; isDirectory: () => boolean }> {
317
- const fileExists = await exists(path);
318
- if (!fileExists) {
319
- throw new Error(`ENOENT: no such file or directory, stat '${path}'`);
823
+ async unlink(path: string): Promise<void> {
824
+ await unlink(path);
825
+ },
826
+
827
+ async stat(path: string): Promise<StatResult> {
828
+ const kind = await statPath(path);
829
+ if (kind === 'notfound') {
830
+ throw Object.assign(
831
+ new Error(`ENOENT: no such file or directory, stat '${path}'`),
832
+ { code: 'ENOENT' }
833
+ );
320
834
  }
321
- return {
322
- isFile: () => true,
323
- isDirectory: () => false,
324
- };
835
+ return makeStatResult(kind);
836
+ },
837
+
838
+ async rmdir(path: string, options?: RmdirOptions): Promise<void> {
839
+ await rmdir(path, options);
840
+ },
841
+
842
+ async rm(path: string, options?: RmOptions): Promise<void> {
843
+ await rm(path, options);
844
+ },
845
+
846
+ async mkdir(path: string, options?: MkdirOptions): Promise<void> {
847
+ await mkdir(path, options);
848
+ },
849
+
850
+ async copyFile(src: string, dest: string): Promise<void> {
851
+ await copyFile(src, dest);
852
+ },
853
+
854
+ async cp(src: string, dest: string, options?: CpOptions): Promise<void> {
855
+ await cp(src, dest, options);
325
856
  },
326
857
  };
327
858
 
328
859
  const fs = {
860
+ // Async / callback
329
861
  readFile,
330
862
  writeFile,
331
863
  readdir,
864
+ unlink,
865
+ rmdir,
866
+ rm,
867
+ mkdir,
868
+ copyFile,
869
+ cp,
870
+ readFileSync,
871
+ writeFileSync,
872
+ appendFileSync,
873
+ readdirSync,
874
+ statSync,
875
+ lstatSync,
876
+ mkdirSync,
877
+ rmdirSync,
878
+ rmSync,
879
+ unlinkSync,
880
+ copyFileSync,
881
+ renameSync,
882
+ accessSync,
332
883
  existsSync,
884
+ // Promises API
333
885
  promises,
334
886
  };
335
887