/** * @author Martin Karkowski * @email m.karkowski@zema.de */ import { ArgumentParser } from "argparse"; import { join, resolve } from "path"; import "reflect-metadata"; import { createFile } from "../helpers/fileMethods"; import { getCentralDecoratedContainer, IFunctionOptions, stringifyWithFunctions, } from "../index.browser"; import { listFunctions, listPackages } from "../index.nodejs"; import { IUiDefinition } from "../types/ui"; /** * Helper Function to write a default configuration. * * @export * @param {string} [dir='./modules'] * @param {string} [filename=join(resolve(process.cwd()), 'config', 'assembly.json')] */ export async function writeUiFile( options: { dir: string; filename: string; } = { dir: "./modules", filename: join(resolve(process.cwd()), "config", "ui.json"), } ) { const uiFile: IUiDefinition = { functions: {}, classes: {}, }; // This call makes shure, that every function is loaded const functions = await listFunctions(options.dir); const packages = await listPackages(options.dir); const CONTAINER = getCentralDecoratedContainer(); // Determine all Packages for (const item of packages) { // Iterate over the classes. for (const cls of item.package.providedClasses) { const itemToAdd: IUiDefinition["classes"][0] = { // The Class Name class: cls.description.name, // The Package Name package: item.package.nameOfPackage, // The Path of he File. path: item.path, // The defined UI defintions. ui: cls.ui, // Define the Methods elements methods: {}, }; // The Service const services = CONTAINER.classes.get(cls.description.name)?._markedElements || []; for (const srv of services) { if (srv.type === "method" && (srv.options as IFunctionOptions).ui) { itemToAdd.methods[srv.accessor] = ( srv.options as IFunctionOptions ).ui; } } if ( itemToAdd.ui || Object.getOwnPropertyNames(itemToAdd.methods).length > 0 ) { // If an ui definition exists, we want // to export it and store it in our file. uiFile.classes[itemToAdd.class] = itemToAdd; } } // Iterate over the functions and provide their uis. item.package.providedFunctions.map((funcs) => { if (funcs.options.ui) { // Store the UI definition in the config file. uiFile.functions[funcs.options.id] = { id: funcs.options.id, ui: funcs.options.ui, }; } }); } for (const [id, data] of CONTAINER.methods.entries()) { if (data.options.ui) { uiFile.functions[id] = { id, ui: data.options.ui, }; } } await createFile(options.filename, stringifyWithFunctions(uiFile, 4)); } /** * Helper to extract the Arguments for the `writeUiFile` function @see writeUiFile * * @author M.Karkowski * @export * @param additionalArguments Arguments added by the nope.cli * @return {*} The Arguments */ export function readInwriteUiFileArgs( additionalArguments: { help: string; type: "string" | "number"; name: string | string; defaultValue?: any; }[] = [] ) { const parser = new ArgumentParser({ // version: "1.0.0", add_help: true, description: "Command Line interface, determines the available Packages.", }); for (const arg of additionalArguments) { parser.add_argument(arg.name, { help: arg.help, default: arg.defaultValue, type: arg.type, }); } parser.add_argument("-f", "--file", { help: "Filename for the configuration.", default: "./config/ui.json", type: "str", dest: "filename", }); parser.add_argument("-d", "--dir", { help: "Directory to search for the ui definitions", default: "./modules", type: "str", dest: "dir", }); const args: { dir: string; filename: string; } = parser.parse_args(); return args; }