WORKING PATTERNS

From first task to full pipeline.

Copy a focused pattern, run it through the API or CLI, then grow it into the automation your project needs.

10patterns
5categories

10 examples shown

Basics

Hello, task runner

Methods become named tasks when their class is registered.

Source ↗
hello.ts
import bz from 'beelzebub';

class HelloTasks extends bz.Tasks {
  hello() {
    this.logger.log('Hello from Beelzebub');
  }
}

bz.add(HelloTasks);
await bz.run('HelloTasks.hello');
Composition

Sequence a release

Keep dependent work ordered and stop when a step fails.

Source ↗
release.ts
class ReleaseTasks extends bz.Tasks {
  release() {
    return this.$sequence(
      '.clean',
      '.compile',
      '.package'
    );
  }
}

bz.add(ReleaseTasks);
await bz.run('ReleaseTasks.release');
Composition

Fan out verification

Run independent checks together and await them as one task.

Source ↗
verify.ts
class VerifyTasks extends bz.Tasks {
  verify() {
    return this.$parallel(
      '.lint',
      '.typecheck',
      '.test'
    );
  }
}

bz.add(VerifyTasks);
await bz.run('VerifyTasks.verify');
Lifecycle

Prepare, trace, clean up

Wrap a run or every task with predictable lifecycle hooks.

Source ↗
hooks.ts
class DatabaseTasks extends bz.Tasks {
  $beforeAll() {
    return openConnection();
  }

  $beforeEach(taskInfo: TaskInfo) {
    this.logger.debug(`starting ${taskInfo.task}`);
  }

  $afterAll() {
    return closeConnection();
  }
}
Typed API

Decorators + task variables

Describe help, defaults, and validated variables beside the task.

Source ↗
deploy.ts
import {
  BzTasks, defaultTask, help, vars
} from 'beelzebub';

class DeployTasks extends BzTasks {
  @defaultTask
  @help('Deploy the application')
  @vars({
    env: { type: 'String', required: true }
  })
  deploy(options: { env: string }) {
    this.logger.info(options.env);
  }
}
Modules

Compose nested task groups

Give a large project clear namespaces without a single giant class.

Source ↗
project.ts
class FrontendTasks extends bz.Tasks {
  build() {
    return bundleFrontend();
  }
}

class ProjectTasks extends bz.Tasks {
  $init() {
    return this.$addSubTasks(FrontendTasks);
  }
}

bz.add(ProjectTasks);
await bz.run('ProjectTasks.FrontendTasks.build');
Signals

Emit domain events

Expose progress to reporters without coupling them to task logic.

Source ↗
events.ts
const app = bz();

app.on(
  'artifact.ready',
  (_taskInfo: TaskInfo, artifact: Artifact) => {
    console.log(artifact.path);
  }
);

class BuildTasks extends app.Tasks {
  async build() {
    const artifact = await compile();
    this.$emit('artifact.ready', artifact);
  }
}
Interfaces

Swap task implementations

Keep a stable contract while projects choose their own implementation.

Source ↗
interface.ts
class CloudDeploy extends bz.Tasks {
  deploy() {
    return uploadToCloud();
  }
}

class DeployTarget extends bz.InterfaceTasks {
  $replaceWith(config: BeelzebubConfig) {
    return config.provider === 'cloud'
      ? CloudDeploy
      : LocalDeploy;
  }
}

bz.add(DeployTarget, { provider: 'cloud' });
Async I/O

Return a Node stream

Promises, generators, and streams all participate in task completion.

Source ↗
streams.ts
import { createReadStream, createWriteStream }
  from 'node:fs';

class AssetTasks extends bz.Tasks {
  copy() {
    return createReadStream('src/logo.svg')
      .pipe(createWriteStream('dist/logo.svg'));
  }
}

bz.add(AssetTasks);
await bz.run('AssetTasks.copy');
CLI

Run the same tasks from bz

Point the CLI at a task file and pass variables after the task name.

Source ↗
terminal
# Run a task
bz --file ./tasks.ts BuildTasks.compile

# Inspect available tasks
bz --file ./tasks.ts --help

# Pass a typed variable
bz --file ./tasks.ts DeployTasks.deploy --env production

Need the full implementation?

The repository includes runnable API and CLI projects for every core pattern.

Open examples on GitHub ↗