@anabranch/fs 0.1.15 → 0.2.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.
package/README.md CHANGED
@@ -7,21 +7,21 @@ operations return a `Source` for streaming; single-value operations return a
7
7
  ## Usage
8
8
 
9
9
  ```ts
10
- import { glob, readLines } from "@anabranch/fs";
10
+ import { glob, readLines } from '@anabranch/fs'
11
11
 
12
- const { successes, errors } = await glob("./src", "**/*.ts")
12
+ const { successes, errors } = await glob('./src', '**/*.ts')
13
13
  .flatMap(async (entry) => {
14
14
  const lines = await readLines(entry.path)
15
- .filter((line) => line.includes("TODO"))
15
+ .filter((line) => line.includes('TODO'))
16
16
  .map((line) => ({ path: entry.path, line }))
17
- .collect();
18
- return lines;
17
+ .collect()
18
+ return lines
19
19
  })
20
- .partition();
20
+ .partition()
21
21
 
22
- console.log("TODOs found:", successes.length);
22
+ console.log('TODOs found:', successes.length)
23
23
  for (const error of errors) {
24
- console.error("Failed:", error);
24
+ console.error('Failed:', error)
25
25
  }
26
26
  ```
27
27
 
@@ -30,7 +30,7 @@ for (const error of errors) {
30
30
  **Deno (JSR)**
31
31
 
32
32
  ```ts
33
- import { glob } from "jsr:@anabranch/fs";
33
+ import { glob } from 'jsr:@anabranch/fs'
34
34
  ```
35
35
 
36
36
  **Node / Bun (npm)**
package/esm/dir.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { Source } from "anabranch";
2
- import type { DirEntry, GlobOptions, WalkEntry, WalkOptions } from "./types.js";
3
- import { type NotDirectory, type NotFound, type PermissionDenied, type Unknown } from "./errors.js";
1
+ import { Source } from 'anabranch';
2
+ import type { DirEntry, GlobOptions, WalkEntry, WalkOptions } from './types.js';
3
+ import { type NotDirectory, type NotFound, type PermissionDenied, type Unknown } from './errors.js';
4
4
  /**
5
5
  * Lists the immediate children of a directory.
6
6
  */
package/esm/dir.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"dir.d.ts","source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,QAAQ,EAAE,QAAQ,CAAC,CAgBtE;AAED;;GAEG;AACH,wBAAgB,IAAI,CAClB,IAAI,EAAE,MAAM,GAAG,GAAG,EAClB,OAAO,CAAC,EAAE,WAAW,GACpB,MAAM,CAAC,SAAS,EAAE,QAAQ,CAAC,CAiE7B;AAED;;GAEG;AACH,wBAAgB,IAAI,CAClB,IAAI,EAAE,MAAM,GAAG,GAAG,EAClB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,WAAW,GACpB,MAAM,CAAC,SAAS,EAAE,QAAQ,CAAC,CAG7B;AAED,sDAAsD;AACtD,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,YAAY,GAAG,gBAAgB,GAAG,OAAO,CAAA"}
package/esm/dir.js CHANGED
@@ -1,9 +1,9 @@
1
- import { readdir } from "node:fs/promises";
2
- import { join, relative } from "node:path";
3
- import { fileURLToPath } from "node:url";
4
- import { Source } from "anabranch";
5
- import { _matchGlob } from "./glob_match.js";
6
- import { nodeErrorToFSError, } from "./errors.js";
1
+ import { readdir } from 'node:fs/promises';
2
+ import { join, relative } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { Source } from 'anabranch';
5
+ import { _matchGlob } from './glob_match.js';
6
+ import { nodeErrorToFSError, } from './errors.js';
7
7
  /**
8
8
  * Lists the immediate children of a directory.
9
9
  */
@@ -49,7 +49,7 @@ export function walk(root, options) {
49
49
  }
