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

stdrun

v6.0.0

Published

Create a CLI with a single function

Downloads

27

Readme

stdrun

Create a CLI with a single function

Usage

A basic program using stdrun looks like this:

// example.js
var { run, text } = require('stdrun')

function main (...args) {
  return text`
    Arguments: ${args.join(', ')}
  `
}

run(main)

Which you can then run in your terminal:

$ node example.js --yes some stuff --target="./path/to/somewhere"
Arguments: --yes, some, stuff, --target=./path/to/somewhere

Streams

You can do more than just text though. If your function returns a node stream, its output is gradually rendered to the terminal.

var fs = require('fs')
var run = require('stdrun')

function main (opts, file) {
  return fs.createReadStream(file)
}

run(main)

stdin

You can also read the stdin stream. The cleanest way to do so is using asynchronous iteration.

var run = require('stdrun')

async function * main () {
  for await (var chunk of process.stdin) {
    yield chunk.toString().toUpperCase()
  }
}

run(main)

Errors

Commands can output two kinds of errors. Critical errors that terminate the program should use throw. Other non-critical errors are yielded as normal values from an iterator. Both types of errors are sent to stderr.

var { run, text } = require('stdrun')

function * main (mode) {
  yield text('Some stuff.')

  if (mode === 'panic') {
    throw new Error('Panic!')
  }
  yield new Error('Something went wrong.')
  yield text('Some more stuff.')
}

run(main)

Subcommands

Subcommands are supported too:

// commands.js
var { main, sub, text } = require('stdrun')

main(() => text`everything else`)
sub('nested', () => text`ping`)
sub('nested', 'deeper', () => text`pong`)
sub('hello', () => text`world`)

Which you can execute like so:

$ node commands.js hello
world
$ node commands.js nested
ping
$ node commands.js nested deeper
pong
$ node commands.js
everything else

License

Apache-2.0