Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions packages/plugins/src/config/configParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Logger } from "@serenityjs/logger";

import { ConfigPropertyMap } from "./pluginConfig";

/**
* Base class for plugin configuration parsers.
*/
class PluginConfigParser {
/**
* The file extension used for configuration file.
*/
public static fileExtension: string = "";

public static validate<T extends Record<string, unknown>>(
obj: T,
properties: ConfigPropertyMap,
logger?: Logger
): boolean {
// Boolean flag to track validation status
let isValid = true;

Object.entries(properties).forEach(([key, prop]) => {
// Get the value of the property from the object
const value = obj[key];

// Check if the value is missing
if (value == undefined) {
logger?.error(
`Failed to Validate Config: Missing required property: ${String(key)}`
);
isValid = false;
}
// Check if the value is of the expected type
else if (typeof value !== prop.type.name.toLowerCase()) {
logger?.error(
`Failed to Validate Config: Invalid type for property ${String(
key
)}: expected ${prop.type.name.toLowerCase()}, got ${typeof value}`
);
isValid = false;
}
});

return isValid;
}

/**
* Deserializes a plugin configuration from a string.
* @param _ The string form of the config file.
*/
public static deserialize<T extends Record<string, unknown>>(_: string): T {
// Overwrite with actual parsing logic
return {} as T;
}
/**
* Serializes a plugin configuration to a string.
* @param _ The configuration object to serialize.
* @return The serialized configuration string.
*/
public static serialize<T extends Record<string, unknown>>(_: T): string {
// Overwrite with actual serialization logic
return "";
}
}

/**
* JSON configuration parser for plugins.
*/
class PluginJsonConfigParser extends PluginConfigParser {
public static fileExtension: string = "json";

public static deserialize<T extends Record<string, unknown>>(
string: string
): T {
return JSON.parse(string) as T;
}

public static serialize<T extends Record<string, unknown>>(
object: T
): string {
return JSON.stringify(object, null, 2) as string;
}
}

export { PluginConfigParser, PluginJsonConfigParser };
2 changes: 2 additions & 0 deletions packages/plugins/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./pluginConfig";
export * from "./configParser";
Loading
Loading