Skip to content

Commit 5991dbb

Browse files
authored
Merge pull request #2152 from rwblair/fix/set_ignore_for_dirs
Pass value of ignore test to creation of filetree object.
2 parents ae54efa + ae52740 commit 5991dbb

File tree

7 files changed

+36
-14
lines changed

7 files changed

+36
-14
lines changed

bids-validator/src/files/browser.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ export async function fileListToTree(files: File[]): Promise<FileTree> {
5555
const tree = filesToTree(files.map((f) => new BIDSFileBrowser(f, ignore, root)))
5656
const bidsignore = tree.get('.bidsignore')
5757
if (bidsignore) {
58-
ignore.add(await readBidsIgnore(bidsignore as BIDSFile))
58+
try {
59+
ignore.add(await readBidsIgnore(bidsignore as BIDSFile))
60+
} catch (err) {
61+
console.log(`Failed to read '.bidsignore' file with the following error:\n${err}`)
62+
}
5963
}
6064
return tree
6165
}

bids-validator/src/files/deno.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as posix from '@std/path/posix'
66
import { type BIDSFile, FileTree } from '../types/filetree.ts'
77
import { requestReadPermission } from '../setup/requestPermissions.ts'
88
import { FileIgnoreRules, readBidsIgnore } from './ignore.ts'
9+
import { logger } from '../utils/logger.ts'
910

1011
/**
1112
* Thrown when a text file is decoded as UTF-8 but contains UTF-16 characters
@@ -119,7 +120,7 @@ async function _readFileTree(
119120
): Promise<FileTree> {
120121
await requestReadPermission()
121122
const name = basename(relativePath)
122-
const tree = new FileTree(relativePath, name, parent)
123+
const tree = new FileTree(relativePath, name, parent, ignore)
123124

124125
for await (const dirEntry of Deno.readDir(join(rootPath, relativePath))) {
125126
if (dirEntry.isFile || dirEntry.isSymlink) {
@@ -129,10 +130,6 @@ async function _readFileTree(
129130
ignore,
130131
)
131132
file.parent = tree
132-
// For .bidsignore, read in immediately and add the rules
133-
if (dirEntry.name === '.bidsignore') {
134-
ignore.add(await readBidsIgnore(file))
135-
}
136133
tree.files.push(file)
137134
}
138135
if (dirEntry.isDirectory) {
@@ -151,7 +148,19 @@ async function _readFileTree(
151148
/**
152149
* Read in the target directory structure and return a FileTree
153150
*/
154-
export function readFileTree(rootPath: string): Promise<FileTree> {
151+
export async function readFileTree(rootPath: string): Promise<FileTree> {
155152
const ignore = new FileIgnoreRules([])
153+
try {
154+
const ignoreFile = new BIDSFileDeno(
155+
rootPath,
156+
'.bidsignore',
157+
ignore,
158+
)
159+
ignore.add(await readBidsIgnore(ignoreFile))
160+
} catch (err) {
161+
if (!Object.hasOwn(err, 'code') || err.code !== 'ENOENT') {
162+
logger.error(`Failed to read '.bidsignore' file with the following error:\n${err}`)
163+
}
164+
}
156165
return _readFileTree(rootPath, '/', ignore)
157166
}

bids-validator/src/schema/applyRules.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function evalJsonCheck(
221221
}
222222

223223
if (sidecarRule && !(keyName in context.sidecarKeyOrigin)) {
224-
logger.warning(
224+
logger.warn(
225225
`sidecarKeyOrigin map failed to initialize for ${context.path} on key ${keyName}. Validation caching not active for this key.`,
226226
)
227227
}

bids-validator/src/schema/context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export class BIDSContext implements Context {
225225
if (error.key) {
226226
this.dataset.issues.add({ code: error.key, location: this.file.path })
227227
}
228-
logger.warning(
228+
logger.warn(
229229
`tsv file could not be opened by loadColumns '${this.file.path}'`,
230230
)
231231
logger.debug(error)

bids-validator/src/types/filetree.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { FileIgnoreRules } from '../files/ignore.ts'
2+
13
export interface BIDSFile {
24
// Filename
35
name: string
@@ -29,18 +31,22 @@ export class FileTree {
2931
name: string
3032
files: BIDSFile[]
3133
directories: FileTree[]
32-
ignored: boolean
3334
viewed: boolean
3435
parent?: FileTree
36+
#ignore: FileIgnoreRules
3537

36-
constructor(path: string, name: string, parent?: FileTree, ignored?: boolean) {
38+
constructor(path: string, name: string, parent?: FileTree, ignore?: FileIgnoreRules) {
3739
this.path = path
3840
this.files = []
3941
this.directories = []
4042
this.name = name
4143
this.parent = parent
4244
this.viewed = false
43-
this.ignored = ignored || false
45+
this.#ignore = ignore ?? new FileIgnoreRules([])
46+
}
47+
48+
get ignored(): boolean {
49+
return this.#ignore.test(this.path)
4450
}
4551

4652
_get(parts: string[]): BIDSFile | FileTree | undefined {

bids-validator/src/validators/bids.ts

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export async function validate(
7070
const bidsDerivatives: FileTree[] = []
7171
const nonstdDerivatives: FileTree[] = []
7272
fileTree.directories = fileTree.directories.filter((dir) => {
73+
if (['sourcedata', 'code'].includes(dir.name)) {
74+
return false
75+
}
7376
if (dir.name !== 'derivatives') {
7477
return true
7578
}

bids-validator/src/validators/json.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const compile = memoize(metadataValidator.compile.bind(metadataValidator)
1010

1111
export function setCustomMetadataFormats(schema: Schema): void {
1212
if (typeof schema.objects.formats !== 'object') {
13-
logger.warning(
13+
logger.warn(
1414
`schema.objects.formats missing from schema, format validation disabled.`,
1515
)
1616
return
@@ -19,7 +19,7 @@ export function setCustomMetadataFormats(schema: Schema): void {
1919
for (const key of Object.keys(schemaFormats)) {
2020
const pattern = schemaFormats[key]['pattern']
2121
if (typeof pattern !== 'string') {
22-
logger.warning(
22+
logger.warn(
2323
`schema.objects.formats.${key} pattern missing or invalid. Skipping this format for addition to context json validator`,
2424
)
2525
continue

0 commit comments

Comments
 (0)