@weborigami/async-tree 0.2.0 → 0.2.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weborigami/async-tree",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Asynchronous tree drivers based on standard JavaScript classes",
5
5
  "type": "module",
6
6
  "main": "./main.js",
@@ -11,7 +11,7 @@
11
11
  "typescript": "5.6.2"
12
12
  },
13
13
  "dependencies": {
14
- "@weborigami/types": "0.2.0"
14
+ "@weborigami/types": "0.2.2"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "node --test --test-reporter=spec",
package/shared.js CHANGED
@@ -29,4 +29,5 @@ export { default as sort } from "./src/operations/sort.js";
29
29
  export { default as take } from "./src/operations/take.js";
30
30
  export * as symbols from "./src/symbols.js";
31
31
  export * as trailingSlash from "./src/trailingSlash.js";
32
+ export { default as TraverseError } from "./src/TraverseError.js";
32
33
  export * from "./src/utilities.js";
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Error class thrown by Tree.traverseOrThrow()
3
+ */
4
+ export default class TraverseError extends ReferenceError {
5
+ constructor(message, options) {
6
+ super(message);
7
+ this.name = "TraverseError";
8
+ Object.assign(this, options);
9
+ }
10
+ }
package/src/Tree.js CHANGED
@@ -5,6 +5,7 @@ import SetTree from "./drivers/SetTree.js";
5
5
  import { DeepObjectTree, ObjectTree } from "./internal.js";
6
6
  import * as symbols from "./symbols.js";
7
7
  import * as trailingSlash from "./trailingSlash.js";
8
+ import TraverseError from "./TraverseError.js";
8
9
  import * as utilities from "./utilities.js";
9
10
  import {
10
11
  castArrayLike,
@@ -476,15 +477,6 @@ export async function traversePath(tree, path) {
476
477
  return traverse(tree, ...keys);
477
478
  }
478
479
 
479
- // Error class thrown by traverseOrThrow()
480
- class TraverseError extends ReferenceError {
481
- constructor(message, options) {
482
- super(message);
483
- this.name = "TraverseError";
484
- Object.assign(this, options);
485
- }
486
- }
487
-
488
480
  /**
489
481
  * Return the values in the specific node of the tree.
490
482
  *
@@ -180,15 +180,7 @@ export default class FileTree {
180
180
  }
181
181
 
182
182
  if (packed) {
183
- // Single writeable value.
184
- if (value instanceof ArrayBuffer) {
185
- // Convert ArrayBuffer to Uint8Array, which Node.js can write directly.
186
- value = new Uint8Array(value);
187
- }
188
- // Ensure this directory exists.
189
- await fs.mkdir(this.dirname, { recursive: true });
190
- // Write out the value as the contents of a file.
191
- await fs.writeFile(destPath, value);
183
+ await writeFile(value, destPath);
192
184
  } else if (isPlainObject(value) && Object.keys(value).length === 0) {
193
185
  // Special case: empty object means create an empty directory.
194
186
  await fs.mkdir(destPath, { recursive: true });
@@ -266,3 +258,26 @@ async function stat(filePath) {
266
258
  throw error;
267
259
  }
268
260
  }
261
+
262
+ // Write a value to a file.
263
+ async function writeFile(value, destPath) {
264
+ // Single writeable value.
265
+ if (value instanceof ArrayBuffer) {
266
+ // Convert ArrayBuffer to Uint8Array, which Node.js can write directly.
267
+ value = new Uint8Array(value);
268
+ }
269
+ // Ensure this directory exists.
270
+ const dirname = path.dirname(destPath);
271
+ await fs.mkdir(dirname, { recursive: true });
272
+
273
+ // Write out the value as the contents of a file.
274
+ try {
275
+ await fs.writeFile(destPath, value);
276
+ } catch (/** @type {any} */ error) {
277
+ if (error.code === "EISDIR" /* Is a directory */) {
278
+ throw new Error(
279
+ `Tried to overwrite a directory with a single file: ${destPath}`
280
+ );
281
+ }
282
+ }
283
+ }