Skip to content

Commit

Permalink
feat: Pass ignore to filesToTree function
Browse files Browse the repository at this point in the history
This ensures that consistent ignores are created for web and test file
trees.
  • Loading branch information
effigies committed Nov 14, 2024
1 parent 116e7cb commit 646abea
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/files/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class BIDSFileBrowser implements BIDSFile {
export async function fileListToTree(files: File[]): Promise<FileTree> {
const ignore = new FileIgnoreRules([])
const root = new FileTree('/', '/', undefined)
const tree = filesToTree(files.map((f) => new BIDSFileBrowser(f, ignore, root)))
const tree = filesToTree(files.map((f) => new BIDSFileBrowser(f, ignore, root)), ignore)
const bidsignore = tree.get('.bidsignore')
if (bidsignore) {
try {
Expand Down
17 changes: 16 additions & 1 deletion src/files/filetree.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert, assertEquals } from '@std/assert'
import type { FileIgnoreRules } from './ignore.ts'
import type { FileTree } from '../types/filetree.ts'
import type { BIDSFile, FileTree } from '../types/filetree.ts'
import { filesToTree, pathsToTree } from './filetree.ts'

Deno.test('FileTree generation', async (t) => {
Expand Down Expand Up @@ -50,4 +50,19 @@ Deno.test('FileTree generation', async (t) => {
assert(tree.contains(['sub-01', 'anat']))
assert(tree.contains(['sub-01', 'anat', 'sub-01_T1w.nii.gz']))
})
await t.step('converts a list with ignores', async () => {
const tree = pathsToTree(
['/dataset_description.json', '/README.md', '/.bidsignore', '/bad_file'],
['bad_file'],
)
assertEquals(tree.directories, [])
assertEquals(tree.files.map((f) => f.name), [
'dataset_description.json',
'README.md',
'.bidsignore',
'bad_file',
])
const bad_file = tree.get('bad_file') as BIDSFile
assert(bad_file.ignored)
})
})
16 changes: 9 additions & 7 deletions src/files/filetree.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { parse, SEPARATOR_PATTERN } from '@std/path'
import * as posix from '@std/path/posix'
import { type BIDSFile, FileTree } from '../types/filetree.ts'
import { FileIgnoreRules } from './ignore.ts'

const nullFile = {
size: 0,
ignored: false,
stream: new ReadableStream(),
text: () => Promise.resolve(''),
readBytes: async (size: number, offset?: number) => new Uint8Array(),
parent: new FileTree('', '/'),
viewed: false,
}

export function pathToFile(path: string): BIDSFile {
export function pathToFile(path: string, ignored: boolean = false): BIDSFile {
const name = path.split('/').pop() as string
return { name, path, ...nullFile }
return { name, path, ignored, ...nullFile }
}

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

export function filesToTree(fileList: BIDSFile[]): FileTree {
export function filesToTree(fileList: BIDSFile[], ignore?: FileIgnoreRules): FileTree {
ignore = ignore ?? new FileIgnoreRules([])
const tree: FileTree = new FileTree('/', '/')
for (const file of fileList) {
const parts = parse(file.path)
Expand All @@ -37,7 +39,7 @@ export function filesToTree(fileList: BIDSFile[]): FileTree {
current = exists
continue
}
const newTree = new FileTree(posix.join(current.path, level), level, current)
const newTree = new FileTree(posix.join(current.path, level), level, current, ignore)
current.directories.push(newTree)
current = newTree
}
Expand Down

0 comments on commit 646abea

Please sign in to comment.