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

@carnesen/run-and-exit

v1.0.0

Published

Run a function or async function, console.log the result, and exit

Downloads

1,003

Readme

@carnesen/run-and-exit

Run a function or async function, console.log the result, and process.exit

build status badge npm version badge github stars badge

Install

$ npm install @carnesen/run-and-exit

This package includes runtime JavaScript files (ES2015 + CommonJS) as well as the corresponding TypeScript type declarations.

Usage

Here's a JavaScript example with an async function that fails:

// example.js
const { runAndExit } = require('@carnesen/run-and-exit');
const { readFile } = require('fs');
const { promisify } = require('util');

runAndExit(async () => {
  const fileContents = await promisify(readFile)('/foo/bar/baz', 'utf8');
  return fileContents;
});
$ node example.js
{ Error: ENOENT: no such file or directory, open '/foo/bar/baz'
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/foo/bar/baz' }
$ echo $?
1

Here's a TypeScript example with a synchronous function that succeeds:

// example.ts
import { runAndExit } from '@carnesen/run-and-exit';

const concat = (a: string, b: string) => `${a}-${b}`;
runAndExit(concat, 'foo', 'bar');
$ ts-node example.ts
foo-bar
$ echo $?
0

runAndExit is intelligently typed in the sense that, continuing the previous example, the TypeScript compiler would complain if you tried this:

// NOT OK
runAndExit(concat, 'foo', 3);
// ^^ error TS2345: Argument of type '3' is not assignable to parameter of type 'string'.

This is achieved using "rest parameters with tuple types", new in TypeScript 3.0. If you're using an older version of TypeScript, runAndExit may not work as advertised here.

API

runAndExit(fn, ...args)

Runs the provided function fn with arguments args.

fn

A function. Can return/throw a value synchronously or return a Promise (e.g. an async function). If fn throws or returns a promise that rejects, the exception is console.logged and then process.exit(1) is called. In particular this means that if you don't want a show a stack trace in the terminal, fn should throw a string instead of an Error object. If fn returns a non-Promise value or a Promise that resolves, the value is console.logged and then process.exit(0) is called.

args

Arguments passed to fn. If using TypeScript, args must be assignable to the parameter types of fn just as if you were calling fn(args) directly.

More information

This micro-package has a half dozen unit tests with 100% coverage. If you want to see more examples of how it works, those tests would be a good place to start. If you encounter any bugs or have any questions or feature requests, please don't hesitate to file an issue or submit a pull request on this project's repository on GitHub.

Related

  • @carnesen/cli: A library for building Node.js command-line interfaces

License

MIT © Chris Arnesen