interface ScanNodesByTypesParams {
nodeId: string;
types: string[];
}
/**
* Scan nodes by types
*/
export async function scanNodesByTypes(
params: ScanNodesByTypesParams,
): Promise<{
success: boolean;
totalFound: number;
nodes: Array<{
id: string;
name: string;
type: string;
path: string;
}>;
}> {
const { nodeId, types } = params;
const rootNode = await figma.getNodeByIdAsync(nodeId);
if (!rootNode) {
throw new Error(`Node not found with ID: ${nodeId}`);
}
const typesSet = new Set(types.map((t) => t.toUpperCase()));
const foundNodes: Array<{
id: string;
name: string;
type: string;
path: string;
}> = [];
function getNodePath(node: BaseNode): string {
const path: string[] = [];
let current: BaseNode | null = node;
while (current?.parent) {
path.unshift(current.name);
current = current.parent;
}
return path.join(" > ");
}
function scanNode(node: BaseNode): void {
if (typesSet.has(node.type)) {
foundNodes.push({
id: node.id,
name: node.name,
type: node.type,
path: getNodePath(node),
});
}
if ("children" in node) {
const parent = node as ChildrenMixin;
for (const child of parent.children) {
scanNode(child);
}
}
}
scanNode(rootNode);
return {
success: true,
totalFound: foundNodes.length,
nodes: foundNodes,
};
}