Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@
},
"dependencies": {
"@luxass/msw-utils": "catalog:prod",
"@luxass/utils": "catalog:prod",
"@ucdjs-internal/shared": "workspace:*",
"@ucdjs/env": "workspace:*",
"@ucdjs/fs-bridge": "workspace:*",
"@ucdjs/schemas": "workspace:*",
"@unicode-utils/core": "catalog:prod",
"msw": "catalog:testing",
"zod": "catalog:prod"
},
Expand Down
40 changes: 40 additions & 0 deletions packages/test-utils/src/mock-store/add-paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { MockStoreNode, MockStoreNodeWithPath } from "./types";

/**
* Recursively traverses the file tree and adds paths to all nodes.
* The path format is: /{prefix}/basePath/pathname or /basePath/pathname if prefix is empty
*
* @param {MockStoreNode[]} nodes - The file nodes without paths
* @param {string} prefix - The prefix to include in the path (e.g., version). If empty, path starts with basePath
* @param {string} [basePath] - The base path to prepend (defaults to "ucd")
* @returns File nodes with paths added
*/
export function addPathsToFileNodes(
nodes: MockStoreNode[],
prefix: string,
basePath: string = "ucd",
): MockStoreNodeWithPath[] {
return nodes.map((node) => {
const pathSegments = [basePath, node.name].filter(Boolean).join("/");
const fullPath = prefix ? `/${prefix}/${pathSegments}` : `/${pathSegments}`;

if (node.type === "directory") {
const dirNode = node as Extract<MockStoreNode, { type: "directory" }>;
return {
...dirNode,
path: `${fullPath}/`,
children: addPathsToFileNodes(
dirNode.children,
prefix,
pathSegments,
),
};
}

const fileNode = node as Extract<MockStoreNode, { type: "file" }>;
return {
...fileNode,
path: fullPath,
};
});
}
Loading
Loading