Skip to content

Commit

Permalink
Added plugin CWD for custom/content references
Browse files Browse the repository at this point in the history
  • Loading branch information
mrinc committed Nov 3, 2022
1 parent ed7abfd commit 7947ddb
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 80 deletions.
4 changes: 2 additions & 2 deletions nodejs/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { ErrorMessages } from '../interfaces/static';
export class ConfigBase<PluginConfigType extends IPluginConfig = any>
extends DefaultBase<PluginConfigType> implements IConfig {
readonly _deploymentProfile: string;
constructor(pluginName: string, cwd: string, log: IPluginLogger, deploymentProfile: string) {
super(pluginName, cwd, log);
constructor(pluginName: string, cwd: string, pluginCwd: string, log: IPluginLogger, deploymentProfile: string) {
super(pluginName, cwd, pluginCwd, log);
this._deploymentProfile = deploymentProfile;
}
async createAppConfig(listOfKnownPlugins: Array<string>): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions nodejs/src/events/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { ErrorMessages } from "../interfaces/static";
export class EventsBase<PluginConfigType extends IPluginConfig = any>
extends DefaultBase<PluginConfigType>
{
constructor(pluginName: string, cwd: string, log: IPluginLogger) {
super(pluginName, cwd, log);
constructor(pluginName: string, cwd: string,pluginCwd: string, log: IPluginLogger) {
super(pluginName, cwd, pluginCwd, log);
}
public async onEvent(
callerPluginName: string,
Expand Down
9 changes: 8 additions & 1 deletion nodejs/src/interfaces/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ export class DefaultBaseCore {
public readonly pluginName: string;
public log: IPluginLogger;
protected cwd: string;
protected pluginCwd: string;

constructor(pluginName: string, cwd: string, log: IPluginLogger) {
constructor(
pluginName: string,
cwd: string,
pluginCwd: string,
log: IPluginLogger
) {
this.pluginName = pluginName;
this.cwd = cwd;
this.pluginCwd = pluginCwd;
this.log = log;
}

Expand Down
1 change: 1 addition & 0 deletions nodejs/src/interfaces/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export interface IReadyPlugin {
mappedName: string;
version: string;
pluginFile: string;
pluginDir: string;
installerFile: string | null;
}
12 changes: 9 additions & 3 deletions nodejs/src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export class LoggerBase<PluginConfigType extends IPluginConfig = any>
removedVar[0]
);
//console.log(`:${removedVar[0]}:${referencedVar}`, meta);
if (Tools.isNullOrUndefined(referencedVar)) referencedVar = "*null/undefined*";
if (Tools.isNullOrUndefined(referencedVar))
referencedVar = "*null/undefined*";
else if (Tools.isArray(referencedVar))
referencedVar = (referencedVar as Array<any>)
.map((x) =>
Expand All @@ -43,8 +44,13 @@ export class LoggerBase<PluginConfigType extends IPluginConfig = any>
return outString;
}

constructor(pluginName: string, cwd: string, defaultLogger: IPluginLogger) {
super(pluginName, cwd, defaultLogger);
constructor(
pluginName: string,
cwd: string,
pluginCwd: string,
defaultLogger: IPluginLogger
) {
super(pluginName, cwd, pluginCwd, defaultLogger);
}

public async reportStat(
Expand Down
3 changes: 2 additions & 1 deletion nodejs/src/plugins/config-default/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ export class Config extends ConfigBase<PluginConfig> {
constructor(
pluginName: string,
cwd: string,
pluginCwd: string,
log: IPluginLogger,
deploymentProfile: string
) {
super(pluginName, cwd, log, deploymentProfile);
super(pluginName, cwd, pluginCwd, log, deploymentProfile);
}
private get activeDeploymentProfile(): DeploymentProfiles<DeploymentProfile> {
return (this._appConfig.deploymentProfiles as IDictionary)[
Expand Down
16 changes: 13 additions & 3 deletions nodejs/src/plugins/events-default/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ export class Events extends EventsBase<PluginConfig> {
protected ear!: emitAndReturn;
protected eas!: emitStreamAndReceiveStream;

constructor(pluginName: string, cwd: string, log: IPluginLogger) {
super(pluginName, cwd, log);
constructor(
pluginName: string,
cwd: string,
pluginCwd: string,
log: IPluginLogger
) {
super(pluginName, cwd, pluginCwd, log);

this.emit = new emit(log);
this.ear = new emitAndReturn(log);
Expand Down Expand Up @@ -48,7 +53,12 @@ export class Events extends EventsBase<PluginConfig> {
event: string,
listener: { (args: Array<any>): Promise<any> }
): Promise<void> {
await this.ear.onReturnableEvent(callerPluginName, pluginName, event, listener);
await this.ear.onReturnableEvent(
callerPluginName,
pluginName,
event,
listener
);
}
public async emitEventAndReturn(
callerPluginName: string,
Expand Down
12 changes: 8 additions & 4 deletions nodejs/src/plugins/log-default/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IPluginLogger, LogMeta } from "../../interfaces/logger";
import { LoggerBase } from "../../logger/logger";
import { ConsoleColours } from './colours';
import { ConsoleColours } from "./colours";
import { PluginConfig } from "./sec.config";

export enum LogLevels {
Expand All @@ -16,10 +16,11 @@ export class Logger extends LoggerBase<PluginConfig> {
constructor(
pluginName: string,
cwd: string,
pluginCwd: string,
defaultLogger: IPluginLogger,
mockConsole?: { (level: number, message: string): void }
) {
super(pluginName, cwd, defaultLogger);
super(pluginName, cwd, pluginCwd, defaultLogger);
this._mockedConsole = mockConsole;
if (this._mockedConsole !== undefined) this._mockConsole = true;
}
Expand All @@ -33,7 +34,10 @@ export class Logger extends LoggerBase<PluginConfig> {
let formattedMessage = this.formatLog<T>(message, meta);
formattedMessage = `[${plugin.toUpperCase()}] ${formattedMessage}`;
let func: any = console.debug;
let colour: Array<ConsoleColours> = [ConsoleColours.BgBlack, ConsoleColours.FgWhite];
let colour: Array<ConsoleColours> = [
ConsoleColours.BgBlack,
ConsoleColours.FgWhite,
];
if (level === LogLevels.STAT) {
formattedMessage = `[STAT] ${formattedMessage}`;
colour = [ConsoleColours.BgYellow, ConsoleColours.FgBlack];
Expand All @@ -58,7 +62,7 @@ export class Logger extends LoggerBase<PluginConfig> {
colour = [ConsoleColours.BgRed, ConsoleColours.FgBlack];
}
if (this._mockConsole) return this._mockedConsole!(level, formattedMessage);
func(colour.join('')+'%s'+ConsoleColours.Reset, formattedMessage);
func(colour.join("") + "%s" + ConsoleColours.Reset, formattedMessage);
}

public async reportStat(
Expand Down
4 changes: 2 additions & 2 deletions nodejs/src/plugins/service-default2/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { testClient } from "../service-default1/plugin";
export class Service extends ServicesBase {
public override initAfterPlugins: string[] = ["service-default1"];
private testClient: testClient;
constructor(pluginName: string, cwd: string, log: IPluginLogger) {
super(pluginName, cwd, log);
constructor(pluginName: string, cwd: string, pluginCwd: string, log: IPluginLogger) {
super(pluginName, cwd, pluginCwd, log);
this.testClient = new testClient(this);
}
public override async init() {
Expand Down
55 changes: 33 additions & 22 deletions nodejs/src/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { Readable } from "stream";
import { DefaultBase } from "../interfaces/base";
import { RegisteredPlugin, ServicesClient } from "./serviceClient";
import { ErrorMessages } from "../interfaces/static";
import {
DynamicallyReferencedMethodType,
} from "@bettercorp/tools/lib/Interfaces";
import { DynamicallyReferencedMethodType } from "@bettercorp/tools/lib/Interfaces";
import {
DynamicallyReferencedMethodOnIEvents,
DynamicallyReferencedMethodEmitIEvents,
DynamicallyReferencedMethodEmitEARIEvents,
} from "../interfaces/events";
import {
ServiceCallable, ServiceEvents, ServiceReturnableEvents,
ServiceCallable,
ServiceEvents,
ServiceReturnableEvents,
} from "./base";

export class ServicesBase<
Expand All @@ -27,7 +27,7 @@ export class ServicesBase<
pluginConfigType extends IPluginConfig = any
>
extends DefaultBase<pluginConfigType>
implements
implements
IService<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents>
{
public readonly initBeforePlugins?: Array<string>;
Expand All @@ -38,32 +38,43 @@ export class ServicesBase<
async run(): Promise<void> {}

public registerPluginClient<
pluginClientOnEvents ,
pluginClientEmitEvents ,
pluginClientOnReturnableEvents ,
pluginClientEmitReturnableEvents ,
pluginCallableMethods,
pluginClientConfigType extends IPluginConfig
>(
pluginName: string
): Promise<RegisteredPlugin<
pluginClientOnEvents,
pluginClientEmitEvents,
pluginClientOnReturnableEvents,
pluginClientEmitReturnableEvents,
pluginCallableMethods,
pluginClientConfigType
>> {
pluginClientConfigType extends IPluginConfig
>(
pluginName: string
): Promise<
RegisteredPlugin<
pluginClientOnEvents,
pluginClientEmitEvents,
pluginClientOnReturnableEvents,
pluginClientEmitReturnableEvents,
pluginCallableMethods,
pluginClientConfigType
>
> {
throw ErrorMessages.BSBNotInit;
}

protected _clients: Array<ServicesClient> = [];
constructor(pluginName: string, cwd: string, log: IPluginLogger) {
super(pluginName, cwd, log);
if (Tools.isNullOrUndefined(this.initBeforePlugins)) this.initBeforePlugins = [];
if (Tools.isNullOrUndefined(this.initAfterPlugins)) this.initAfterPlugins = [];
if (Tools.isNullOrUndefined(this.runBeforePlugins)) this.runBeforePlugins = [];
if (Tools.isNullOrUndefined(this.runAfterPlugins)) this.runAfterPlugins = [];
constructor(
pluginName: string,
cwd: string,
pluginCwd: string,
log: IPluginLogger
) {
super(pluginName, cwd, pluginCwd, log);
if (Tools.isNullOrUndefined(this.initBeforePlugins))
this.initBeforePlugins = [];
if (Tools.isNullOrUndefined(this.initAfterPlugins))
this.initAfterPlugins = [];
if (Tools.isNullOrUndefined(this.runBeforePlugins))
this.runBeforePlugins = [];
if (Tools.isNullOrUndefined(this.runAfterPlugins))
this.runAfterPlugins = [];
}
receiveStream(
listener: (error: Error | null, stream: Readable) => Promise<void>,
Expand Down
2 changes: 2 additions & 0 deletions nodejs/src/serviceBase/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class SBBase {
events: IServiceEvents<any, any, any, any>,
config: ConfigBase<PluginConfigType>,
cwd: string,
pluginCwd: string,
generateEventsForService: (
pluginName: string,
mappedPluginName: string
Expand All @@ -84,6 +85,7 @@ export class SBBase {
let tPlugin = new RegisteredPlugin<any, any, any, any, any, any>(
pluginName,
cwd,
pluginCwd,
generateLoggerForPlugin(pluginName + "-client")
);
SBBase.setupServicePluginSpecific(
Expand Down
1 change: 1 addition & 0 deletions nodejs/src/serviceBase/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class SBConfig {
appConfig = new (importedPlugin.Config as unknown as typeof ConfigBase)(
this.configPlugin.plugin.name,
this.cwd,
this.configPlugin.plugin.pluginDir,
logger,
this.configPlugin.deploymentProfile
);
Expand Down
1 change: 1 addition & 0 deletions nodejs/src/serviceBase/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class SBEvents {
new (importedPlugin.Events as unknown as typeof EventsBase)(
plugin.name,
cwd,
plugin.pluginDir,
pluginLog
);
await this.log.debug(`Create events plugin: {name}`, {
Expand Down
4 changes: 3 additions & 1 deletion nodejs/src/serviceBase/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class SBLogger {
runningLive: boolean,
CORE_PLUGIN_NAME: string
) {
this._logger = new DefaultLogger("default-logger", "./", undefined!);
this._logger = new DefaultLogger("default-logger", "./", "./", undefined!);
SBBase.setupPlugin(
appId,
runningDebug,
Expand All @@ -28,6 +28,7 @@ export class SBLogger {
this._loggerEvents = new DefaultEvents(
"default-logger-events",
"./",
"./",
this.generateNullLoggerForPlugin()
);
this.log = this.generateLoggerForPlugin(`${CORE_PLUGIN_NAME}-logger`);
Expand Down Expand Up @@ -125,6 +126,7 @@ export class SBLogger {
new (importedPlugin.Logger as unknown as typeof LoggerBase)(
plugin.name,
cwd,
plugin.pluginDir,
this.generateLoggerForPlugin(plugin.mappedName)
);
await this.log.debug(`Create logging plugin: {name}`, {
Expand Down
7 changes: 5 additions & 2 deletions nodejs/src/serviceBase/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export class SBPlugins {
coreLogger: IPluginLogger,
path: string,
version: string,
libOnly = false
libOnly = false,
pluginDir: string
): Promise<Array<IReadyPlugin>> {
const arrOfPlugins: Array<IReadyPlugin> = [];

Expand Down Expand Up @@ -88,6 +89,7 @@ export class SBPlugins {
version: version,
pluginFile,
installerFile: pluginInstallerFile,
pluginDir: pluginDir
});
}

Expand Down Expand Up @@ -141,7 +143,8 @@ export class SBPlugins {
coreLogger,
innerPluginLibPlugin,
packageVersion,
libOnly
libOnly,
path
);
}

Expand Down
2 changes: 2 additions & 0 deletions nodejs/src/serviceBase/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class SBServices {
new (importedPlugin.Service as unknown as typeof ServicesBase)(
plugin.name,
cwd,
plugin.pluginDir,
generateLoggerForPlugin(plugin.mappedName)
);
await this.log.debug(`Create service plugin: {name}`, {
Expand All @@ -77,6 +78,7 @@ export class SBServices {
await generateEventsForService(plugin.name, plugin.mappedName),
appConfig,
cwd,
plugin.pluginDir,
generateEventsForService,
generateLoggerForPlugin,
this.log,
Expand Down
6 changes: 3 additions & 3 deletions nodejs/src/tests/plugins/events/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ const getPluginConfig = async () => {

describe("plugins/events-default", () => {
emit(async () => {
const refP = new events("test-plugin", process.cwd(), fakeLogger);
const refP = new events("test-plugin", process.cwd(), process.cwd(), fakeLogger);
(refP as any).getPluginConfig = getPluginConfig;
if (refP.init !== undefined) await refP.init();
return refP;
}, 10);
emitAndReturn(async () => {
const refP = new events("test-plugin", process.cwd(), fakeLogger);
const refP = new events("test-plugin", process.cwd(), process.cwd(), fakeLogger);
(refP as any).getPluginConfig = getPluginConfig;
if (refP.init !== undefined) await refP.init();
return refP;
}, 10);
emitStreamAndReceiveStream(async () => {
const refP = new events("test-plugin", process.cwd(), fakeLogger);
const refP = new events("test-plugin", process.cwd(), process.cwd(), fakeLogger);
(refP as any).getPluginConfig = getPluginConfig;
if (refP.init !== undefined) await refP.init();
//refP.eas.staticCommsTimeout = 25;
Expand Down
Loading

0 comments on commit 7947ddb

Please sign in to comment.