Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tweak types
  • Loading branch information
mattrothenberg committed Jan 4, 2023
commit ee9aa0def1b59dc1a2d4d3d65758b4ed8cc30cd7
19 changes: 15 additions & 4 deletions utils/lib/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ type RecursiveGitTree =

type RecursiveGitTreeItem = RecursiveGitTree[number];

// Create a new type that is everything in RecursiveGitTreeItem but with "path" marked as required.
// This is because Octokit types "path" as optional, but it's never undefined in reality AFAICT.
type RecursiveGitTreeItemWithPath = Omit<RecursiveGitTreeItem, "path"> & {
path: string;
};

type ItemType = "blob" | "tree";
type RootNode = { path: "/" };

class TreeNode {
export class TreeNode {
path: string;
type: ItemType;
meta: RecursiveGitTreeItem;
meta: RecursiveGitTreeItemWithPath | RootNode;
children: TreeNode[];

constructor(
path: string,
meta: RecursiveGitTreeItem,
meta: RecursiveGitTreeItemWithPath | RootNode,
type: ItemType,
children: TreeNode[] = []
) {
Expand Down Expand Up @@ -84,7 +91,11 @@ export function buildTree(items: RecursiveGitTree) {
}
}
if (!found) {
const newNode = new TreeNode(part, item, "tree");
const newNode = new TreeNode(
part,
{ ...item, path: item.path || "" },
"tree"
);
currentNode.children.push(newNode);
currentNode = newNode;
nodes.push(newNode);
Expand Down