|
| 1 | +/** |
| 2 | + * Example of how to use the definition processor utilities |
| 3 | + */ |
| 4 | + |
| 5 | +import { processDefinitions, createDefinitionManager } from './src/utils/definitionProcessor.js'; |
| 6 | +import { buildSchemasFromVersion } from 'hed-validator'; |
| 7 | + |
| 8 | +async function exampleUsage() { |
| 9 | + // Load HED schemas |
| 10 | + const hedSchemas = await buildSchemasFromVersion('8.4.0'); |
| 11 | + |
| 12 | + // Example definition strings |
| 13 | + const definitionStrings = [ |
| 14 | + '(Definition/MyColor, (Red))', |
| 15 | + '(Definition/MyAction, (Move))', |
| 16 | + '(Definition/InvalidDef, Red)' // This will cause an error |
| 17 | + ]; |
| 18 | + |
| 19 | + // Process the definitions - now synchronous! |
| 20 | + console.log('Processing definitions...'); |
| 21 | + const result = processDefinitions(definitionStrings, hedSchemas); |
| 22 | + |
| 23 | + console.log('Result:', { |
| 24 | + success: result.success, |
| 25 | + processedCount: result.processedCount, |
| 26 | + failedCount: result.failedCount, |
| 27 | + issueCount: result.issues.length, |
| 28 | + definitionCount: result.definitionManager.definitions.size |
| 29 | + }); |
| 30 | + |
| 31 | + if (result.issues.length > 0) { |
| 32 | + console.log('Issues found:'); |
| 33 | + result.issues.forEach(issue => { |
| 34 | + console.log(` ${issue.severity}: ${issue.message}`); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + // If successful, the definition manager can be used for validation |
| 39 | + if (result.success) { |
| 40 | + console.log('Definitions in manager:'); |
| 41 | + for (const [name, definition] of result.definitionManager.definitions) { |
| 42 | + console.log(` - ${name}: ${(definition as any).defContents?.normalized || 'No contents'}`); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Only run if this file is executed directly |
| 48 | +if (require.main === module) { |
| 49 | + exampleUsage().catch(console.error); |
| 50 | +} |
| 51 | + |
| 52 | +export { exampleUsage }; |
0 commit comments