code: function addSourceToNode(
    node: TreeDirectory,
    url: ParsedURL,
    source: Source
  ): Source | TreeNode[] {
    const isFile = !isPathDirectory(url.path);
  
    if (node.type == "source") {
      throw new Error(`Unexpected type "source" at: ${node.name}`);
    }
  
    // if we have a file, and the subtree has no elements, overwrite the
    // subtree contents with the source
    if (isFile) {
      // $FlowIgnore
      node.type = "source";
      return source;
    }
  
    const { filename } = url;
    const { found: childFound, index: childIndex } = findNodeInContents(
      node,
      createTreeNodeMatcher(filename, false, null)
    );
  
    // if we are readding an existing file in the node, overwrite the existing
    // file and return the node's contents
    if (childFound) {
      const existingNode = node.contents[childIndex];
      if (existingNode.type === "source") {
        existingNode.contents = source;
      }
  
      return node.contents;
    }
  
    // if this is a new file, add the new file;
    const newNode = createSourceNode(filename, source.url, source);
    const contents = node.contents.slice(0);
    contents.splice(childIndex, 0, newNode);
    return contents;
  }
docs: /*
  Add a source file to a directory node in the tree