50
50
  for (const entry of entries) {
51
51
  const entryPath = join(dirPath, entry.name);
52
- let relPath = relative(rootPath, entryPath).replace(/\\/g, "/");
52
+ let relPath = relative(rootPath, entryPath).replace(/\\/g, '/');
53
53
  const isFile = entry.isFile();
54
54
  const isDirectory = entry.isDirectory();
55
55
  const isSymlink = entry.isSymbolicLink();
package/esm/errors.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * @example
5
5
  * ```ts
6
- * import { readTextFile, FSError, FSErrors } from "@anabranch/fs";
6
+ * import { readTextFile, FSError } from "@anabranch/fs";
7
7
  *
8
8
  * const result = await readTextFile("./config.json").run();
9
9
  * if (result.type === "error") {
@@ -20,54 +20,50 @@ export declare class FSError extends Error {
20
20
  constructor(kind: string, path: string | URL, message: string);
21
21
  }
22
22
  export declare class NotFound extends FSError {
23
+ name: "NotFound";
23
24
  constructor(path: string | URL, message: string);
24
25
  }
25
26
  /** File already exists when it should not. */
26
27
  export declare class AlreadyExists extends FSError {
28
+ name: "AlreadyExists";
27
29
  constructor(path: string | URL, message: string);
28
30
  }
29
31
  /** Path is a directory when a file was expected. */
30
32
  export declare class IsDirectory extends FSError {
33
+ name: "IsDirectory";
31
34
  constructor(path: string | URL, message: string);
32
35
  }
33
36
  /** Path is not a directory when a directory was expected. */
34
37
  export declare class NotDirectory extends FSError {
38
+ name: "NotDirectory";
35
39
  constructor(path: string | URL, message: string);
36
40
  }
37
41
  /** Permission denied accessing the file system. */
38
42
  export declare class PermissionDenied extends FSError {
43
+ name: "PermissionDenied";
39
44
  constructor(path: string | URL, message: string);
40
45
  }
41
46
  /** Error reading from the file system. */
42
47
  export declare class ReadError extends FSError {
48
+ name: "ReadError";
43
49
  constructor(path: string | URL, message: string);
44
50
  }
45
51
  /** Error writing to the file system. */
46
52
  export declare class WriteError extends FSError {
53
+ name: "WriteError";
47
54
  constructor(path: string | URL, message: string);
48
55
  }
49
56
  /** File content is invalid (e.g., malformed JSON). */
50
57
  export declare class InvalidData extends FSError {
51
58
  readonly cause?: unknown;
59
+ name: "InvalidData";
52
60
  constructor(path: string | URL, message: string, cause?: unknown);
53
61
  }
54
62
  /** Unknown file system error. */
55
63
  export declare class Unknown extends FSError {
56
64
  readonly cause?: unknown;
65
+ name: "Unknown";
57
66
  constructor(path: string | URL, message: string, cause?: unknown);
58
67
  }
59
- /** Registry of file-system error constructors for `instanceof` checks. */
60
- export declare const FSErrors: {
61
- readonly NotFound: typeof NotFound;
62
- readonly AlreadyExists: typeof AlreadyExists;
63
- readonly IsDirectory: typeof IsDirectory;
64
- readonly NotDirectory: typeof NotDirectory;
65
- readonly PermissionDenied: typeof PermissionDenied;
66
- readonly ReadError: typeof ReadError;
67
- readonly WriteError: typeof WriteError;
68
- readonly InvalidData: typeof InvalidData;
69
- readonly Unknown: typeof Unknown;
70
- };
71
- declare function nodeErrorToFSError(error: unknown, path: string | URL): FSError;
72
- export { nodeErrorToFSError };
68
+ export declare function nodeErrorToFSError(error: unknown, path: string | URL): FSError;
73
69
  //# sourceMappingURL=errors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,OAAQ,SAAQ,KAAK;IAChC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC;gBAEhB,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,GAAG,EAAE,OAAO,EAAE,MAAM;CAK9D;AAED,qBAAa,QAAS,SAAQ,OAAO;gBACvB,IAAI,EAAE,MAAM,GAAG,GAAG,EAAE,OAAO,EAAE,MAAM;CAGhD;AAED,8CAA8C;AAC9C,qBAAa,aAAc,SAAQ,OAAO;gBAC5B,IAAI,EAAE,MAAM,GAAG,GAAG,EAAE,OAAO,EAAE,MAAM;CAGhD;AAED,oDAAoD;AACpD,qBAAa,WAAY,SAAQ,OAAO;gBAC1B,IAAI,EAAE,MAAM,GAAG,GAAG,EAAE,OAAO,EAAE,MAAM;CAGhD;AAED,6DAA6D;AAC7D,qBAAa,YAAa,SAAQ,OAAO;gBAC3B,IAAI,EAAE,MAAM,GAAG,GAAG,EAAE,OAAO,EAAE,MAAM;CAGhD;AAED,mDAAmD;AACnD,qBAAa,gBAAiB,SAAQ,OAAO;gBAC/B,IAAI,EAAE,MAAM,GAAG,GAAG,EAAE,OAAO,EAAE,MAAM;CAGhD;AAED,0CAA0C;AAC1C,qBAAa,SAAU,SAAQ,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EAAE,MAAM,GAAG,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAIjE;AAED,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,EACd,IAAI,EAAE,MAAM,GAAG,GAAG,GACjB,OAAO,CAmBT"}
package/esm/errors.js CHANGED
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * @example
5
5
  * ```ts
6
- * import { readTextFile, FSError, FSErrors } from "@anabranch/fs";
6
+ * import { readTextFile, FSError } from "@anabranch/fs";
7
7
  *
8
8
  * const result = await readTextFile("./config.json").run();
9
9
  * if (result.type === "error") {
@@ -35,100 +35,141 @@ export class FSError extends Error {
35
35
  }
36
36
  export class NotFound extends FSError {
37
37
  constructor(path, message) {
38
- super("NotFound", path, message);
38
+ super('NotFound', path, message);
39
+ Object.defineProperty(this, "name", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: 'NotFound'
44
+ });
39
45
  }
40
46
  }
41
47
  /** File already exists when it should not. */
42
48
  export class AlreadyExists extends FSError {
43
49
  constructor(path, message) {
44
- super("AlreadyExists", path, message);
50
+ super('AlreadyExists', path, message);
51
+ Object.defineProperty(this, "name", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: 'AlreadyExists'
56
+ });
45
57
  }
46
58
  }
