Skip to content

Commit 646abea

Browse files
committed
feat: Pass ignore to filesToTree function
This ensures that consistent ignores are created for web and test file trees.
1 parent 116e7cb commit 646abea

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/files/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class BIDSFileBrowser implements BIDSFile {
5252
export async function fileListToTree(files: File[]): Promise<FileTree> {
5353
const ignore = new FileIgnoreRules([])
5454
const root = new FileTree('/', '/', undefined)
55-
const tree = filesToTree(files.map((f) => new BIDSFileBrowser(f, ignore, root)))
55+
const tree = filesToTree(files.map((f) => new BIDSFileBrowser(f, ignore, root)), ignore)
5656
const bidsignore = tree.get('.bidsignore')
5757
if (bidsignore) {
5858
try {

src/files/filetree.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assert, assertEquals } from '@std/assert'
22
import type { FileIgnoreRules } from './ignore.ts'
3-
import type { FileTree } from '../types/filetree.ts'
3+
import type { BIDSFile, FileTree } from '../types/filetree.ts'
44
import { filesToTree, pathsToTree } from './filetree.ts'
55

66
Deno.test('FileTree generation', async (t) => {
@@ -50,4 +50,19 @@ Deno.test('FileTree generation', async (t) => {
5050
assert(tree.contains(['sub-01', 'anat']))
5151
assert(tree.contains(['sub-01', 'anat', 'sub-01_T1w.nii.gz']))
5252
})
53+
await t.step('converts a list with ignores', async () => {
54+
const tree = pathsToTree(
55+
['/dataset_description.json', '/README.md', '/.bidsignore', '/bad_file'],
56+
['bad_file'],
57+
)
58+
assertEquals(tree.directories, [])
59+
assertEquals(tree.files.map((f) => f.name), [
60+
'dataset_description.json',
61+
'README.md',
62+
'.bidsignore',
63+
'bad_file',
64+
])
65+
const bad_file = tree.get('bad_file') as BIDSFile
66+
assert(bad_file.ignored)
67+
})
5368
})

src/files/filetree.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import { parse, SEPARATOR_PATTERN } from '@std/path'
22
import * as posix from '@std/path/posix'
33
import { type BIDSFile, FileTree } from '../types/filetree.ts'
4+
import { FileIgnoreRules } from './ignore.ts'
45

56
const nullFile = {
67
size: 0,
7-
ignored: false,
88
stream: new ReadableStream(),
99
text: () => Promise.resolve(''),
1010
readBytes: async (size: number, offset?: number) => new Uint8Array(),
1111
parent: new FileTree('', '/'),
1212
viewed: false,
1313
}
1414

15-
export function pathToFile(path: string): BIDSFile {
15+
export function pathToFile(path: string, ignored: boolean = false): BIDSFile {
1616
const name = path.split('/').pop() as string
17-
return { name, path, ...nullFile }
17+
return { name, path, ignored, ...nullFile }
1818
}
1919

20-
export function pathsToTree(paths: string[]): FileTree {
21-
return filesToTree(paths.map(pathToFile))
20+
export function pathsToTree(paths: string[], ignore?: string[]): FileTree {
21+
const ignoreRules = new FileIgnoreRules(ignore ?? [])
22+
return filesToTree(paths.map((path) => pathToFile(path, ignoreRules.test(path))))
2223
}
2324

24-
export function filesToTree(fileList: BIDSFile[]): FileTree {
25+
export function filesToTree(fileList: BIDSFile[], ignore?: FileIgnoreRules): FileTree {
26+
ignore = ignore ?? new FileIgnoreRules([])
2527
const tree: FileTree = new FileTree('/', '/')
2628
for (const file of fileList) {
2729
const parts = parse(file.path)
@@ -37,7 +39,7 @@ export function filesToTree(fileList: BIDSFile[]): FileTree {
3739
current = exists
3840
continue
3941
}
40-
const newTree = new FileTree(posix.join(current.path, level), level, current)
42+
const newTree = new FileTree(posix.join(current.path, level), level, current, ignore)
4143
current.directories.push(newTree)
4244
current = newTree
4345
}

0 commit comments

Comments
 (0)