JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q65090F
  • License Apache-2.0

Create a CLI with a single function

Package Exports

  • stdrun

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (stdrun) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

stdrun

Create a CLI with a single function

Usage

We can define a basic command line program with the following code:

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

function main (opts = {}, ...args) {
  return lines`
    Options: ${JSON.stringify(opts)}
    Arguments: ${args.join(', ')}
  `
}

run(main)

Which you can then run like so:

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

Streaming output

It is also possible to gradually stream your output to stdout. For this we can use a generator function.

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

function* main () {
  yield line('first')
  yield line('second')
  yield line('third')
}

run(main)

Asynchronous operations

Your main function can also be asynchronous (or return a promise). The terminal will stay blocked until the operation is completed.

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

async function main () {
  var content = await fs.readFile('./README.md')
  return content.toUpperCase()
}

run(main)

Node streams

Because stdrun supports both streaming and asynchronous output, you can send any Node stream directly to your terminal.

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

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

run(main)

License

Apache-2.0