import { Action , FunctionRepo } from "./index" ; import slugify = require ( "@sindresorhus/slugify" ) ; export class Actions { list : FunctionRepo ; constructor ( ) { this . list = { } ; } get ( name ) { return this . list [ name ] ; } size ( ) { return Object . keys ( this . list ) . length ; } /** provides the names of all registered actions */ names ( ) { return Object . keys ( this . list ) . sort ( ) ; } register ( fileBasedName , action ) { if ( typeof action === "function" ) { this . list [ fileBasedName ] = action ; } else if ( typeof action . default === "function" ) { this . list [ fileBasedName ] = action . default ; } else if ( typeof action === "object" ) { for ( const key of Object . keys ( action ) ) { if ( typeof key !== "string" ) { throw new Error ( `Key "${ key }" has unexpected type "${ typeof key }", expected string` ) ; } this . list [ slugify ( key ) ] = action [ key ] ; } } else { throw new Error ( `Cannot use object of type "${ typeof action }" as an exported action: ${ action }` ) ; } } }