import { Command } from './command' ; import { CommandMetadata , OptionMetadata } from './types' ; export enum MetadataKey { OPTION = 'mammot:option' , COMMAND = 'mammot:command' } export function addOption ( command , metadata ) { const existing = Reflect . getMetadata ( MetadataKey . OPTION , command ) as OptionMetadata [ ] | undefined ; Reflect . defineMetadata ( MetadataKey . OPTION , [ ... ( existing ?? [ ] ) , metadata ] , command ) ; } export function readCommand ( command ) { const options = ( Reflect . getMetadata ( MetadataKey . OPTION , command ) ?? [ ] ) as OptionMetadata [ ] ; const meta = Reflect . getMetadata ( MetadataKey . COMMAND , command . constructor ) as CommandMetadata | undefined ; if ( ! meta ) { throw new Error ( 'No name found for command!' ) ; } return { options , ... meta , } ; } export function getParamType ( command , index , prop = 'run' ) { const data = Reflect . getMetadata ( 'design:paramtypes' , command , prop ) as unknown [ ] | undefined ; if ( ! data ) { throw new Error ( `Could not get reflection types for ${ Command . name }` ) ; } return data [ index ] ; } export function addCommandData ( command , data ) { Reflect . defineMetadata ( MetadataKey . COMMAND , data , command ) ; }