npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@triviality/commander

v1.1.6

Published

Commander as a Triviality Module

Downloads

5

Readme

Table of Contents

Installation

To install the stable version:

yarn add @triviality/commander

This assumes you are using yarn as your package manager.

or

npm install @triviality/commander

Triviality commander

Add integration for Commander in Triviality.

  • Add option to split commands configuration over multiple Modules into multiple configuration services.
  • Exposes Module for automatic version based on your package.json
  • StartCommanderService service that response when no valid command is given.

Example

Example commander configuration:

import { Command } from 'commander';
import { CommanderConfigurationInterface } from '@triviality/commander';

export class CommanderHelloConfiguration implements CommanderConfigurationInterface {

  public async configure(program: Command) {
    program
      .command('hello <name>')
      .description('Say hello to someone')
      .option('-s, --shout', 'shout the hello message')
      .action((name, cmd: { shout: boolean }) => {
        const message = `hello ${name}`;
        console.log(cmd.shout ? message.toUpperCase() : message);
      });
  }
}

Module with configuration added to the configuration registry:

import { Module } from '@triviality/core';
import { CommanderConfigurationInterface } from '@triviality/commander';
import { CommanderHelloConfiguration } from './CommanderHelloConfiguration';
import { CommanderByeConfiguration } from './CommanderByeConfiguration';

export class CommanderExampleModule implements Module {
  public registries() {
    return {
      commanderConfigurations: (): CommanderConfigurationInterface[] => {
        return [
          this.commanderExampleConfiguration(),
          this.commanderByeExample(),
        ];
      },
    };
  }

  public commanderExampleConfiguration() {
    return new CommanderHelloConfiguration();
  }

  public commanderByeExample() {
    return new CommanderByeConfiguration();
  }
}

Add the module to the ContainerFactory

import { ContainerFactory } from '@triviality/core';
import { CommanderFeature } from '@triviality/commander';
import { CommanderPackageVersionFeature } from '@triviality/commander';
import { CommanderExampleModule } from './CommanderExampleModule';

ContainerFactory
  .create()
  .add(CommanderFeature)
  .add(CommanderPackageVersionFeature)
  .add(CommanderExampleModule)
  .build()
  .then((container) => {
    container
      .startCommanderService()
      .start();
  });

if we run the file, we can call the actual commands.

./node_modules/.bin/ts-node example/bootstrap.ts hello world
hello world
./node_modules/.bin/ts-node example/bootstrap.ts bye world
bye world
./node_modules/.bin/ts-node example/bootstrap.ts hello world --shout
HELLO WORLD

Version

CommanderPackageVersionModule exposes automatic version to commander based on your package.json

./node_modules/.bin/ts-node example/bootstrap.ts --version
0.4.0

Usage

To create a terminal application you can best use ts-node. Call your typescript service container from there.

#!/usr/bin/env node

require("ts-node/register");
require("./example/bootstrap");

Don't forget to give it executable permissions (chmod +x ./cli.js).

Now you can directly run your commands:

######cli "cli.js"(hello world -s) ######cli "cli.js"(bye world)

Thanks

Special thanks to:

  • Eric Pinxteren

Reads

commander

triviality