actions

owservable actions

This project is maintained by owservable

owservable

@owservable/actions

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.

๐Ÿš€ Features

๐Ÿ“ฆ Installation

npm install @owservable/actions

or

yarn add @owservable/actions

or

pnpm add @owservable/actions

๐Ÿ”ง Usage

Basic Action Implementation

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;
  }
}

Command Line Action

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;
  }
}

Running Actions as Commands

import { runActionAsCommand } from '@owservable/actions';

const action = new MyCommandAction();
await runActionAsCommand(action);

Cronjob 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;
  }
}

Watcher Action

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;
  }
}

๐Ÿ“š API Documentation

Base Classes

Action

Abstract base class providing common functionality for all actions.

Properties:

Methods:

Interfaces

ActionInterface

Base interface that all actions must implement.

Methods:

ActionAsCommandInterface

Interface for actions that can be run as CLI commands.

Extends: ActionInterface

Methods:

ActionAsCronjobInterface

Interface for actions that can be run as scheduled cronjobs.

Extends: ActionInterface

Methods:

ActionAsWatcherInterface

Interface for actions that can be run as file/directory watchers.

Extends: ActionInterface

Methods:

ActionAsWorkerInterface

Interface for actions that can be run as background workers.

Extends: ActionInterface

Methods:

ActionAsControllerInterface

Interface for actions that can be run as HTTP controllers.

Extends: ActionInterface

Methods:

Utility Functions

runActionAsCommand(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.

๐Ÿ—๏ธ Requirements

๐Ÿงช Testing

npm test

๐Ÿ“– Documentation

๐Ÿ“„ License

Licensed under The Unlicense.

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.