nope/lib/ui/helpers.nodejs.ts
Martin Karkowski 20112adbe5 # 1.2.0
- Added:
  - `lib/cli/nope` adding scan for ui service
  - `lib/decorators/container`: Main Container, holding all `methods` and `classes`. Use `getCentralDecoratedContainer()` to get this decorator.
  - `types/nope/nopePackage.interface` added `IClassDescription` which contains the class description used in the Package Description.
  - `logger/nopeLogger`: added methods: `enabledFor`, `enableFor`, `shouldLog`
  - `package.json`: installed types of `ace` text editor.
  - `ui/helpers.browser`: Created `convertInstanceRenderPage` and `IUiDefinition`
  - `ui/helpers.nodejs`: Added a Helper to write the Ui-File (`writeUiFile`) and parse its arguments (`readInwriteUiFileArgs`)
  - `ui/index.*`: Crated the corresponding exports.
- Modified:
  - `lib/decorators/*` Adding the main `container` where every function, service method etc is added. All decorators now safe the decorated elements in the container.
  - `helpers/json`: Adding `BEGIN_STR` and `END_STR` for parsing functions as constants.
  - `logger/eventLogging`: simplify `useEventLogger`
  - `logger/index.browser`: Adating exports.
  - `loader/loadPackages`: Modifing `IPackageConfig` now extends Partial the `IPackageDescription`
  - `types/ui/editor/IEditPage`: adapting Type of `getData` to `T`->`any`. Adapting the return of `getPorts` (The Ports will be generated in the ui then)
  - `types/ui/editor/helpers.interface`: Adapting the `w2ui` and added `w2uiHelpers` and added `ace`. Rearanging `IRenderData` element. to compact the data.
  - `types/ui/editor/render.callbacks`: Rearange the Generic Type of `TRenderInstancePage` and Renaming `TCreatorPage` to `TInstanceManagerPage`. Adapting the `option` of `TInstanceManagerPage` regarding the `createInstance` and `instances`
  - `types/ui/editor/index`: Adapting the Exports.
  - `lib/index.browser`: Exporting `ui` elements
  - `lib/index.nodejs`: Exporting `ui` elements
  - `lib/types/index`: Exporting `ui` elements
- Fixes:
  - `types/nope/nopeInstanceManager.interface`: Fixing Type of createInstance. Now the Type `I` extends `INopeModule` instead of being set to `IGenericNopeModule`
2022-07-02 11:30:10 +02:00

159 lines
4.0 KiB
TypeScript

/**
* @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 "./helpers.browser";
/**
* 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;
}