@anabranch/fs 0.1.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 (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +45 -0
  3. package/esm/_dnt.polyfills.d.ts +7 -0
  4. package/esm/_dnt.polyfills.d.ts.map +1 -0
  5. package/esm/_dnt.polyfills.js +1 -0
  6. package/esm/anabranch/index.d.ts +44 -0
  7. package/esm/anabranch/index.d.ts.map +1 -0
  8. package/esm/anabranch/index.js +41 -0
  9. package/esm/anabranch/streams/channel.d.ts +15 -0
  10. package/esm/anabranch/streams/channel.d.ts.map +1 -0
  11. package/esm/anabranch/streams/channel.js +122 -0
  12. package/esm/anabranch/streams/source.d.ts +68 -0
  13. package/esm/anabranch/streams/source.d.ts.map +1 -0
  14. package/esm/anabranch/streams/source.js +72 -0
  15. package/esm/anabranch/streams/stream.d.ts +431 -0
  16. package/esm/anabranch/streams/stream.d.ts.map +1 -0
  17. package/esm/anabranch/streams/stream.js +625 -0
  18. package/esm/anabranch/streams/task.d.ts +117 -0
  19. package/esm/anabranch/streams/task.d.ts.map +1 -0
  20. package/esm/anabranch/streams/task.js +416 -0
  21. package/esm/anabranch/streams/util.d.ts +33 -0
  22. package/esm/anabranch/streams/util.d.ts.map +1 -0
  23. package/esm/anabranch/streams/util.js +18 -0
  24. package/esm/fs/dir.d.ts +17 -0
  25. package/esm/fs/dir.d.ts.map +1 -0
  26. package/esm/fs/dir.js +90 -0
  27. package/esm/fs/errors.d.ts +64 -0
  28. package/esm/fs/errors.d.ts.map +1 -0
  29. package/esm/fs/errors.js +125 -0
  30. package/esm/fs/glob_match.d.ts +15 -0
  31. package/esm/fs/glob_match.d.ts.map +1 -0
  32. package/esm/fs/glob_match.js +73 -0
  33. package/esm/fs/index.d.ts +38 -0
  34. package/esm/fs/index.d.ts.map +1 -0
  35. package/esm/fs/index.js +31 -0
  36. package/esm/fs/read.d.ts +22 -0
  37. package/esm/fs/read.d.ts.map +1 -0
  38. package/esm/fs/read.js +75 -0
  39. package/esm/fs/types.d.ts +67 -0
  40. package/esm/fs/types.d.ts.map +1 -0
  41. package/esm/fs/types.js +1 -0
  42. package/esm/fs/watch.d.ts +17 -0
  43. package/esm/fs/watch.d.ts.map +1 -0
  44. package/esm/fs/watch.js +37 -0
  45. package/esm/fs/write.d.ts +16 -0
  46. package/esm/fs/write.d.ts.map +1 -0
  47. package/esm/fs/write.js +42 -0
  48. package/esm/package.json +3 -0
  49. package/package.json +24 -0
@@ -0,0 +1,37 @@
1
+ import { existsSync, watch as fsWatch } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { Channel } from "../anabranch/index.js";
5
+ import { nodeErrorToFSError, } from "./errors.js";
6
+ /**
7
+ * Watches `path` for file-system changes, yielding a {@link FsEvent} for each.
8
+ *
9
+ * Uses a push-based queue internally so events are never dropped between yields.
10
+ * Cancel the stream to stop watching (e.g. via `take`, breaking `for await`, or
11
+ * returning from the generator).
12
+ *
13
+ * Note: on some platforms `rename` events cannot distinguish creation from
14
+ * deletion without a stat check. This implementation infers the kind by
15
+ * checking whether the file still exists at event time.
16
+ */
17
+ export function watch(path, options) {
18
+ const watchPath = path instanceof URL ? fileURLToPath(path) : path;
19
+ const watcher = fsWatch(path, { recursive: options?.recursive ?? true, persistent: false }, (eventType, filename) => {
20
+ const name = filename;
21
+ const fullPath = name ? join(watchPath, name) : watchPath;
22
+ let kind;
23
+ if (eventType === "rename") {
24
+ kind = existsSync(fullPath) ? "create" : "remove";
25
+ }
26
+ else {
27
+ kind = "modify";
28
+ }
29
+ channel.send({ kind, paths: [fullPath] });
30
+ });
31
+ const channel = new Channel({
32
+ onClose: () => watcher.close(),
33
+ });
34
+ watcher.once("error", (err) => channel.fail(nodeErrorToFSError(err, path)));
35
+ watcher.once("close", () => channel.close());
36
+ return channel;
37
+ }
@@ -0,0 +1,16 @@
1
+ import { Task } from "../anabranch/index.js";
2
+ import { type IsDirectory, type NotFound, type PermissionDenied, type Unknown, type WriteError } from "./errors.js";
3
+ /**
4
+ * Writes a UTF-8 string to a file, creating or overwriting it.
5
+ */
6
+ export declare function writeTextFile(path: string | URL, content: string): Task<void, WriteFileError>;
7
+ /**
8
+ * Writes a `Uint8Array` to a file, creating or overwriting it.
9
+ */
10
+ export declare function writeFile(path: string | URL, data: Uint8Array): Task<void, WriteFileError>;
11
+ /**
12
+ * Serialises `value` as JSON and writes it to a file, creating or overwriting it.
13
+ */
14
+ export declare function writeJson(path: string | URL, value: unknown): Task<void, WriteFileError>;
15
+ export type WriteFileError = NotFound | IsDirectory | PermissionDenied | WriteError | Unknown;
16
+ //# sourceMappingURL=write.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"write.d.ts","sourceRoot":"","sources":["../../src/fs/write.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EACL,KAAK,WAAW,EAEhB,KAAK,QAAQ,EACb,KAAK,gBAAgB,EACrB,KAAK,OAAO,EACZ,KAAK,UAAU,EAChB,MAAM,aAAa,CAAC;AAErB;;GAEG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,GAAG,GAAG,EAClB,OAAO,EAAE,MAAM,GACd,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC,CAQ5B;AAED;;GAEG;AACH,wBAAgB,SAAS,CACvB,IAAI,EAAE,MAAM,GAAG,GAAG,EAClB,IAAI,EAAE,UAAU,GACf,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC,CAQ5B;AAED;;GAEG;AACH,wBAAgB,SAAS,CACvB,IAAI,EAAE,MAAM,GAAG,GAAG,EAClB,KAAK,EAAE,OAAO,GACb,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC,CAQ5B;AAED,MAAM,MAAM,cAAc,GACtB,QAAQ,GACR,WAAW,GACX,gBAAgB,GAChB,UAAU,GACV,OAAO,CAAC"}
@@ -0,0 +1,42 @@
1
+ import { writeFile as fsWriteFile } from "node:fs/promises";
2
+ import { Task } from "../anabranch/index.js";
3
+ import { nodeErrorToFSError, } from "./errors.js";
4
+ /**
5
+ * Writes a UTF-8 string to a file, creating or overwriting it.
6
+ */
7
+ export function writeTextFile(path, content) {
8
+ return Task.of(async () => {
9
+ try {
10
+ await fsWriteFile(path, content, "utf8");
11
+ }
12
+ catch (error) {
13
+ throw nodeErrorToFSError(error, path);
14
+ }
15
+ });
16
+ }
17
+ /**
18
+ * Writes a `Uint8Array` to a file, creating or overwriting it.
19
+ */
20
+ export function writeFile(path, data) {
21
+ return Task.of(async () => {
22
+ try {
23
+ await fsWriteFile(path, data);
24
+ }
25
+ catch (error) {
26
+ throw nodeErrorToFSError(error, path);
27
+ }
28
+ });
29
+ }
30
+ /**
31
+ * Serialises `value` as JSON and writes it to a file, creating or overwriting it.
32
+ */
33
+ export function writeJson(path, value) {
34
+ return Task.of(async () => {
35
+ try {
36
+ await fsWriteFile(path, JSON.stringify(value), "utf8");
37
+ }
38
+ catch (error) {
39
+ throw nodeErrorToFSError(error, path);
40
+ }
41
+ });
42
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@anabranch/fs",
3
+ "version": "0.1.0",
4
+ "description": "Streaming file-system utilities for the anabranch ecosystem",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/frodi-karlsson/anabranch.git"
8
+ },
9
+ "license": "MIT",
10
+ "bugs": {
11
+ "url": "https://github.com/frodi-karlsson/anabranch/issues"
12
+ },
13
+ "module": "./esm/fs/index.js",
14
+ "exports": {
15
+ ".": {
16
+ "import": "./esm/fs/index.js"
17
+ }
18
+ },
19
+ "scripts": {},
20
+ "dependencies": {
21
+ "anabranch": "^0"
22
+ },
23
+ "_generatedBy": "dnt@dev"
24
+ }