2024-07-13 23:02:46 +02:00
import { Operator , OperatorSchema } from "../functions" ;
2024-02-23 23:48:03 +01:00
import i18next from "i18next" ;
2024-07-13 21:20:47 +02:00
const compileTimeOperatorList : { basename : string } [ ] = import . meta . compileTime ( "../compiletime/operatorDescription.ts" ) ; // The will compile to ["impose", "extractPages", etc...]
2024-02-23 23:48:03 +01:00
export async function getOperatorByName ( name : string ) : Promise < typeof Operator | undefined > {
// Check if exists
2024-07-13 21:20:47 +02:00
if ( ! compileTimeOperatorList . find ( e = > e . basename == name ) ) return ;
2024-02-23 23:48:03 +01:00
2024-02-27 21:51:03 +01:00
const loadedModule = await import ( "../functions/" + name + ".ts" ) ;
2024-05-10 23:01:18 +02:00
const operator = loadedModule [ capitalizeFirstLetter ( name ) ] ;
if ( ! operator ) {
throw Error ( "This operator does not export its class in the correct format." )
}
return operator ;
2024-02-23 23:48:03 +01:00
}
2024-07-13 23:02:46 +02:00
export async function getSchemaByName ( name : string ) : Promise < OperatorSchema | undefined > {
// Check if exists
if ( ! compileTimeOperatorList . find ( e = > e . basename == name ) ) return ;
2024-07-14 00:53:57 +02:00
await i18next . loadNamespaces ( name , ( err ) = > { if ( err ) throw err ; } ) ;
2024-07-13 23:02:46 +02:00
const loadedModule = await import ( "../functions/" + name + ".schema.ts" ) ;
const schema = loadedModule . default ;
if ( ! schema ) {
throw Error ( "This operator does not export its class in the correct format." )
}
return schema ;
}
2024-02-23 23:48:03 +01:00
export function listOperatorNames ( ) : string [ ] {
2024-07-13 21:20:47 +02:00
const availableOperators = compileTimeOperatorList . map ( e = > e . basename ) ;
2024-02-23 23:48:03 +01:00
return availableOperators ;
}
function capitalizeFirstLetter ( string : String ) {
return string . charAt ( 0 ) . toUpperCase ( ) + string . slice ( 1 ) ;
}