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

clidec

v0.0.2-alpha3

Published

cli apps with decorators

Downloads

2

Readme

clidec

this software is very much a WIP.

What?

A tiny framework for building CLI tools/application using decorators and TypeScript.

Built on command-line-args and command-line-usage.

Inspired by NestJS and Angular.

Why?

  • You like writing CLI tools, but you hate the tedious overhead of having to set up sub-commands & options/flags, plus supporting decent help menus.
  • You think CLI tools like Helm and git are pretty good examples of how CLI tools should work.
  • Meta-programming is cool in 2019
  • If you call yourself a JS dev and you're not writng frameworks what are you doing with your life?

Ok, show me?

Given the following file test.ts:

import { bootstrap, Command, Help, Opt } from "clidec";

@Help([
  {
    header: "Test - test command",
    content:
      "This command shows how clidec works, and how it can be used to bootstrap cli app development really quickly & with minimal boilerplate." +
      "FYI, this help block uses {green.bold chalk} template syntax."
  },
  {
    header: "Examples",
    content: ["$ test foo [file]", "$ test bar"]
  }
])
class Test {
  @Command({
    name: "foo",
    alias: "f",
    summary: "this foo command does foo stuff",
    help: [
      {
        header: "foo command",
        content:
          "This command uses a bunch of different options, including required options."
      },
      {
        header: "examples",
        content: ["$ cmd foo <file>", "$ cmd foo -r <file>"]
      }
    ]
  })
  public foo(
    @Opt({
      name: "file",
      alias: "f",
      defaultOption: true,
      required: true,
      typeLabel: "file",
      description: "file to read"
    })
    file?: string,
    @Opt({
      name: "raw",
      alias: "r",
      type: Boolean,
      description: "use raw input, for example"
    })
    raw?: boolean
  ): void {
    // do some command stuff
  }

  @Command({
    name: "bar",
    alias: "b",
    summary: "the bar command does bar things",
    help: [
      {
        header: "bar command",
        content:
          "This command has an option that can be used multiple times. The value that's passed to the function will be a string array."
      },
      {
        header: "examples:",
        content: ["$ test bar -m first -m second"]
      }
    ]
  })
  public bar(
    @Opt({
      multiple: true,
      name: "multiple",
      alias: "m",
      description: "argument that can be used multiple times",
      typeLabel: "string"
    })
    multiple: string[]
  ) {
    // do some stuff here
  }
}

// actually run our commands!
bootstrap(new Test());

Example output from running $ ts-node test.ts / $ ts-node test.ts help / $ ts-node test.ts --help:

Test - test command

  This command shows how clidec works, and how it can be used to bootstrap cli
  app development really quickly & with minimal boilerplate.FYI, this help
  block uses chalk template syntax.

Examples

  $ test foo [file]
  $ test bar

Commands:

  foo   f   this foo command does foo stuff
  bar   b   the bar command does bar things

Example output from: $ ts-node foo.ts help foo:

foo command

  This command uses a bunch of different options, including required options.

examples

  $ cmd foo <file>
  $ cmd foo -r <file>

Options

  -r, --raw         use raw input, for example
  -f, --file file   file to read

Example output from: $ ts-node foo.ts help bar:

bar command

  This command has an option that can be used multiple times. The value that's
  passed to the function will be a string array.

examples

  $ test bar -m first -m second

Options

  -m, --multiple string   argument that can be used multiple times

Still here?

Here is all you need to know to use this thing.

@Command(object) (methods)

Make this function a command.

| property | type | description | | ----------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- | | name | string (required) | the name of your (sub) command. | | alias | string | alias for this command. | | description | string | used to describe this sub command in the help menu. | | help | HelpSection | used when your cmd is run with $ cmd help <subcommand-name> |

@Opt(object) (parameters)

| property | type | description | | ----------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | | name | string (required) | the name of your param. Should probably match the parameters name | | alias | string | alias for this option. | | required | boolean | whether or not this option is required. If it is required and not provided when the command is executed, the program will print a warning and exit. | | description | string | used to describe this param in the help menu. |

Also includes all of the properties specified by command-line-args OptionDefinition and those specified by command-line-usage OptionDefinition.

@Help(HelpArgs) (classes)

Create a help menu using command-line-usage which will show up when the user runs $ help or $ --help.

bootstrap(object)

Once you've created your class and decorated it with the above ☝️ decorators, you will probably want to run it. Luckily, this is pretty easy. Just import the bootstrap function from clidec and pass an instance of your class to it.

Important

Remember you NEED TO add this the compilerOptions section of your tsconfig.json:

    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,

Future:

  • Add some tests. This is a rush job.
  • Add some kind of 'devMode' option for the bootstrap function which runs a bunch of sanity checks to make sure all the decorators are set up right.
  • Add a 'middleware' system so you could handle different output types.
  • Add a nice error handler. Maybe support for async functions too.