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

@baethon/udba-cli

v1.0.3

Published

Minimalistic CLI app framework

Downloads

6

Readme

Build Status JavaScript Style Guide

@baethon/udba-cli

Minimalistic CLI interface built on top of yargs. It allows building CLI applications inside an existing app.

Command example

Installation

First, install the package:

npm i @baethon/udba-cli

Then create your CLI binary (./cli; ideally, in the root folder):

#!/usr/bin/env node

const { Runtime } = require('@baethon/udba-cli')

const app = new Runtime()

app.load(`${__dirname}/commands/**/*.js`)  
    .then(() => app.run(process.argv.slice(2)))  
    .then(() => app.exit())  
    .catch((error) => {    
        console.warn(error.message)    
        app.exit(1)  
    })

Add executable permissions:

chmod +x ./cli

Creating the first command

Create (commands/hello-world.command.js):

const { Command } = require('@baethon/udba-cli')

class HelloWorld extends Command {    
    static get signature () {
        return 'hello {--who=World}'
    }

    static get description () {
        return 'Say hello to someone'
    }

    handle ({ who }) {
        console.log(`Hello ${who}!`)
    }
}

Execute it:

./cli hello --who=Jon

Signatures

The command declarations are very different as in yargs. The core of the command's definition is signature. The package parses it and creates a valid yargs command.

The signature syntax is based on the syntax of Laravel Artisan commands. All defined parameters can be: string, array (of string), or a boolean.

Positional parameters

  • required parameter: {paramName}

  • optional parameter: {paramName?}

  • optional parameter with default value: {paramName="Some value"}

  • array parameter: {paramName*}

Flags

All flags are optional.

  • boolean parameter (default value: false): {--paramName}

  • string parameter (default value: '' [empty string]): {--paramName=}

  • string parameter (default value: 'Some value'): {--paramName="Some value"}

  • array parameter (default value: []): {--paramName=*}

  • flag alias: {--p|paramName}

Loading commands

You can add a single command using the add() method.

const HelloWorld = require('./commands/hello-world.command.js')

app.add(HelloWorld)

You can also load all commands using glob patterns.

app.load([`${__dirname}/commands/*.js`, `other/path/*.js`])

The load() method returns a Promise. Make sure to wait until it resolves. load() is compatible with globby API.

The Runtime accepts only classes that extend the Command class.

Command handler

The handle() method is responsible for executing the command. As a first argument, it receives the parameters parsed by yargs (note, that there will be more values then defined in the signature). handle() can return a Promise. If this happens, the runtime waits for its fulfillment.

Calling commands programmatically

It's possible to run selected commands from your application. This requires having an additional step (in the application) which sets up the runtime:

const { Runtime } = require('@baethon/udba-cli')

const app = new Runtime()
app.load(`${__dirname}/commands/**/*.js`)

module.exports = app

Calling the command:

const cliApp = require('./cli-runtime.js')

cliApp.call('hello --who=Jon')

Please mind that the runtime setup example is simplified. The app should wait for the load() method to finish.

Acknowledgements

Testing

npm test