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

@radic/console

v1.3.2

Published

Create class based commands with Yargs using decorators

Downloads

78

Readme

Console

A slightly modified yargs clone that allows class based command structure and dependency injection, with the help of some decorators. This is an slightly opinionated way of dealing with bigger cli applications in a structural way. Efforts have been made to make everything fully extendable and replaceable, while keeping it possible to use yargs the same way when need to do so.

Documentation

Check out the documentation website for in depth documentation.

This package also uses @radic/core to provide the service container which provides dependency injection and service providers to allow a specified way of adding, extending and modifying existing extensions to the platform. For anyone that has used Laravel's Application with its ServiceProviders, you'll feel right at home.

Examples

Lets start with the gist of it so you know if this is something you'll want to have or rather pass.

A full working example can be found in this repo. Check out the apps/cli directory that uses pretty much all available options

  • You can also use this library in Javascript if you want. Examples will be in Typescript.
  • This example uses typescript compiled from src to lib directory. So many of the code below will be examples from src

Basic way of starting your cli application

bin/foo

#!/usr/bin/env node
require('../lib').cli(); // lib is the compiled javascript

src/index.ts

export async function cli(){
    /**
     * Boots up the application. This might be done in various ways. Check the `apps/cli/src/index.ts` file for an example
     */
}

A Command

commands/move.ts Lets start with a simple command to show of the basics.

  • The BaseCommand is not required. But it contains goodies we'll later check into
  • The parameters are directly translated from the first parameter @command string that is equal to yargs it's way of command definition.
  • Options are declared with @option decorator.
import {command, option, BaseCommand } from '@radic/console';

@command('move <path> [files...]', 'Move one or multiple files to the given path')
export default class extends BaseCommand {
    @option('o', 'Override exising files') write: boolean;
    @option('l', 'Limit the amount of files to be moved') limit: number=100;

    async handle(path: string, ...files:string[]) {
        for(const index in files){
            if(this.limit && this.limit > index){
                break;
            }
            let file = files[index];
            if(this.write){
                // code
            }
        }
    }
}

Subcommands

commands/config.ts To create a structure with sub-commands.

  • It defaults to defining the directory with sub commands to __dirname + '/${name}_cmds'.
  • This can easily be altered. This example show how
import {group, GroupCommand } from '@radic/console';

@group('config', 'Configuration management', __dirname)
export default class extends GroupCommand {
    builder = cli => {
        cli.commandDir(__dirname+'/config');
    }
}

commands/config_cmds/list.ts

  • Config is an imaginary class that has been bound into the Service Container. (more on that later)
  • Config is using dot notated getters/setters, for the example sake
  • dot-object has a method dot to convert object to dotted-key/value pair. Used here for listing nested configuration structures
import {command, option, BaseCommand } from '@radic/console';
import {inject} from '@radic/core';
import {Config} from '../../'
import {dot} from 'dot-object'

@command('list [path]', 'List all configuration')
export default class extends BaseCommand {
    @inject('config') config:Config

    async handle(path?: string) {
        let config;
        if(path){
            config = this.config.get(path, {});
        } else {
            config = this.config.all();
        }
        Object.entries(dot(config)).forEach(([key,value]) => {
            console.log(`[${key}]: `, value);
        })
    }
}

Further reading and examples are found in the documentation