From 3ef60b742e2e47bad9f6111ea73d6ef2d45b3e18 Mon Sep 17 00:00:00 2001 From: Ross Blair Date: Tue, 8 Oct 2024 16:33:37 -0500 Subject: [PATCH] make directory ignored attribute look more like file ignored attribute. --- bids-validator/src/files/deno.ts | 10 +++++----- bids-validator/src/files/ignore.ts | 4 ++-- bids-validator/src/types/filetree.ts | 12 +++++++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/bids-validator/src/files/deno.ts b/bids-validator/src/files/deno.ts index 92bbbebcc..519d3460a 100644 --- a/bids-validator/src/files/deno.ts +++ b/bids-validator/src/files/deno.ts @@ -2,6 +2,7 @@ * Deno specific implementation for reading files */ import { basename, join } from '@std/path' +import { existsSync } from '@std/fs' import * as posix from '@std/path/posix' import { type BIDSFile, FileTree } from '../types/filetree.ts' import { requestReadPermission } from '../setup/requestPermissions.ts' @@ -119,7 +120,7 @@ async function _readFileTree( ): Promise { await requestReadPermission() const name = basename(relativePath) - const tree = new FileTree(relativePath, name, parent, ignore.test(join(rootPath, relativePath))) + const tree = new FileTree(relativePath, name, parent, ignore) for await (const dirEntry of Deno.readDir(join(rootPath, relativePath))) { if (dirEntry.isFile || dirEntry.isSymlink) { @@ -129,10 +130,6 @@ async function _readFileTree( ignore, ) file.parent = tree - // For .bidsignore, read in immediately and add the rules - if (dirEntry.name === '.bidsignore') { - ignore.add(await readBidsIgnore(file)) - } tree.files.push(file) } if (dirEntry.isDirectory) { @@ -153,5 +150,8 @@ async function _readFileTree( */ export function readFileTree(rootPath: string): Promise { const ignore = new FileIgnoreRules([]) + if (existsSync(join(rootPath, '.bidsignore'))) { + ignore.add(readBidsIgnore(join(rootPath, '.bidsignore'))) + } return _readFileTree(rootPath, '/', ignore) } diff --git a/bids-validator/src/files/ignore.ts b/bids-validator/src/files/ignore.ts index 6cb1cb358..d16e26010 100644 --- a/bids-validator/src/files/ignore.ts +++ b/bids-validator/src/files/ignore.ts @@ -2,8 +2,8 @@ import type { BIDSFile } from '../types/filetree.ts' import { default as ignore } from '@ignore' import type { Ignore } from '@ignore' -export async function readBidsIgnore(file: BIDSFile) { - const value = await file.text() +export function readBidsIgnore(file: string) { + const value = Deno.readTextFileSync(file) if (value) { const lines = value.split('\n') return lines diff --git a/bids-validator/src/types/filetree.ts b/bids-validator/src/types/filetree.ts index 0e46c3f1e..a250e2cc6 100644 --- a/bids-validator/src/types/filetree.ts +++ b/bids-validator/src/types/filetree.ts @@ -1,3 +1,5 @@ +import { FileIgnoreRules } from '../files/ignore.ts' + export interface BIDSFile { // Filename name: string @@ -29,18 +31,22 @@ export class FileTree { name: string files: BIDSFile[] directories: FileTree[] - ignored: boolean viewed: boolean parent?: FileTree + #ignore: FileIgnoreRules - constructor(path: string, name: string, parent?: FileTree, ignored?: boolean) { + constructor(path: string, name: string, parent?: FileTree, ignore?: FileIgnoreRules) { this.path = path this.files = [] this.directories = [] this.name = name this.parent = parent this.viewed = false - this.ignored = ignored || false + this.#ignore = ignore ?? new FileIgnoreRules([]) + } + + get ignored(): boolean { + return this.#ignore.test(this.path) } _get(parts: string[]): BIDSFile | FileTree | undefined {