47
59
  /** Path is a directory when a file was expected. */
48
60
  export class IsDirectory extends FSError {
49
61
  constructor(path, message) {
50
- super("IsDirectory", path, message);
62
+ super('IsDirectory', path, message);
63
+ Object.defineProperty(this, "name", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: 'IsDirectory'
68
+ });
51
69
  }
52
70
  }
53
71
  /** Path is not a directory when a directory was expected. */
54
72
  export class NotDirectory extends FSError {
55
73
  constructor(path, message) {
56
- super("NotDirectory", path, message);
74
+ super('NotDirectory', path, message);
75
+ Object.defineProperty(this, "name", {
76
+ enumerable: true,
77
+ configurable: true,
78
+ writable: true,
79
+ value: 'NotDirectory'
80
+ });
57
81
  }
58
82
  }
59
83
  /** Permission denied accessing the file system. */
60
84
  export class PermissionDenied extends FSError {
61
85
  constructor(path, message) {
62
- super("PermissionDenied", path, message);
86
+ super('PermissionDenied', path, message);
87
+ Object.defineProperty(this, "name", {
88
+ enumerable: true,
89
+ configurable: true,
90
+ writable: true,
91
+ value: 'PermissionDenied'
92
+ });
63
93
  }
64
94
  }
65
95
  /** Error reading from the file system. */
66
96
  export class ReadError extends FSError {
67
97
  constructor(path, message) {
68
- super("ReadError", path, message);
98
+ super('ReadError', path, message);
99
+ Object.defineProperty(this, "name", {
100
+ enumerable: true,
101
+ configurable: true,
102
+ writable: true,
103
+ value: 'ReadError'
104
+ });
69
105
  }
70
106
  }
71
107
  /** Error writing to the file system. */
72
108
  export class WriteError extends FSError {
73
109
  constructor(path, message) {
74
- super("WriteError", path, message);
110
+ super('WriteError', path, message);
111
+ Object.defineProperty(this, "name", {
112
+ enumerable: true,
113
+ configurable: true,
114
+ writable: true,
115
+ value: 'WriteError'
116
+ });
75
117
  }
76
118
  }
77
119
  /** File content is invalid (e.g., malformed JSON). */
78
120
  export class InvalidData extends FSError {
79
121
  constructor(path, message, cause) {
80
- super("InvalidData", path, message);
122
+ super('InvalidData', path, message);
81
123
  Object.defineProperty(this, "cause", {
82
124
  enumerable: true,
83
125
  configurable: true,
84
126
  writable: true,
85
127
  value: void 0
86
128
  });
129
+ Object.defineProperty(this, "name", {
130
+ enumerable: true,
131
+ configurable: true,
132
+ writable: true,
133
+ value: 'InvalidData'
134
+ });
87
135
  this.cause = cause;
88
136
  }
89
137
  }
90
138
  /** Unknown file system error. */
91
139
  export class Unknown extends FSError {
92
140
  constructor(path, message, cause) {
93
- super("Unknown", path, message);
141
+ super('Unknown', path, message);
94
142
  Object.defineProperty(this, "cause", {
95
143
  enumerable: true,
96
144
  configurable: true,
97
145
  writable: true,
98
146
  value: void 0
99
147
  });
148
+ Object.defineProperty(this, "name", {
149
+ enumerable: true,
150
+ configurable: true,
151
+ writable: true,
152
+ value: 'Unknown'
153
+ });
100
154
  this.cause = cause;
101
155
  }
102
156
  }
