@weborigami/async-tree 0.0.55 → 0.0.56

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.0.55",
3
+ "version": "0.0.56",
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.4.5"
12
12
  },
13
13
  "dependencies": {
14
- "@weborigami/types": "0.0.55"
14
+ "@weborigami/types": "0.0.56"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "node --test --test-reporter=spec",
package/src/FileTree.js CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  getRealmObjectPrototype,
7
7
  hiddenFileNames,
8
8
  isPacked,
9
+ isPlainObject,
9
10
  naturalOrder,
10
11
  } from "./utilities.js";
11
12
 
@@ -165,6 +166,9 @@ export default class FileTree {
165
166
  await fs.mkdir(this.dirname, { recursive: true });
166
167
  // Write out the value as the contents of a file.
167
168
  await fs.writeFile(destPath, value);
169
+ } else if (isPlainObject(value) && Object.keys(value).length === 0) {
170
+ // Special case: empty object means create an empty directory.
171
+ await fs.mkdir(destPath, { recursive: true });
168
172
  } else if (Tree.isTreelike(value)) {
169
173
  // Treat value as a tree and write it out as a subdirectory.
170
174
  const destTree = Reflect.construct(this.constructor, [destPath]);
@@ -1,8 +1,10 @@
1
1
  import { DeepObjectTree, Tree } from "../internal.js";
2
2
 
3
3
  /**
4
- * Caches values from a source tree in a second cache tree. If no second tree is
5
- * supplied, an in-memory cache is used.
4
+ * Caches values from a source tree in a second cache tree. Cache source tree
5
+ * keys in memory.
6
+ *
7
+ * If no second tree is supplied, an in-memory value cache is used.
6
8
  *
7
9
  * An optional third filter tree can be supplied. If a filter tree is supplied,
8
10
  * only values for keys that match the filter will be cached.
@@ -22,6 +24,7 @@ export default function treeCache(sourceTreelike, cacheTree, filterTreelike) {
22
24
 
23
25
  /** @type {AsyncMutableTree} */
24
26
  const cache = cacheTree ?? new DeepObjectTree({});
27
+ let keys;
25
28
  return {
26
29
  description: "cache",
27
30
 
@@ -65,14 +68,7 @@ export default function treeCache(sourceTreelike, cacheTree, filterTreelike) {
65
68
  },
66
69
 
67
70
  async keys() {
68
- const keys = new Set(await source.keys());
69
-
70
- // We also add the cache's keys in case the keys provided by the source
71
- // tree have changed since the cache was updated.
72
- for (const key of await cache.keys()) {
73
- keys.add(key);
74
- }
75
-
71
+ keys ??= source.keys();
76
72
  return keys;
77
73
  },
78
74
  };
@@ -9,7 +9,7 @@ import { Tree } from "../src/internal.js";
9
9
  const dirname = path.dirname(fileURLToPath(import.meta.url));
10
10
  const tempDirectory = path.join(dirname, "fixtures/temp");
11
11
 
12
- describe("FileTree", async () => {
12
+ describe.only("FileTree", async () => {
13
13
  test("can get the keys of the tree", async () => {
14
14
  const fixture = createFixture("fixtures/markdown");
15
15
  assert.deepEqual(Array.from(await fixture.keys()), [
@@ -61,6 +61,23 @@ describe("FileTree", async () => {
61
61
  await removeTempDirectory();
62
62
  });
63
63
 
64
+ test.only("can create empty subfolder via set()", async () => {
65
+ await createTempDirectory();
66
+
67
+ // Write out new, empty folder called "empty".
68
+ const tempFiles = new FileTree(tempDirectory);
69
+ await tempFiles.set("empty", {});
70
+
71
+ // Verify folder exists and has no contents.
72
+ const folderPath = path.join(tempDirectory, "empty");
73
+ const stats = await fs.stat(folderPath);
74
+ assert(stats.isDirectory());
75
+ const files = await fs.readdir(folderPath);
76
+ assert.deepEqual(files, []);
77
+
78
+ await removeTempDirectory();
79
+ });
80
+
64
81
  test("can write out subfolder via set()", async () => {
65
82
  await createTempDirectory();
66
83