( nodes: JsDelivrFileNode[], parentPath: string = '', )
| 28 | * Convert jsDelivr nested structure to our PackageFileTree format |
| 29 | */ |
| 30 | export function convertToFileTree( |
| 31 | nodes: JsDelivrFileNode[], |
| 32 | parentPath: string = '', |
| 33 | ): PackageFileTree[] { |
| 34 | const result: PackageFileTree[] = [] |
| 35 | |
| 36 | for (const node of nodes) { |
| 37 | const path = parentPath ? `${parentPath}/${node.name}` : node.name |
| 38 | |
| 39 | if (node.type === 'directory') { |
| 40 | const children = node.files ? convertToFileTree(node.files, path) : [] |
| 41 | |
| 42 | result.push({ |
| 43 | name: node.name, |
| 44 | path, |
| 45 | type: 'directory', |
| 46 | size: children.reduce((total, child) => total + (child.size ?? 0), 0), |
| 47 | children, |
| 48 | }) |
| 49 | } else { |
| 50 | result.push({ |
| 51 | name: node.name, |
| 52 | path, |
| 53 | type: 'file', |
| 54 | hash: node.hash, |
| 55 | size: node.size, |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Sort: directories first, then files, alphabetically within each group |
| 61 | result.sort((a, b) => { |
| 62 | if (a.type !== b.type) { |
| 63 | return a.type === 'directory' ? -1 : 1 |
| 64 | } |
| 65 | return a.name.localeCompare(b.name) |
| 66 | }) |
| 67 | |
| 68 | return result |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Fetch and convert file tree for a package version. |
no outgoing calls
no test coverage detected