103
- /** Registry of file-system error constructors for `instanceof` checks. */
104
- export const FSErrors = {
105
- NotFound,
106
- AlreadyExists,
107
- IsDirectory,
108
- NotDirectory,
109
- PermissionDenied,
110
- ReadError,
111
- WriteError,
112
- InvalidData,
113
- Unknown,
114
- };
115
- function nodeErrorToFSError(error, path) {
157
+ export function nodeErrorToFSError(error, path) {
116
158
  const message = error instanceof Error ? error.message : String(error);
117
159
  const nodeError = error;
118
160
  switch (nodeError.code) {
119
- case "ENOENT":
161
+ case 'ENOENT':
120
162
  return new NotFound(path, message);
121
- case "EEXIST":
163
+ case 'EEXIST':
122
164
  return new AlreadyExists(path, message);
123
- case "EISDIR":
165
+ case 'EISDIR':
124
166
  return new IsDirectory(path, message);
125
- case "ENOTDIR":
167
+ case 'ENOTDIR':
126
168
  return new NotDirectory(path, message);
127
- case "EACCES":
128
- case "EPERM":
169
+ case 'EACCES':
170
+ case 'EPERM':
129
171
  return new PermissionDenied(path, message);
130
172
  default:
131
173
  return new Unknown(path, message, error);
132
174
  }
133
175
  }
134
- export { nodeErrorToFSError };
package/esm/glob_match.js CHANGED
@@ -15,17 +15,17 @@ export function _matchGlob(pattern) {
15
15
  return new RegExp(`^${patternToRegexStr(pattern)}$`);
16
16
  }
17
17
  function patternToRegexStr(pattern) {
18
- const notSep = "[^/]";
19
- let regex = "";
18
+ const notSep = '[^/]';
19
+ let regex = '';
20
20
  let i = 0;
21
21
  while (i < pattern.length) {
22
22
  const ch = pattern[i];
23
- if (ch === "*") {
24
- if (i + 1 < pattern.length && pattern[i + 1] === "*") {
25
- regex += ".*";
23
+ if (ch === '*') {
24
+ if (i + 1 < pattern.length && pattern[i + 1] === '*') {
25
+ regex += '.*';
26
26
  i += 2;
27
27
  // consume the separator that follows **, e.g. "**/"
28
- if (i < pattern.length && (pattern[i] === "/" || pattern[i] === "\\")) {
28
+ if (i < pattern.length && (pattern[i] === '/' || pattern[i] === '\\')) {
29
29
  i++;
30
30
  }
31
31
  }
@@ -34,26 +34,26 @@ function patternToRegexStr(pattern) {
34
34
  i++;
35
35
  }
36
36
  }
37
- else if (ch === "?") {
37
+ else if (ch === '?') {
38
38
  regex += notSep;
39
39
  i++;
40
40
  }
41
- else if (ch === "{") {
42
- const end = pattern.indexOf("}", i + 1);
41
+ else if (ch === '{') {
42
+ const end = pattern.indexOf('}', i + 1);
43
43
  if (end === -1) {
44
- regex += "\\{";
44
+ regex += '\\{';
45
45
  i++;
46
46
  }
47
47
  else {
48
- const alternatives = pattern.slice(i + 1, end).split(",");
49
- regex += `(?:${alternatives.map(patternToRegexStr).join("|")})`;
48
+ const alternatives = pattern.slice(i + 1, end).split(',');
49
+ regex += `(?:${alternatives.map(patternToRegexStr).join('|')})`;
50
50
  i = end + 1;
51
51
  }
52
52
  }
53
- else if (ch === "[") {
54
- const end = pattern.indexOf("]", i + 1);
53
+ else if (ch === '[') {
54
+ const end = pattern.indexOf(']', i + 1);
55
55
  if (end === -1) {
56
- regex += "\\[";
56
+ regex += '\\[';
57
57
  i++;
58
58
  }
59
59
  else {
package/esm/index.d.ts CHANGED
@@ -24,15 +24,15 @@
24
24
  * @module
25
25
  */
26
26
  import "./_dnt.polyfills.js";
27
- export { readFile, readJson, readLines, readTextFile } from "./read.js";
28
- export type { ReadFileError, ReadJsonError } from "./read.js";
29
- export { writeFile, writeJson, writeTextFile } from "./write.js";
30
- export type { WriteFileError } from "./write.js";
31
- export { glob, readDir, walk } from "./dir.js";
32
- export type { DirError } from "./dir.js";
33
- export { watch } from "./watch.js";
34
- export type { WatchError } from "./watch.js";
35
- export type { DirEntry, FsEvent, GlobOptions, WalkEntry, WalkOptions, WatchOptions, } from "./types.js";
36
- export { FSError, FSErrors } from "./errors.js";
37
- export type { AlreadyExists, InvalidData, IsDirectory, NotDirectory, NotFound, PermissionDenied, ReadError, Unknown, WriteError, } from "./errors.js";
27
+ export { readFile, readJson, readLines, readTextFile } from './read.js';
28
+ export type { ReadFileError, ReadJsonError } from './read.js';
29
+ export { writeFile, writeJson, writeTextFile } from './write.js';
30
+ export type { WriteFileError } from './write.js';
31
+ export { glob, readDir, walk } from './dir.js';
32
+ export type { DirError } from './dir.js';
33
+ export { watch } from './watch.js';
34
+ export type { WatchError } from './watch.js';
35
+ export type { DirEntry, FsEvent, GlobOptions, WalkEntry, WalkOptions, WatchOptions, } from './types.js';
36
+ export { FSError } from './errors.js';
37
+ export { AlreadyExists, InvalidData, IsDirectory, NotDirectory, NotFound, PermissionDenied, ReadError, Unknown, WriteError, } from './errors.js';
38
38
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,OAAO,qBAAqB,CAAC;AAE7B,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACxE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACjE,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAC/C,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,YAAY,EACV,QAAQ,EACR,OAAO,EACP,WAAW,EACX,SAAS,EACT,WAAW,EACX,YAAY,GACb,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAChD,YAAY,EACV,aAAa,EACb,WAAW,EACX,WAAW,EACX,YAAY,EACZ,QAAQ,EACR,gBAAgB,EAChB,SAAS,EACT,OAAO,EACP,UAAU,GACX,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,OAAO,qBAAqB,CAAC;AAE7B,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACvE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AAC7D,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAChE,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAChD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAA;AAC9C,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAC5C,YAAY,EACV,QAAQ,EACR,OAAO,EACP,WAAW,EACX,SAAS,EACT,WAAW,EACX,YAAY,GACb,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AACrC,OAAO,EACL,aAAa,EACb,WAAW,EACX,WAAW,EACX,YAAY,EACZ,QAAQ,EACR,gBAAgB,EAChB,SAAS,EACT,OAAO,EACP,UAAU,GACX,MAAM,aAAa,CAAA"}
package/esm/index.js CHANGED
@@ -24,8 +24,9 @@
24
24
  * @module
25
25
  */
26
26
  import "./_dnt.polyfills.js";
27
- export { readFile, readJson, readLines, readTextFile } from "./read.js";
28
- export { writeFile, writeJson, writeTextFile } from "./write.js";
29
- export { glob, readDir, walk } from "./dir.js";
30
- export { watch } from "./watch.js";
31
- export { FSError, FSErrors } from "./errors.js";
27
+ export { readFile, readJson, readLines, readTextFile } from './read.js';
28
+ export { writeFile, writeJson, writeTextFile } from './write.js';
29
+ export { glob, readDir, walk } from './dir.js';
30
+ export { watch } from './watch.js';
31
+ export { FSError } from './errors.js';
32
+ export { AlreadyExists, InvalidData, IsDirectory, NotDirectory, NotFound, PermissionDenied, ReadError, Unknown, WriteError, } from './errors.js';
package/esm/read.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { Source, Task } from "anabranch";
2
- import { FSError, FSErrors, type NotFound, type PermissionDenied, type ReadError, type Unknown } from "./errors.js";
3
- export { FSError, FSErrors };
1
+ import { Source, Task } from 'anabranch';
2
+ import { InvalidData, type NotFound, type PermissionDenied, type ReadError, type Unknown } from './errors.js';
4
3
  /**
5
4
  * Streams lines from a text file one at a time using `node:readline`.
6
5
  */
@@ -16,9 +15,9 @@ export declare function readFile(path: string | URL): Task<Uint8Array, ReadFileE
16
15
  /**
17
16
  * Reads a JSON file and parses it, returning the value typed as `T`.
18
17
  */
19
- export declare function readJson<T>(path: string | URL): Task<T, ReadJsonError>;
18
+ export declare function readJson<T extends Record<string, any> = Record<string, any>>(path: string | URL): Task<T, ReadJsonError>;
20
19
  /** Errors that can occur when reading files. */
21
20
  export type ReadFileError = NotFound | PermissionDenied | ReadError | Unknown;
22
21
  /** Errors that can occur when reading JSON files. */
23
- export type ReadJsonError = NotFound | PermissionDenied | ReadError | InstanceType<typeof FSErrors.InvalidData> | Unknown;
22
+ export type ReadJsonError = NotFound | PermissionDenied | ReadError | InstanceType<typeof InvalidData> | Unknown;
24
23
  //# sourceMappingURL=read.d.ts.map
package/esm/read.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"read.d.ts","sourceRoot":"","sources":["../src/read.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EACL,OAAO,EACP,QAAQ,EAER,KAAK,QAA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;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,MAAM,GAAG,GAAG,GACjB,IAAI,CAAC,MAAM,EAAE,aAAa,CAAC,CAG7B;AAED;;GAEG;AACH,wBAAgB,QAAQ,CACtB,IAAI,EAAE,MAAM,GAAG,GAAG,GACjB,IAAI,CAAC,UAAU,EAAE,aAAa,CAAC,CAKjC;AAED;;GAEG;AAEH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1E,IAAI,EAAE,MAAM,GAAG,GAAG,GACjB,IAAI,CAAC,CAAC,EAAE,aAAa,CAAC,CAYxB;AAED,gDAAgD;AAChD,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,gBAAgB,GAAG,SAAS,GAAG,OAAO,CAAA;AAE7E,qDAAqD;AACrD,MAAM,MAAM,aAAa,GACrB,QAAQ,GACR,gBAAgB,GAChB,SAAS,GACT,YAAY,CAAC,OAAO,WAAW,CAAC,GAChC,OAAO,CAAA"}
package/esm/read.js CHANGED
@@ -1,9 +1,8 @@
1
- import { createReadStream } from "node:fs";
2
- import { readFile as fsReadFile } from "node:fs/promises";
3
- import { createInterface } from "node:readline";
4
- import { Source, Task } from "anabranch";
5
- import { FSError, FSErrors, nodeErrorToFSError, } from "./errors.js";
6
- export { FSError, FSErrors };
1
+ import { createReadStream } from 'node:fs';
2
+ import { readFile as fsReadFile } from 'node:fs/promises';
3
+ import { createInterface } from 'node:readline';
4
+ import { Source, Task } from 'anabranch';
5
+ import { InvalidData, nodeErrorToFSError, } from './errors.js';
7
6
  /**
8
7
  * Streams lines from a text file one at a time using `node:readline`.
9
8
  */
@@ -29,47 +28,26 @@ export function readLines(path) {
29
28
  * Reads an entire file as a UTF-8 string.
30
29
  */
31
30
  export function readTextFile(path) {
32
- return Task.of(async () => {
33
- try {
34
- return await fsReadFile(path, "utf8");
35
- }
36
- catch (error) {
37
- throw nodeErrorToFSError(error, path);
38
- }
39
- });
31
+ return Task.of(async () => await fsReadFile(path, 'utf8'))
32
+ .mapErr((error) => nodeErrorToFSError(error, path));
40
33
  }
41
34
  /**
42
35
  * Reads an entire file as a `Uint8Array`.
43
36
  */
44
37
  export function readFile(path) {
45
38
  return Task.of(async () => {
46
- try {
47
- const buf = await fsReadFile(path);
48
- return new Uint8Array(buf);
49
- }
50
- catch (error) {
51
- throw nodeErrorToFSError(error, path);
52
- }
53
- });
39
+ const buf = await fsReadFile(path);
40
+ return new Uint8Array(buf);
41
+ }).mapErr((error) => nodeErrorToFSError(error, path));
54
42
  }
55
43
  /**
56
44
  * Reads a JSON file and parses it, returning the value typed as `T`.
57
45
  */
46
+ // deno-lint-ignore no-explicit-any
58
47
  export function readJson(path) {
59
- return Task.of(async () => {
60
- try {
61
- const text = await fsReadFile(path, "utf8");
62
- try {
63
- return JSON.parse(text);
64
- }
65
- catch (error) {
66
- throw new FSErrors.InvalidData(path, error.message, error);
67
- }
68
- }
69
- catch (error) {
70
- if (error instanceof FSError)
71
- throw error;
72
- throw nodeErrorToFSError(error, path);
73
- }
48
+ return Task.of(async () => await fsReadFile(path, 'utf8'))
49
+ .mapErr((error) => nodeErrorToFSError(error, path))
50
+ .tryMap((text) => JSON.parse(text), (error) => {
51
+ return new InvalidData(path, error.message, error);
74
52
  });
75
53
  }
package/esm/types.d.ts CHANGED
@@ -47,12 +47,12 @@ export interface WalkOptions {
47
47
  skip?: RegExp[];
48
48
  }
49
49
  /** Options for {@link glob}. Same as {@link WalkOptions} without `match`. */
50
- export interface GlobOptions extends Omit<WalkOptions, "match"> {
50
+ export interface GlobOptions extends Omit<WalkOptions, 'match'> {
51
51
  }
52
52
  /** A file-system change event, as yielded by {@link watch}. */
53
53
  export interface FsEvent {
54
54
  /** The type of change. */
55
- kind: "create" | "modify" | "remove";
55
+ kind: 'create' | 'modify' | 'remove';
56
56
  /** Absolute paths of the affected files. */
57
57
  paths: string[];
58
58
  }
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,MAAM,WAAW,QAAQ;IACvB,iDAAiD;IACjD,IAAI,EAAE,MAAM,CAAC;IACb,2CAA2C;IAC3C,MAAM,EAAE,OAAO,CAAC;IAChB,wCAAwC;IACxC,WAAW,EAAE,OAAO,CAAC;IACrB,4CAA4C;IAC5C,SAAS,EAAE,OAAO,CAAC;CACpB;AAED,yFAAyF;AACzF,MAAM,WAAW,SAAU,SAAQ,QAAQ;IACzC,sCAAsC;IACtC,IAAI,EAAE,MAAM,CAAC;CACd;AAED,gCAAgC;AAChC,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,6EAA6E;AAC7E,MAAM,WAAW,WAAY,SAAQ,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC;CAAG;AAElE,+DAA+D;AAC/D,MAAM,WAAW,OAAO;IACtB,0BAA0B;IAC1B,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;IACrC,4CAA4C;IAC5C,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,iCAAiC;AACjC,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,MAAM,WAAW,QAAQ;IACvB,iDAAiD;IACjD,IAAI,EAAE,MAAM,CAAA;IACZ,2CAA2C;IAC3C,MAAM,EAAE,OAAO,CAAA;IACf,wCAAwC;IACxC,WAAW,EAAE,OAAO,CAAA;IACpB,4CAA4C;IAC5C,SAAS,EAAE,OAAO,CAAA;CACnB;AAED,yFAAyF;AACzF,MAAM,WAAW,SAAU,SAAQ,QAAQ;IACzC,sCAAsC;IACtC,IAAI,EAAE,MAAM,CAAA;CACb;AAED,gCAAgC;AAChC,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IAChB;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;CAChB;AAED,6EAA6E;AAC7E,MAAM,WAAW,WAAY,SAAQ,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC;CAAG;AAElE,+DAA+D;AAC/D,MAAM,WAAW,OAAO;IACtB,0BAA0B;IAC1B,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAA;IACpC,4CAA4C;IAC5C,KAAK,EAAE,MAAM,EAAE,CAAA;CAChB;AAED,iCAAiC;AACjC,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB"}
package/esm/watch.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { Stream } from "anabranch";
2
- import type { FsEvent, WatchOptions } from "./types.js";
3
- import { type NotFound, type PermissionDenied } from "./errors.js";
1
+ import type { Stream } from 'anabranch';
2
+ import type { FsEvent, WatchOptions } from './types.js';
3
+ import { type NotFound, type PermissionDenied } from './errors.js';
4
4
  /** Errors that can occur when watching the file system. */
5
5
  export type WatchError = NotFound | PermissionDenied;
6
6
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"watch.d.ts","sourceRoot":"","sources":["../src/watch.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACxD,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,gBAAgB,EACtB,MAAM,aAAa,CAAC;AAErB,2DAA2D;AAC3D,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,gBAAgB,CAAC;AAErD;;;;;;;;;;GAUG;AACH,wBAAgB,KAAK,CACnB,IAAI,EAAE,MAAM,GAAG,GAAG,EAClB,OAAO,CAAC,EAAE,YAAY,GACrB,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,CAiC7B"}
1
+ {"version":3,"file":"watch.d.ts","sourceRoot":"","sources":["../src/watch.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AACvC,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AACvD,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,gBAAgB,EACtB,MAAM,aAAa,CAAA;AAEpB,2DAA2D;AAC3D,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,gBAAgB,CAAA;AAEpD;;;;;;;;;;GAUG;AACH,wBAAgB,KAAK,CACnB,IAAI,EAAE,MAAM,GAAG,GAAG,EAClB,OAAO,CAAC,EAAE,YAAY,GACrB,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,CAiC7B"}
package/esm/watch.js CHANGED
@@ -1,8 +1,8 @@
1
- import { existsSync, watch as fsWatch } from "node:fs";
2
- import { join } from "node:path";
3
- import { fileURLToPath } from "node:url";
4
- import { Channel, Source } from "anabranch";
5
- import { nodeErrorToFSError, } from "./errors.js";
1
+ import { existsSync, watch as fsWatch } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { Channel, Source } from 'anabranch';
5
+ import { nodeErrorToFSError, } from './errors.js';
6
6
  /**
7
7
  * Watches `path` for file-system changes, yielding a {@link FsEvent} for each.
8
8
  *
@@ -21,19 +21,19 @@ export function watch(path, options) {
21
21
  const name = filename;
22
22
  const fullPath = name ? join(watchPath, name) : watchPath;
23
23
  let kind;
24
- if (eventType === "rename") {
25
- kind = existsSync(fullPath) ? "create" : "remove";
24
+ if (eventType === 'rename') {
25
+ kind = existsSync(fullPath) ? 'create' : 'remove';
26
26
  }
27
27
  else {
28
- kind = "modify";
28
+ kind = 'modify';
29
29
  }
30
30
  channel.send({ kind, paths: [fullPath] });
31
31
  });
32
32
  const channel = new Channel({
33
33
  onClose: () => watcher?.close(),
34
34
  });
35
- watcher.once("error", (err) => channel.fail(nodeErrorToFSError(err, path)));
36
- watcher.once("close", () => channel.close());
35
+ watcher.once('error', (err) => channel.fail(nodeErrorToFSError(err, path)));
36
+ watcher.once('close', () => channel.close());
37
37
  yield* channel;
38
38
  });
39
39
  }
package/esm/write.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Task } from "anabranch";
2
- import { type IsDirectory, type NotFound, type PermissionDenied, type Unknown, type WriteError } from "./errors.js";
1
+ import { Task } from 'anabranch';
2
+ import { type IsDirectory, type NotFound, type PermissionDenied, type Unknown, type WriteError } from './errors.js';
3
3
  /**
4
4
  * Writes a UTF-8 string to a file, creating or overwriting it.
5
5
  */
@@ -1 +1 @@
1
- {"version":3,"file":"write.d.ts","sourceRoot":"","sources":["../src/write.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,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,CAC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GAChB,UAAU,GACV,OAAO,CAAA"}
package/esm/write.js CHANGED
@@ -1,42 +1,24 @@
1
- import { writeFile as fsWriteFile } from "node:fs/promises";
2
- import { Task } from "anabranch";
3
- import { nodeErrorToFSError, } from "./errors.js";
1
+ import { writeFile as fsWriteFile } from 'node:fs/promises';
2
+ import { Task } from 'anabranch';
3
+ import { nodeErrorToFSError, } from './errors.js';
4
4
  /**
5
5
  * Writes a UTF-8 string to a file, creating or overwriting it.
6
6
  */
7
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
- });
8
+ return Task.of(async () => await fsWriteFile(path, content, 'utf8'))
9
+ .mapErr((error) => nodeErrorToFSError(error, path));
16
10
  }
17
11
  /**
18
12
  * Writes a `Uint8Array` to a file, creating or overwriting it.
19
13
  */
20
14
  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
- });
15
+ return Task.of(async () => await fsWriteFile(path, data))
16
+ .mapErr((error) => nodeErrorToFSError(error, path));
29
17
  }
30
18
  /**
31
19
  * Serialises `value` as JSON and writes it to a file, creating or overwriting it.
32
20
  */
33
21
  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
- });
22
+ return Task.of(async () => await fsWriteFile(path, JSON.stringify(value), 'utf8'))
23
+ .mapErr((error) => nodeErrorToFSError(error, path));
42
24
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@anabranch/fs",
3
- "version": "0.1.15",
4
- "description": "Streaming file-system utilities for the anabranch ecosystem",
3
+ "version": "0.2.0",
4
+ "description": "Streaming file-system utilities for reading, walking, globbing, and watching files with composable error handling.",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/frodi-karlsson/anabranch.git"
@@ -21,7 +21,7 @@
21
21
  "anabranch": "^0"
22
22
  },
23
23
  "devDependencies": {
24
- "@types/node": "^20"
24
+ "@types/node": "^24"
25
25
  },
26
26
  "_generatedBy": "dnt@dev"
27
27
  }