Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!--
A new scriv changelog fragment.

Uncomment the section that is right (remove the HTML comment wrapper).
For top level release notes, leave all the headers commented out.
-->

<!--
### Added

- A bullet item for the Added category.

-->
<!--
### Changed

- A bullet item for the Changed category.

-->
### Fixed

- CLI and web validator now default to loading '.bids-validator-config.json'. Validator only attempts this if no other config file was passed in.

<!--
### Deprecated

- A bullet item for the Deprecated category.

-->
<!--
### Removed

- A bullet item for the Removed category.

-->
<!--
### Security

- A bullet item for the Security category.

-->
<!--
### Infrastructure

- A bullet item for the Infrastructure category.

-->
19 changes: 17 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import * as colors from '@std/fmt/colors'
import { readFileTree } from './files/deno.ts'
import { fileListToTree } from './files/browser.ts'
import { FileIgnoreRules } from './files/ignore.ts'
import { resolve } from '@std/path'
import { join, resolve } from '@std/path'
import { exists } from "jsr:@std/fs/exists";
import { validate } from './validators/bids.ts'
import { consoleFormat, resultToJSONStr } from './utils/output.ts'
import { setupLogging } from './utils/logger.ts'
Expand All @@ -28,7 +29,21 @@ export async function main(): Promise<ValidationResult> {
: undefined
const tree = await readFileTree(absolutePath, prune, options.preferredRemote)

const config = options.config ? JSON.parse(Deno.readTextFileSync(options.config)) as Config : {}
let config = {}
if (options.config) {
config = JSON.parse(Deno.readTextFileSync(options.config))
} else {
const defaultConfig = join(absolutePath, '.bids-validator-config.json')
try {
await Deno.lstat(defaultConfig)
config = JSON.parse(Deno.readTextFileSync(defaultConfig))
options.config = defaultConfig
} catch {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
Comment on lines +36 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we did what we do for .bidsignore and pull it out of the tree? We don't need a filesystem hit to check if it's there.

Suggested change
const defaultConfig = join(absolutePath, '.bids-validator-config.json')
try {
await Deno.lstat(defaultConfig)
config = JSON.parse(Deno.readTextFileSync(defaultConfig))
options.config = defaultConfig
} catch {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
const configFile = tree.get('.bids-validator-config.json') as BIDSFile
if (configFile) {
config = await configFile.text().then((text) => JSON.parse(text))
}

We could also use our JSON loader and create an issue. Probably better not to crash...

}

// Run the schema based validator
const schemaResult = await validate(tree, options, config)
Expand Down
12 changes: 10 additions & 2 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from "react"
import "./App.css"
import { directoryOpen } from "https://esm.sh/[email protected]"
import { directoryOpen, fileOpen } from "https://esm.sh/[email protected]"
import confetti from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/confetti.module.mjs';
import { fileListToTree, validate, getVersion } from "../dist/validator/main.js"
import type { ValidationResult } from "../../src/types/validation-result.ts"
Expand Down Expand Up @@ -50,11 +50,19 @@ function App() {
const [validation, setValidation] = useState<ValidationResult>()

async function validateDir() {

const dirHandle = await directoryOpen({
recursive: true,
})
let config = {}
const configFile = dirHandle.find(file => file.name ==='.bids-validator-config.json')
if (configFile) {
config = configFile.text().then(text => JSON.parse(text)).catch((err) => {
alert(`Failed to load ".bids-validator-config.json". \n\nUsing empty configuration object:\n\n${err}`)
})
}
const fileTree = await fileListToTree(dirHandle)
Comment on lines +57 to 64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly:

Suggested change
let config = {}
const configFile = dirHandle.find(file => file.name ==='.bids-validator-config.json')
if (configFile) {
config = configFile.text().then(text => JSON.parse(text)).catch((err) => {
alert(`Failed to load ".bids-validator-config.json". \n\nUsing empty configuration object:\n\n${err}`)
})
}
const fileTree = await fileListToTree(dirHandle)
const fileTree = await fileListToTree(dirHandle)
let config = {}
const configFile = fileTree.get('.bids-validator-config.json') as BIDSFile
if (configFile) {
config = await configFile.text().then(text => JSON.parse(text)).catch((err) => {
alert(`Failed to load ".bids-validator-config.json". \n\nUsing empty configuration object:\n\n${err}`)
})
}

setValidation(await validate(fileTree, {}))
setValidation(await validate(fileTree, config))
}

const [version, setVersion] = useState<string>()
Expand Down
Loading