|
| 1 | +import { cli, Strategy } from '@jackwener/opencli/registry'; |
| 2 | +import type { IPage } from '@jackwener/opencli/types'; |
| 3 | +import { |
| 4 | + findFolder, |
| 5 | + formatSize, |
| 6 | + listMyDrive, |
| 7 | +} from './utils.js'; |
| 8 | + |
| 9 | +interface DriveNode { |
| 10 | + name: string; |
| 11 | + fid: string; |
| 12 | + is_dir: boolean; |
| 13 | + size: string; |
| 14 | + path: string; |
| 15 | + children?: DriveNode[]; |
| 16 | +} |
| 17 | + |
| 18 | +async function buildTree( |
| 19 | + page: IPage, |
| 20 | + pdirFid: string, |
| 21 | + parentPath: string, |
| 22 | + depth: number, |
| 23 | + maxDepth: number, |
| 24 | + dirsOnly: boolean, |
| 25 | +): Promise<DriveNode[]> { |
| 26 | + if (depth > maxDepth) return []; |
| 27 | + |
| 28 | + const files = await listMyDrive(page, pdirFid); |
| 29 | + const nodes: DriveNode[] = []; |
| 30 | + |
| 31 | + for (const file of files) { |
| 32 | + if (dirsOnly && !file.dir) continue; |
| 33 | + |
| 34 | + const path = parentPath ? `${parentPath}/${file.file_name}` : file.file_name; |
| 35 | + const node: DriveNode = { |
| 36 | + name: file.file_name, |
| 37 | + fid: file.fid, |
| 38 | + is_dir: file.dir, |
| 39 | + size: formatSize(file.size), |
| 40 | + path, |
| 41 | + }; |
| 42 | + |
| 43 | + if (file.dir && depth < maxDepth) { |
| 44 | + node.children = await buildTree(page, file.fid, path, depth + 1, maxDepth, dirsOnly); |
| 45 | + } |
| 46 | + |
| 47 | + nodes.push(node); |
| 48 | + } |
| 49 | + |
| 50 | + return nodes; |
| 51 | +} |
| 52 | + |
| 53 | +function flattenTree(nodes: DriveNode[], level = 0): Record<string, unknown>[] { |
| 54 | + const result: Record<string, unknown>[] = []; |
| 55 | + const indent = ' '.repeat(level); |
| 56 | + |
| 57 | + for (const node of nodes) { |
| 58 | + result.push({ |
| 59 | + name: `${indent}${node.name}`, |
| 60 | + fid: node.fid, |
| 61 | + is_dir: node.is_dir, |
| 62 | + size: node.size, |
| 63 | + path: node.path, |
| 64 | + }); |
| 65 | + if (node.children) { |
| 66 | + result.push(...flattenTree(node.children, level + 1)); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return result; |
| 71 | +} |
| 72 | + |
| 73 | +cli({ |
| 74 | + site: 'quark', |
| 75 | + name: 'ls', |
| 76 | + description: 'List files in your Quark Drive', |
| 77 | + domain: 'pan.quark.cn', |
| 78 | + strategy: Strategy.COOKIE, |
| 79 | + args: [ |
| 80 | + { name: 'path', positional: true, default: '', help: 'Folder path to list (empty for root)' }, |
| 81 | + { name: 'depth', type: 'int', default: 0, help: 'Max depth to traverse' }, |
| 82 | + { name: 'dirs-only', type: 'boolean', default: false, help: 'Show directories only' }, |
| 83 | + ], |
| 84 | + columns: ['name', 'is_dir', 'size', 'fid', 'path'], |
| 85 | + func: async (page, kwargs) => { |
| 86 | + const path = (kwargs.path as string) ?? ''; |
| 87 | + const depth = Math.max(0, (kwargs.depth as number) ?? 0); |
| 88 | + const dirsOnly = (kwargs['dirs-only'] as boolean) ?? false; |
| 89 | + |
| 90 | + const rootFid = path ? await findFolder(page, path) : '0'; |
| 91 | + const tree = await buildTree(page, rootFid, path, 0, depth, dirsOnly); |
| 92 | + return flattenTree(tree); |
| 93 | + }, |
| 94 | +}); |
0 commit comments