Files
jackwener__opencli/clis/quark/ls.ts
X 61b247f90c feat(quark): add Quark Drive adapter (#858)
* feat(quark): add Quark Drive adapter (ls, mkdir, mv, rename, rm, save, share-tree)

Browser-based adapter for Quark Cloud Drive (pan.quark.cn) using cookie
strategy. Supports file browsing, folder management, and saving shared
files with task polling for async operations.

* fix(quark): address review feedback on adapter correctness and docs

- ls: fix depth off-by-one (default 0 now lists target folder only)
- save/mv: report success only after pollTask confirms completion; throw on timeout
- save/mv: reject combining --to and --to-fid instead of silently preferring --to-fid
- utils: check content-type before calling r.json() to handle non-JSON responses gracefully
- tests: add quark graceful auth-failure E2E cases for all 7 commands
- docs: add Browser Bridge extension to prerequisites; fix mkdir --parent example; expand command table with positional args; add Notes section explaining stoken flow

* fix(quark): map auth failures and cover utils

* fix(quark): reuse auth mapping for share-tree

---------

Co-authored-by: xzy <xzy@mbp-m5.local>
Co-authored-by: jackwener <jakevingoo@gmail.com>
2026-04-08 00:28:59 +08:00

95 lines
2.4 KiB
TypeScript

import { cli, Strategy } from '@jackwener/opencli/registry';
import type { IPage } from '@jackwener/opencli/types';
import {
findFolder,
formatSize,
listMyDrive,
} from './utils.js';
interface DriveNode {
name: string;
fid: string;
is_dir: boolean;
size: string;
path: string;
children?: DriveNode[];
}
async function buildTree(
page: IPage,
pdirFid: string,
parentPath: string,
depth: number,
maxDepth: number,
dirsOnly: boolean,
): Promise<DriveNode[]> {
if (depth > maxDepth) return [];
const files = await listMyDrive(page, pdirFid);
const nodes: DriveNode[] = [];
for (const file of files) {
if (dirsOnly && !file.dir) continue;
const path = parentPath ? `${parentPath}/${file.file_name}` : file.file_name;
const node: DriveNode = {
name: file.file_name,
fid: file.fid,
is_dir: file.dir,
size: formatSize(file.size),
path,
};
if (file.dir && depth < maxDepth) {
node.children = await buildTree(page, file.fid, path, depth + 1, maxDepth, dirsOnly);
}
nodes.push(node);
}
return nodes;
}
function flattenTree(nodes: DriveNode[], level = 0): Record<string, unknown>[] {
const result: Record<string, unknown>[] = [];
const indent = ' '.repeat(level);
for (const node of nodes) {
result.push({
name: `${indent}${node.name}`,
fid: node.fid,
is_dir: node.is_dir,
size: node.size,
path: node.path,
});
if (node.children) {
result.push(...flattenTree(node.children, level + 1));
}
}
return result;
}
cli({
site: 'quark',
name: 'ls',
description: 'List files in your Quark Drive',
domain: 'pan.quark.cn',
strategy: Strategy.COOKIE,
args: [
{ name: 'path', positional: true, default: '', help: 'Folder path to list (empty for root)' },
{ name: 'depth', type: 'int', default: 0, help: 'Max depth to traverse' },
{ name: 'dirs-only', type: 'boolean', default: false, help: 'Show directories only' },
],
columns: ['name', 'is_dir', 'size', 'fid', 'path'],
func: async (page, kwargs) => {
const path = (kwargs.path as string) ?? '';
const depth = Math.max(0, (kwargs.depth as number) ?? 0);
const dirsOnly = (kwargs['dirs-only'] as boolean) ?? false;
const rootFid = path ? await findFolder(page, path) : '0';
const tree = await buildTree(page, rootFid, path, 0, depth, dirsOnly);
return flattenTree(tree);
},
});