owservable actions
This project is maintained by owservable
A TypeScript library implementing the Action Pattern for creating reusable, executable units of work that can be run as commands, cronjobs, watchers, workers, or controllers.
npm install @owservable/actions
or
yarn add @owservable/actions
or
pnpm add @owservable/actions
import { Action, ActionInterface } from '@owservable/actions';
class MyAction extends Action implements ActionInterface {
protected _description = 'My custom action';
async handle(...args: any[]): Promise<any> {
// Your business logic here
console.log('Action executed with args:', args);
return { success: true };
}
description(): string {
return this._description;
}
}
import { Action, ActionAsCommandInterface } from '@owservable/actions';
class MyCommandAction extends Action implements ActionAsCommandInterface {
protected _signature = 'my-command {--option=default}';
protected _description = 'My command action';
async handle(...args: any[]): Promise<any> {
// Your business logic here
return { success: true };
}
async asCommand(options: any): Promise<void> {
console.log('Command executed with options:', options);
await this.handle(options);
}
signature(): string {
return this._signature;
}
description(): string {
return this._description;
}
}
import { runActionAsCommand } from '@owservable/actions';
const action = new MyCommandAction();
await runActionAsCommand(action);
import { Action, ActionAsCronjobInterface } from '@owservable/actions';
class MyCronjobAction extends Action implements ActionAsCronjobInterface {
protected _schedule = '0 0 * * *'; // Daily at midnight
protected _description = 'Daily cleanup job';
async handle(...args: any[]): Promise<any> {
// Your cronjob logic here
return { success: true };
}
async asCronjob(): Promise<void> {
console.log('Cronjob executed');
await this.handle();
}
schedule(): string {
return this._schedule;
}
description(): string {
return this._description;
}
}
import { Action, ActionAsWatcherInterface } from '@owservable/actions';
class MyWatcherAction extends Action implements ActionAsWatcherInterface {
protected _description = 'File watcher action';
async handle(...args: any[]): Promise<any> {
// Your watcher logic here
return { success: true };
}
async asWatcher(): Promise<void> {
console.log('Watcher executed');
await this.handle();
}
description(): string {
return this._description;
}
}
Action
Abstract base class providing common functionality for all actions.
Properties:
_signature
: Command signature for CLI actions_description
: Human-readable description_schedule
: Cron schedule for cronjob actionsMethods:
signature()
: Returns the command signaturedescription()
: Returns the action descriptionschedule()
: Returns the cron scheduleActionInterface
Base interface that all actions must implement.
Methods:
description(): string
- Returns action descriptionhandle(...args: any[]): Promise<any>
- Main action logicActionAsCommandInterface
Interface for actions that can be run as CLI commands.
Extends: ActionInterface
Methods:
signature(): string
- Returns command signatureasCommand(options: any): Promise<void>
- Execute as commandActionAsCronjobInterface
Interface for actions that can be run as scheduled cronjobs.
Extends: ActionInterface
Methods:
asCronjob(): Promise<void>
- Execute as cronjobActionAsWatcherInterface
Interface for actions that can be run as file/directory watchers.
Extends: ActionInterface
Methods:
asWatcher(): Promise<void>
- Execute as watcherActionAsWorkerInterface
Interface for actions that can be run as background workers.
Extends: ActionInterface
Methods:
asWorker(): Promise<void>
- Execute as workerActionAsControllerInterface
Interface for actions that can be run as HTTP controllers.
Extends: ActionInterface
Methods:
asController(): Promise<void>
- Execute as controllerrunActionAsCommand(action: ActionAsCommandInterface): Promise<void>
Executes an action as a CLI command with option parsing.
findCommandAction(actionsFolder: string, actionName: string): ActionAsCommandInterface
Finds and loads a command action from a folder.
getOptionAndDefaultValue(config: string): {option: string, defaultValue: any}
Parses command option configuration strings.
npm test
Licensed under The Unlicense.
Contributions are welcome! Please feel free to submit a Pull Request.