From 646abea92bea0bf0ff4a262ea7807026f6a2aeb7 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Thu, 14 Nov 2024 15:46:04 -0500 Subject: [PATCH 1/2] feat: Pass ignore to filesToTree function This ensures that consistent ignores are created for web and test file trees. --- src/files/browser.ts | 2 +- src/files/filetree.test.ts | 17 ++++++++++++++++- src/files/filetree.ts | 16 +++++++++------- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/files/browser.ts b/src/files/browser.ts index 4a52b1dc..138c7ee2 100644 --- a/src/files/browser.ts +++ b/src/files/browser.ts @@ -52,7 +52,7 @@ export class BIDSFileBrowser implements BIDSFile { export async function fileListToTree(files: File[]): Promise { 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 { diff --git a/src/files/filetree.test.ts b/src/files/filetree.test.ts index b23307e3..8398e97c 100644 --- a/src/files/filetree.test.ts +++ b/src/files/filetree.test.ts @@ -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) => { @@ -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) + }) }) diff --git a/src/files/filetree.ts b/src/files/filetree.ts index 045eca90..49d187ad 100644 --- a/src/files/filetree.ts +++ b/src/files/filetree.ts @@ -1,10 +1,10 @@ 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(), @@ -12,16 +12,18 @@ const nullFile = { 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) @@ -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 } From d4f133c3d405ae3f14eb6c3d938851ffb5dba356 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Thu, 14 Nov 2024 15:52:00 -0500 Subject: [PATCH 2/2] rf: Uniformize bidsignore reading between web and Deno --- src/files/deno.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/files/deno.ts b/src/files/deno.ts index f3065a1f..3895a0cd 100644 --- a/src/files/deno.ts +++ b/src/files/deno.ts @@ -151,17 +151,14 @@ async function _readFileTree( */ export async function readFileTree(rootPath: string): Promise { const ignore = new FileIgnoreRules([]) - try { - const ignoreFile = new BIDSFileDeno( - rootPath, - '.bidsignore', - ignore, - ) - ignore.add(await readBidsIgnore(ignoreFile)) - } catch (err) { - if (err && typeof err === 'object' && !('code' in err && err.code === 'ENOENT')) { - logger.error(`Failed to read '.bidsignore' file with the following error:\n${err}`) + const tree = await _readFileTree(rootPath, '/', ignore) + const bidsignore = tree.get('.bidsignore') + if (bidsignore) { + try { + ignore.add(await readBidsIgnore(bidsignore as BIDSFile)) + } catch (err) { + console.log(`Failed to read '.bidsignore' file with the following error:\n${err}`) } } - return _readFileTree(rootPath, '/', ignore) + return tree }