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

to-clf

v1.0.0-alpha.6

Published

Automatically run functions as CLI programs.

Downloads

21

Readme

toCLF ("to command line function")

Command Line Functions are functions that can be called directly from the command line.

The easiest way to use to-clf is to just call the clf command line utility by passing it an existing JavaScript file and it will run whichever function this file exports as a CLI app, automatically analyzing its parameters and turning them into command line parameters you can pass in.

For example, say you've already written a function called deploy in deploy.js:

module.exports = async function deploy
({
    dryRun = false /* Just print what this will do */,
    regions = [] /* Which regions to deploy to */,
},  environment /* qa, staging, or production */ )
{
    console.log(`Deploying to ${environment} to ${regions.join(", ")}!`);
}

You can just call it with clf like so:

$ clf deploy.js qa --region aws-west-2
Deploying to qa to aws-west-2!

Not only that, the comments will be appropriately interpreted for the --help command:

$ clf deploy.js --help
Usage: deploy [options] <environment>

Arguments:
  environment               qa, staging, or production

Options:
  -d, --dry-run             Just print what this will do
  -r, --region <region...>  Which regions to deploy to
  -h, --help                display help for command

Notice that clf automatically inferred the types of the command line options from the actual function definition. It knows dryRun is a boolean from the default parameter, and turned regions function option into the singular region command line parameter that can be passed in multiple times, since it noticed it was an array.

If you are writing a new function from scratch, you can choose to manually wrap it in a call to toCLF yourself, so that it can always be used both as a function in a larger program, or called directly as a CLI app just using node:

const toCLF = require("to-clf");

module.exports = toCLF(async function deploy
({
    dryRun = false /* Just print what this will do. */,
    regions = [] /* Which regions to deploy to. */,
},  environment /* qa, staging, or production */ )
{
    console.log(`Deploying to ${environment} to ${regions.join(", ")}!`);
});

This will work identically to our first function, except you can now call it directly using node instead of clf, as if you had wanted it to be a CLI app from the beginning, and again still without sacrificing its usage as an importable normal function in the rest of your program:

$ node deploy.js qa --region aws-west-2

This actually makes to-clf one of the easiest ways to write CLI apps in node!

How Does this Work?

to-clf analyzes the AST of the function in question to build a CLI options and arguments parser. Here are the details:

const toCLF = require("to-clf");

// toCLF will automatically interpret the destructured arguments of your
// function as CLI options.
module.exports = toCLF(function deploy
({
    // toCLF will automatically detect the CLI option type based on the default
    // parameter. For example, it knows the following option is a boolean:
    dryRun = false /* This comment automatically becomes the help description. */,

    // toCLF turns arrays into options you can pass multiple times
    regions = [] /* Regions to deploy to. */,

    // Unnamed arguments are also supported. If you leave out a default parameter,
    // the option or argument becomes required.
},  environment /* qa, staging, or production */)
{
    console.log(`Deploying to ${environment} to ${regions.join(", ")}!`);
});

Installing to-clf

to-clf is available on npm.

To use the clf command line utility, install it globally:

$ npm install to-clf --location=global

To use the toCLF function, install it locally:

$ npm install to-clf