-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathwalk.ts
More file actions
59 lines (54 loc) · 1.76 KB
/
walk.ts
File metadata and controls
59 lines (54 loc) · 1.76 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
import { BIDSContext, type BIDSContextDataset } from './context.ts'
import type { BIDSFile, FileTree } from '../types/filetree.ts'
import type { DatasetIssues } from '../issues/datasetIssues.ts'
import { loadTSV } from '../files/tsv.ts'
function* quickWalk(dir: FileTree): Generator<BIDSFile> {
for (const file of dir.files) {
yield file
}
for (const subdir of dir.directories) {
yield* quickWalk(subdir)
}
}
const nullFile = {
stream: new ReadableStream(),
text: async () => '',
readBytes: async (size: number, offset?: number) => new Uint8Array(),
}
function pseudoFile(dir: FileTree, opaque: boolean): BIDSFile {
return {
name: `${dir.name}/`,
path: `${dir.path}/`,
size: opaque ? [...quickWalk(dir)].reduce((acc, file) => acc + file.size, 0) : 0,
ignored: dir.ignored,
parent: dir.parent as FileTree,
viewed: dir.viewed,
...nullFile,
}
}
/** Recursive algorithm for visiting each file in the dataset, creating a context */
async function* _walkFileTree(
fileTree: FileTree,
dsContext: BIDSContextDataset,
): AsyncIterable<BIDSContext> {
for (const file of fileTree.files) {
yield new BIDSContext(file, dsContext)
}
for (const dir of fileTree.directories) {
const pseudo = dsContext.isPseudoFile(dir)
const opaque = pseudo || dsContext.isOpaqueDirectory(dir)
const context = new BIDSContext(pseudoFile(dir, opaque), dsContext)
context.directory = !pseudo
yield context
if (!opaque) {
yield* _walkFileTree(dir, dsContext)
}
}
loadTSV.cache.delete(fileTree.path)
}
/** Walk all files in the dataset and construct a context for each one */
export async function* walkFileTree(
dsContext: BIDSContextDataset,
): AsyncIterable<BIDSContext> {
yield* _walkFileTree(dsContext.tree, dsContext)
}