import { ScriptData } from 'js-yaml-loader!@/*' ; import { Script } from '@/domain/Script' ; import { RecommendationLevel } from '@/domain/RecommendationLevel' ; import { IScriptCode } from '@/domain/IScriptCode' ; import { ScriptCode } from '@/domain/ScriptCode' ; import { parseDocUrls } from '../DocumentationParser' ; import { createEnumParser , IEnumParser } from '../../Common/Enum' ; import { ICategoryCollectionParseContext } from './ICategoryCollectionParseContext' ; export function parseScript ( data , context , levelParser = createEnumParser ( RecommendationLevel ) ) { validateScript ( data ) ; if ( ! context ) { throw new Error ( 'undefined context' ) ; } const script = new Script ( data . name , parseCode ( data , context ) , parseDocUrls ( data ) , parseLevel ( data . recommend , levelParser ) ) ; return script ; } function parseLevel ( level , parser ) { if ( ! level ) { return undefined ; } return parser . parseEnum ( level , 'level' ) ; } function parseCode ( script , context ) { if ( context . compiler . canCompile ( script ) ) { return context . compiler . compile ( script ) ; } return new ScriptCode ( script . code , script . revertCode , context . syntax ) ; } function ensureNotBothCallAndCode ( script ) { if ( script . code && script . call ) { throw new Error ( 'cannot define both "call" and "code"' ) ; } if ( script . revertCode && script . call ) { throw new Error ( 'cannot define "revertCode" if "call" is defined' ) ; } } function validateScript ( script ) { if ( ! script ) { throw new Error ( 'undefined script' ) ; } if ( ! script . code && ! script . call ) { throw new Error ( 'must define either "call" or "code"' ) ; } ensureNotBothCallAndCode ( script ) ; }