import { CategoryData , ScriptData , CategoryOrScriptData , InstructionHolder , } from 'js-yaml-loader!@/*' ; import { Script } from '@/domain/Script' ; import { Category } from '@/domain/Category' ; import { parseDocUrls } from './DocumentationParser' ; import { ICategoryCollectionParseContext } from './Script/ICategoryCollectionParseContext' ; import { parseScript } from './Script/ScriptParser' ; let categoryIdCounter = 0 ; export function parseCategory ( category , context ) { if ( ! context ) { throw new Error ( 'undefined context' ) ; } ensureValid ( category ) ; const children = { subCategories : new Array < Category > ( ) , subScripts : new Array < Script > ( ) , } ; for ( const data of category . children ) { parseCategoryChild ( data , children , category , context ) ; } return new Category ( categoryIdCounter ++ , category . category , parseDocUrls ( category ) , children . subCategories , children . subScripts ) ; } function ensureValid ( category ) { if ( ! category ) { throw Error ( 'category is null or undefined' ) ; } if ( ! category . children || category . children . length === 0 ) { throw Error ( `category has no children: "${ category . category }"` ) ; } if ( ! category . category || category . category . length === 0 ) { throw Error ( 'category has no name' ) ; } } interface ICategoryChildren { subCategories : Category [ ] ; subScripts : Script [ ] ; } function parseCategoryChild ( data , children , parent , context ) { if ( isCategory ( data ) ) { const subCategory = parseCategory ( data as CategoryData , context ) ; children . subCategories . push ( subCategory ) ; } else if ( isScript ( data ) ) { const scriptData = data as ScriptData ; const script = parseScript ( scriptData , context ) ; children . subScripts . push ( script ) ; } else { throw new Error ( `Child element is neither a category or a script.
                Parent: ${ parent . category }, element: ${ JSON . stringify ( data ) }` ) ; } } function isScript ( data ) { const holder = ( data as InstructionHolder ) ; return hasCode ( holder ) || hasCall ( holder ) ; } function isCategory ( data ) { const { category } = data as CategoryData ; return category && category . length > 0 ; } function hasCode ( holder ) { return holder . code && holder . code . length > 0 ; } function hasCall ( holder ) { return holder . call !== undefined ; }