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 🙏

© 2025 – Pkg Stats / Ryan Hefner

command-functions

v0.1.4

Published

Create libraries that function as both CLI and js libraries at the same time

Downloads

2

Readme

Command Functions

Create your own node libraries that work both as node libraries and CLI tools at the same time

How it works

Step 1 - Defining our Functions - Build a Command Map

First we create an object that maps our command names to their functions

const commands = {
    favoriteColor: () => Math.random() < 0.75 ? 'purple': 'green'
}

Step 2 - Calling the Command Functions library

First install command functions as a dependency

npm install --save command-functions

Next use the CommandFunctions class to convert your functions into a library

const CommandFunctions = require('command-functions/CommandFunctions')

const commands = {
    favoriteColor: () => Math.random() < 0.75 ? 'purple': 'green'
}
const app = new CommandFunctions(commands)

// Detects whether being required or being run by the terminal
if (require.main === module) { // It's being run by the terminal
  app.runCLI()
    .then(() => {
      console.log('Finished')
      process.exit(0)
    })
    .catch(error => {
      console.error(error)
      process.exit(1)
    })
} else { // It's a module
  module.exports = app.getExports()
}

Note: How args are parsed

When being run from the command line, Command Functions will parse your arguments internally using Minimist. Minimist will automatically parse all your arguments into their closest JSON approximation. Meaning the argument "age=[1,2,3]" will now make your function receive an options object, whose age property will be an Array containing the numbers 1 through 3. Primary arguments will be passed in before the options object, meaning the input syntax for your function should look something like this

function myFunction(firstPrimaryArg, secondPrimaryArg, options={}) {...}

Providing additional command options

Instead of providing a function as the handler for a given command in your command map, you can wrap your function inside of an object, providing the function as the "handler" property. In this case the rest of the properties are treated as options. Here's an example of what a command using this syntax might look like

const commands = {
    favoriteColor: {
        handler: () => Math.random() < 0.75 ? 'purple': 'green'
    }
}

Command Options

handler

This is required for every command. It's a function that runs the command.

noOptions

Set this to true if you would not like your command to accept any options.

format

A Sandhands format to validate that commands arguments. This can be used to ensure all of your arguments are formatted correctly. Each individual argument can be given it's own format, in which case the command might look like this

{
   handler: someFunction,
   args: {
        name: {
            format: String // Gets Angry if the name argument provided is not a string
        }
   }
}

To ensure you received a valid set of primary arguments, you could provide a format that looks something like this

{
    _: [String],
    secondaryOption: Boolean,
    randomOption: Number
}