-
Notifications
You must be signed in to change notification settings - Fork 520
Expand file tree
/
Copy pathproject-file-tree.ts
More file actions
248 lines (212 loc) · 6.84 KB
/
project-file-tree.ts
File metadata and controls
248 lines (212 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import path from 'path'
import * as ignore from 'ignore'
import { DEFAULT_IGNORED_PATHS } from './old-constants'
import { isValidProjectRoot } from './util/file'
import type { CodebuffFileSystem } from './types/filesystem'
import type { DirectoryNode, FileTreeNode } from './util/file'
export const DEFAULT_MAX_FILES = 10_000
export function getProjectFileTree(params: {
projectRoot: string
maxFiles?: number
fs: CodebuffFileSystem
}): FileTreeNode[] {
const withDefaults = { maxFiles: DEFAULT_MAX_FILES, ...params }
const { projectRoot, fs } = withDefaults
let { maxFiles } = withDefaults
const start = Date.now()
const defaultIgnore = ignore.default()
for (const pattern of DEFAULT_IGNORED_PATHS) {
defaultIgnore.add(pattern)
}
if (!isValidProjectRoot(projectRoot)) {
defaultIgnore.add('.*')
maxFiles = 0
}
const root: DirectoryNode = {
name: path.basename(projectRoot),
type: 'directory',
children: [],
filePath: '',
}
const queue: {
node: DirectoryNode
fullPath: string
ignore: ignore.Ignore
}[] = [
{
node: root,
fullPath: projectRoot,
ignore: defaultIgnore,
},
]
let totalFiles = 0
while (queue.length > 0 && totalFiles < maxFiles) {
const { node, fullPath, ignore: currentIgnore } = queue.shift()!
const mergedIgnore = ignore
.default()
.add(currentIgnore)
.add(parseGitignore({ fullDirPath: fullPath, projectRoot, fs }))
try {
const files = fs.readdirSync(fullPath)
for (const file of files) {
if (totalFiles >= maxFiles) break
const filePath = path.join(fullPath, file)
const relativeFilePath = path.relative(projectRoot, filePath)
if (mergedIgnore.ignores(relativeFilePath)) continue
try {
const stats = fs.statSync(filePath)
if (stats.isDirectory()) {
const childNode: DirectoryNode = {
name: file,
type: 'directory',
children: [],
filePath: relativeFilePath,
}
node.children.push(childNode)
queue.push({
node: childNode,
fullPath: filePath,
ignore: mergedIgnore,
})
} else {
const lastReadTime = stats.atimeMs
node.children.push({
name: file,
type: 'file',
lastReadTime,
filePath: relativeFilePath,
})
totalFiles++
}
} catch (error: any) {
// Don't print errors, you probably just don't have access to the file.
}
}
} catch (error: any) {
// Don't print errors, you probably just don't have access to the directory.
}
}
return root.children
}
function rebaseGitignorePattern(
rawPattern: string,
relativeDirPath: string,
): string {
// Preserve negation and directory-only flags
const isNegated = rawPattern.startsWith('!')
let pattern = isNegated ? rawPattern.slice(1) : rawPattern
const dirOnly = pattern.endsWith('/')
// Strip the trailing slash for slash-detection only
const core = dirOnly ? pattern.slice(0, -1) : pattern
const anchored = core.startsWith('/') // anchored to .gitignore dir
// Detect if the "meaningful" part (minus optional leading '/' and trailing '/')
// contains a slash. If not, git treats it as recursive.
const coreNoLead = anchored ? core.slice(1) : core
const hasSlash = coreNoLead.includes('/')
// Build the base (where this .gitignore lives relative to projectRoot)
const base = relativeDirPath.replace(/\\/g, '/') // normalize
let rebased: string
if (anchored) {
// "/foo" from evals/.gitignore -> "evals/foo"
rebased = base ? `${base}/${coreNoLead}` : coreNoLead
} else if (!hasSlash) {
// "logs" or "logs/" should recurse from evals/: "evals/**/logs[/]"
if (base) {
rebased = `${base}/**/${coreNoLead}`
} else {
// At project root already; "logs" stays "logs" to keep recursive semantics
rebased = coreNoLead
}
} else {
// "foo/bar" relative to evals/: "evals/foo/bar"
rebased = base ? `${base}/${coreNoLead}` : coreNoLead
}
if (dirOnly && !rebased.endsWith('/')) {
rebased += '/'
}
// Normalize to forward slashes
rebased = rebased.replace(/\\/g, '/')
return isNegated ? `!${rebased}` : rebased
}
export function parseGitignore(params: {
fullDirPath: string
projectRoot: string
fs: CodebuffFileSystem
}): ignore.Ignore {
const { fullDirPath, projectRoot, fs } = params
const ig = ignore.default()
const relativeDirPath = path.relative(projectRoot, fullDirPath)
const ignoreFiles = [
path.join(fullDirPath, '.gitignore'),
path.join(fullDirPath, '.codebuffignore'),
path.join(fullDirPath, '.manicodeignore'), // Legacy support
]
for (const ignoreFilePath of ignoreFiles) {
if (!fs.existsSync(ignoreFilePath)) continue
const ignoreContent = fs.readFileSync(ignoreFilePath, 'utf8')
const lines = ignoreContent.split('\n')
for (let line of lines) {
line = line.trim()
if (line === '' || line.startsWith('#')) continue
const finalPattern = rebaseGitignorePattern(line, relativeDirPath)
ig.add(finalPattern)
}
}
return ig
}
export function getAllFilePaths(
nodes: FileTreeNode[],
basePath: string = '',
): string[] {
return nodes.flatMap((node) => {
if (node.type === 'file') {
return [path.join(basePath, node.name)]
}
return getAllFilePaths(node.children || [], path.join(basePath, node.name))
})
}
export function flattenTree(nodes: FileTreeNode[]): FileTreeNode[] {
return nodes.flatMap((node) => {
if (node.type === 'file') {
return [node]
}
return flattenTree(node.children ?? [])
})
}
export function getLastReadFilePaths(
flattenedNodes: FileTreeNode[],
count: number,
) {
return flattenedNodes
.filter((node) => node.lastReadTime)
.sort((a, b) => (a.lastReadTime || 0) - (b.lastReadTime || 0))
.reverse()
.slice(0, count)
.map((node) => node.filePath)
}
export function isFileIgnored(params: {
filePath: string
projectRoot: string
fs: CodebuffFileSystem
}): boolean {
const { filePath, projectRoot, fs } = params
const defaultIgnore = ignore.default()
for (const pattern of DEFAULT_IGNORED_PATHS) {
defaultIgnore.add(pattern)
}
const relativeFilePath = path.relative(
projectRoot,
path.join(projectRoot, filePath),
)
const dirPath = path.dirname(path.join(projectRoot, filePath))
// Get ignore patterns from the directory containing the file and all parent directories
const mergedIgnore = ignore.default().add(defaultIgnore)
let currentDir = dirPath
while (currentDir.startsWith(projectRoot)) {
mergedIgnore.add(
parseGitignore({ fullDirPath: currentDir, projectRoot, fs }),
)
currentDir = path.dirname(currentDir)
}
return mergedIgnore.ignores(relativeFilePath)
}