Files
Mykola Mokhnach 62db5f0b20 refactor(base-plugin)!: remove deprecated legacy logger and default export (#22718)
BREAKING CHANGE: Plugin#logger has been removed; use Plugin#log instead.
BREAKING CHANGE: @appium/base-plugin no longer exports BasePlugin as the default export; use the named BasePlugin export.
2026-09-04 21:55:32 +02:00

62 lines
2.1 KiB
TypeScript

import {ExtensionCore, generateDriverLogPrefix, validateExecuteMethodParams} from '@appium/base-driver';
import {node} from '@appium/support';
import type {
Constraints,
Driver,
ExecuteMethodMap,
ExternalDriver,
MethodMap,
NextPluginCallback,
Plugin,
PluginCommand,
StringRecord,
} from '@appium/types';
type BasePluginMapType = Plugin & Record<string, PluginCommand>;
/**
* Base plugin class for Appium plugins.
* Subclasses should use type `import('@appium/types').MethodMap<SubclassName>` for
* `newMethodMap` and `ExecuteMethodMap<SubclassName>` for `executeMethodMap`.
*/
export class BasePlugin extends ExtensionCore implements Plugin {
static newMethodMap: MethodMap<BasePluginMapType> = {};
static executeMethodMap: ExecuteMethodMap<BasePluginMapType> = {};
readonly name: string;
readonly cliArgs: Readonly<Record<string, unknown>>;
constructor(name: string, cliArgs: Record<string, unknown> = {}, driverId: string | null = null) {
super();
if (driverId) {
this.updateLogPrefix(`${generateDriverLogPrefix(this)} <${driverId}>`);
}
this.name = name;
this.cliArgs = node.deepFreeze({...cliArgs});
}
/**
* A convenience method for plugins that implement their own `executeMethodMap`.
* Pass through to the driver's execute method if the plugin does not handle the script.
*/
async executeMethod<C extends Constraints>(
next: NextPluginCallback,
driver: Driver<C>,
script: string,
protoArgs: readonly [StringRecord<unknown>] | readonly unknown[],
): Promise<unknown> {
const PluginClass = this.constructor as typeof BasePlugin;
const commandMetadata = {...PluginClass.executeMethodMap?.[script]};
if (!commandMetadata.command || !(commandMetadata.command in this)) {
this.log.info(`Plugin did not know how to handle method '${script}'. Passing control to next`);
return await next();
}
const command = this[commandMetadata.command as keyof this] as PluginCommand<ExternalDriver<C>>;
const args = validateExecuteMethodParams(protoArgs as unknown[], commandMetadata.params);
return await command.call(this, next, driver, ...args);
}
}