summaryrefslogtreecommitdiff
path: root/src/cli/fileTree.ts
diff options
context:
space:
mode:
authorYuren Hao <97327730+YurenHao0426@users.noreply.github.com>2026-09-13 11:07:18 +0700
committerGitHub <noreply@github.com>2026-09-13 11:07:18 +0700
commitc78a5ef589cacb5d7bdc7586b0cc270aded4b5fc (patch)
tree69877995c0ecad3eb7266b1bb282c11a6f16dcdf /src/cli/fileTree.ts
parent34f22876ab9e15e9f14300a8c21cd7109800d1ab (diff)
parent1e81ba361eed444442fa98b0770e7ed78dae864b (diff)
Merge pull request #1 from YurenHao0426/agent-cli
Headless CLI mode for agents
Diffstat (limited to 'src/cli/fileTree.ts')
-rw-r--r--src/cli/fileTree.ts69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/cli/fileTree.ts b/src/cli/fileTree.ts
new file mode 100644
index 0000000..736b1e1
--- /dev/null
+++ b/src/cli/fileTree.ts
@@ -0,0 +1,69 @@
+// Walk Overleaf project root folder into flat maps (same logic as main/index.ts walkRootFolder)
+
+export interface FileTreeEntry {
+ name: string
+ path: string // relative path, forward slashes
+ isDir: boolean
+ docId?: string // text docs
+ fileRefId?: string // binary files
+}
+
+export interface FileTreeResult {
+ entries: FileTreeEntry[]
+ docPathMap: Record<string, string> // docId → relPath
+ pathDocMap: Record<string, string> // relPath → docId
+ fileRefs: Array<{ id: string; path: string }>
+ folderMap: Record<string, string> // folderId → relPath ('' for root)
+ pathFolderMap: Record<string, string> // relPath → folderId
+ rootFolderId: string
+}
+
+interface FolderLike {
+ _id: string
+ name: string
+ docs?: Array<{ _id: string; name: string }>
+ fileRefs?: Array<{ _id: string; name: string }>
+ folders?: FolderLike[]
+}
+
+export function walkRootFolder(rootFolder: FolderLike[]): FileTreeResult {
+ const docPathMap: Record<string, string> = {}
+ const pathDocMap: Record<string, string> = {}
+ const fileRefs: Array<{ id: string; path: string }> = []
+ const folderMap: Record<string, string> = {}
+ const pathFolderMap: Record<string, string> = {}
+ const entries: FileTreeEntry[] = []
+ const rootFolderId = rootFolder[0]?._id || ''
+
+ function walk(f: FolderLike, prefix: string): void {
+ // Register this folder
+ const folderPath = prefix ? prefix.slice(0, -1) : '' // remove trailing /
+ folderMap[f._id] = folderPath
+ pathFolderMap[folderPath] = f._id
+
+ for (const doc of f.docs || []) {
+ const relPath = prefix + doc.name
+ docPathMap[doc._id] = relPath
+ pathDocMap[relPath] = doc._id
+ entries.push({ name: doc.name, path: relPath, isDir: false, docId: doc._id })
+ }
+
+ for (const ref of f.fileRefs || []) {
+ const relPath = prefix + ref.name
+ fileRefs.push({ id: ref._id, path: relPath })
+ entries.push({ name: ref.name, path: relPath, isDir: false, fileRefId: ref._id })
+ }
+
+ for (const sub of f.folders || []) {
+ const subPrefix = prefix + sub.name + '/'
+ entries.push({ name: sub.name, path: subPrefix.slice(0, -1), isDir: true })
+ walk(sub, subPrefix)
+ }
+ }
+
+ if (rootFolder[0]) {
+ walk(rootFolder[0], '')
+ }
+
+ return { entries, docPathMap, pathDocMap, fileRefs, folderMap, pathFolderMap, rootFolderId }
+}