From 7c8b2f2bd1d9ab3d9c822bf685636ac62e83dd4e Mon Sep 17 00:00:00 2001 From: Ross Blair Date: Wed, 4 Sep 2024 13:37:41 -0500 Subject: [PATCH 1/4] incomplete --- bids-validator/src/files/outfile.ts | 16 +++++++++++++++ bids-validator/src/main.ts | 32 ++++++++++++----------------- bids-validator/src/setup/options.ts | 5 +++++ bids-validator/src/utils/output.ts | 14 +++++++++++++ 4 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 bids-validator/src/files/outfile.ts diff --git a/bids-validator/src/files/outfile.ts b/bids-validator/src/files/outfile.ts new file mode 100644 index 000000000..ff1b7e78c --- /dev/null +++ b/bids-validator/src/files/outfile.ts @@ -0,0 +1,16 @@ +export function stringToFile(s: string, path: string) { + if (globalThis.Deno) { + denoStringToFile(s, path) + } else { + browserStringToFile(s, path) + } +} + +function denoStringToFile(s: string, path: string) { + Deno.writeTextFileSync(s, path) +} + +/* path doesn't make much sense here. Should be just a filename. */ +function browserStringToFile(s: string, path: string) { + console.log(s) +} diff --git a/bids-validator/src/main.ts b/bids-validator/src/main.ts index aa48cbd4a..2d767e046 100644 --- a/bids-validator/src/main.ts +++ b/bids-validator/src/main.ts @@ -3,9 +3,10 @@ import type { Config } from './setup/options.ts' import * as colors from '@std/fmt/colors' import { readFileTree } from './files/deno.ts' import { fileListToTree } from './files/browser.ts' +import { stringToFile } from './files/outfile.ts' import { resolve } from '@std/path' import { validate } from './validators/bids.ts' -import { consoleFormat } from './utils/output.ts' +import { consoleFormat, resultToJSONStr } from './utils/output.ts' import { setupLogging } from './utils/logger.ts' import type { ValidationResult } from './types/validation-result.ts' @@ -27,26 +28,19 @@ export async function main(): Promise { // Run the schema based validator const schemaResult = await validate(tree, options, config) + let output_string = "" if (options.json) { - console.log( - JSON.stringify(schemaResult, (key, value) => { - if (value?.parent) { - // Remove parent reference to avoid circular references - value.parent = undefined - } - if (value instanceof Map) { - return Object.fromEntries(value) - } else { - return value - } - }), - ) + output_string = resultToJSONStr(schemaResult) } else { - console.log( - consoleFormat(schemaResult, { - verbose: options.verbose ? options.verbose : false, - }), - ) + output_string = consoleFormat(schemaResult, { + verbose: options.verbose ? options.verbose : false, + }) + } + + if (options.outfile) { + stringToFile(output_string, options.outfile) + } else { + console.log(output_string) } return schemaResult diff --git a/bids-validator/src/setup/options.ts b/bids-validator/src/setup/options.ts index 67ddaa703..12ad4795a 100644 --- a/bids-validator/src/setup/options.ts +++ b/bids-validator/src/setup/options.ts @@ -26,6 +26,7 @@ export type ValidatorOptions = { debug: LevelName color?: boolean recursive?: boolean + outfile?: string blacklistModalities: string[] } @@ -70,6 +71,10 @@ export const validateCommand = new Command() '-r, --recursive', 'Validate datasets found in derivatives directories in addition to root dataset', ) + .option( + '-o, --outfile ', + 'File to write validation results to. Implies --json.', + ) // Disabling color output is only available in Deno if (typeof Deno !== 'undefined') { diff --git a/bids-validator/src/utils/output.ts b/bids-validator/src/utils/output.ts index 81499330d..30b21fe65 100644 --- a/bids-validator/src/utils/output.ts +++ b/bids-validator/src/utils/output.ts @@ -170,3 +170,17 @@ function helpUrl(code: string): string { // Provide a link to NeuroStars return `https://neurostars.org/search?q=${code}` } + +export function resultToJSONStr(result: ValidationResult) { + return JSON.stringify(result, (key, value) => { + if (value?.parent) { + // Remove parent reference to avoid circular references + value.parent = undefined + } + if (value instanceof Map) { + return Object.fromEntries(value) + } else { + return value + } + }) +} From 020c550dc2a511a182d0d7bb31698dbfe035e83d Mon Sep 17 00:00:00 2001 From: Ross Blair Date: Mon, 9 Sep 2024 10:19:37 -0500 Subject: [PATCH 2/4] have console error and log messages from nifti library go to logger at info level --- bids-validator/src/files/nifti.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bids-validator/src/files/nifti.ts b/bids-validator/src/files/nifti.ts index 356163bfc..ee98ec3c6 100644 --- a/bids-validator/src/files/nifti.ts +++ b/bids-validator/src/files/nifti.ts @@ -29,9 +29,12 @@ async function extract(buffer: Uint8Array, nbytes: number): Promise function readHeaderQuiet(buf: ArrayBuffer) { const console_error = console.error - console.error = () => {} + const console_log = console.log + console.error = (msg: string) => { logger.info(msg)} + console.log = (msg: string) => { logger.info(msg)} const header = readHeader(buf) console.error = console_error + console.log = console_log return header } From 798302259e5df263cdd5d02d60d80f64654291fe Mon Sep 17 00:00:00 2001 From: Ross Blair Date: Mon, 9 Sep 2024 10:30:27 -0500 Subject: [PATCH 3/4] Don't try and support browsers with output file option --- bids-validator/src/files/outfile.ts | 16 ---------------- bids-validator/src/main.ts | 10 +++++++--- 2 files changed, 7 insertions(+), 19 deletions(-) delete mode 100644 bids-validator/src/files/outfile.ts diff --git a/bids-validator/src/files/outfile.ts b/bids-validator/src/files/outfile.ts deleted file mode 100644 index 0fa3a2415..000000000 --- a/bids-validator/src/files/outfile.ts +++ /dev/null @@ -1,16 +0,0 @@ -export function stringToFile(s: string, path: string) { - if (globalThis.Deno) { - denoStringToFile(s, path) - } else { - browserStringToFile(s, path) - } -} - -function denoStringToFile(s: string, path: string) { - Deno.writeTextFileSync(path, s) -} - -/* path doesn't make much sense here. Should be just a filename. */ -function browserStringToFile(s: string, path: string) { - console.log(s) -} diff --git a/bids-validator/src/main.ts b/bids-validator/src/main.ts index 2d767e046..adb9f0aa2 100644 --- a/bids-validator/src/main.ts +++ b/bids-validator/src/main.ts @@ -3,7 +3,6 @@ import type { Config } from './setup/options.ts' import * as colors from '@std/fmt/colors' import { readFileTree } from './files/deno.ts' import { fileListToTree } from './files/browser.ts' -import { stringToFile } from './files/outfile.ts' import { resolve } from '@std/path' import { validate } from './validators/bids.ts' import { consoleFormat, resultToJSONStr } from './utils/output.ts' @@ -30,7 +29,7 @@ export async function main(): Promise { let output_string = "" if (options.json) { - output_string = resultToJSONStr(schemaResult) + output_string = resultToJSONStr(schemaResult) } else { output_string = consoleFormat(schemaResult, { verbose: options.verbose ? options.verbose : false, @@ -38,7 +37,12 @@ export async function main(): Promise { } if (options.outfile) { - stringToFile(output_string, options.outfile) + if (globalThis.Deno) { + Deno.writeTextFileSync(options.outfile, output_string) + } else { + console.error("Output to file only supported in Deno runtime") + console.log(output_string) + } } else { console.log(output_string) } From 2381a3a7b2c07d466d85df8f5612496547494033 Mon Sep 17 00:00:00 2001 From: Ross Blair Date: Mon, 9 Sep 2024 10:37:39 -0500 Subject: [PATCH 4/4] copied the wrong import for logger to use in nifti.ts --- bids-validator/src/files/nifti.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bids-validator/src/files/nifti.ts b/bids-validator/src/files/nifti.ts index ee98ec3c6..41f498310 100644 --- a/bids-validator/src/files/nifti.ts +++ b/bids-validator/src/files/nifti.ts @@ -1,6 +1,6 @@ import { isCompressed, readHeader } from '@mango/nifti' import type { BIDSFile } from '../types/filetree.ts' -import type { logger } from '../utils/logger.ts' +import { logger } from '../utils/logger.ts' import type { NiftiHeader } from '@bids/schema/context' async function extract(buffer: Uint8Array, nbytes: number): Promise {