Skip to content

opencommand/corm-node

Repository files navigation

Corm - TypeScript Command Pipeline Library

A fluent API for building command pipelines in TypeScript, inspired by the Go version of Corm.

Features

  • Fluent API: Chain commands with intuitive method calls
  • Multiple Operators: Support for pipe (|), and (&), and-and (&&), or-or (||), and semicolon (;)
  • Type Safety: Full TypeScript support with type checking
  • Extensible: Easy to create custom command implementations
  • Async/Await: Modern Promise-based execution

Installation

npm install corm

Quick Start

import { Pipeline, pipe, cmd, go, sudo } from 'corm';

// Builder pattern
const pipeline1 = Pipeline.new()
  .add(cmd('echo', 'hello'))
  .pipe(cmd('grep', 'h'))
  .andAnd(cmd('echo', 'success'));

console.log(pipeline1.toString());
// Output: echo hello | grep h && echo success

// Functional pattern
const pipeline2 = pipe(
  cmd('echo', 'hello'),
  cmd('grep', 'h'),
  cmd('wc', '-l')
);

console.log(pipeline2.toString());
// Output: echo hello | grep h | wc -l

API Reference

Pipeline Class

The main class for building command pipelines.

Methods

  • add(cmd: Command): Add a command to the pipeline
  • pipe(cmd: Command): Pipe output to the next command (|)
  • and(cmd: Command): Run command in background (&)
  • andAnd(cmd: Command): Run command only if previous succeeded (&&)
  • orOr(cmd: Command): Run command only if previous failed (||)
  • semicolon(cmd: Command): Run command sequentially (;)
  • toString(): Get string representation
  • run(): Execute the pipeline
  • output(): Execute and return output

Static Methods

  • Pipeline.new(): Create a new pipeline instance

Functional API

  • pipe(...cmds): Create pipeline with pipe operators
  • and(...cmds): Create pipeline with and operators
  • andAnd(...cmds): Create pipeline with and-and operators
  • orOr(...cmds): Create pipeline with or-or operators
  • semicolon(...cmds): Create pipeline with semicolon operators

Built-in Commands

SimpleCommand

import { cmd } from 'corm';

const command = cmd('echo', 'hello', 'world');
console.log(command.toString()); // echo hello world

Go Commands

import { go } from 'corm';

const testCmd = go()
  .test('./...')
  .coverprofile('coverage.out')
  .covermode('atomic');

console.log(testCmd.toString());
// go test -coverprofile=coverage.out -covermode=atomic ./...

Sudo Command

import { sudo, cmd } from 'corm';

const sudoCmd = sudo(cmd('echo', 'hello'))
  .shell()
  .interactive();

console.log(sudoCmd.toString());
// sudo -i -s echo hello

Examples

Complex Pipeline

import { Pipeline, sudo, go, cmd } from 'corm';

const pipeline = Pipeline.new()
  .add(sudo(go().test('./...').coverprofile('coverage.out')).shell())
  .pipe(cmd('grep', 'PASS'))
  .andAnd(cmd('echo', 'All tests passed'))
  .orOr(cmd('echo', 'Some tests failed'));

console.log(pipeline.toString());
// sudo -s go test -coverprofile=coverage.out ./... | grep PASS && echo All tests passed || echo Some tests failed

Functional Style

import { pipe, andAnd, orOr, cmd } from 'corm';

const pipeline = pipe(
  cmd('npm', 'test'),
  cmd('grep', 'PASS')
).andAnd(
  cmd('echo', 'Tests passed')
).orOr(
  cmd('echo', 'Tests failed')
);

Custom Command Implementation

import { BaseCommand } from 'corm';

class DockerCommand extends BaseCommand {
  private image: string;
  private args: string[] = [];

  constructor(image: string) {
    super();
    this.image = image;
  }

  name(): string {
    return 'docker';
  }

  run(command: string): DockerCommand {
    this.args.push('run', this.image, command);
    return this;
  }

  toString(): string {
    return `docker ${this.args.join(' ')}`;
  }
}

const dockerCmd = new DockerCommand('node:18').run('npm test');
console.log(dockerCmd.toString()); // docker run node:18 npm test

Development

Building

npm run build

Testing

npm test

Development Mode

npm run dev

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published