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 🙏

© 2026 – Pkg Stats / Ryan Hefner

cli-script-utils

v0.7.0

Published

Commonly used utility functions for writing command-line scripts

Readme

cli-script-utils

This repository contains some of the utility functions that I've used for almost any cli script that I've written. The focus has been on simplicity and brevity.

These functions have by no means been completely tested, or are overly-powerful or configurable. However, they have no external and very few interal dependencies so that inlining (copy-pasting) into scripts is easy.

Functions

glob(dir: string, pattern: string): string[]

A simple glob implementation.

compileGlobPattern(pattern: string): RegExp

Compiles a given glob pattern to a RegExp.
WARNING: Not a standards compliant implementation.

compileGlobPredicate(globs: string[]): (x: string) => boolean

Compiles a the given glob patterns to a predicate function. Globs are devided into positive and neative ones (i.e. starting with !). The compiled predicate returns true if its argument matches any of the positive globs and none of the negative ones.

const predicate = compileGlobPredicate([
    '**/*.js',
    '!built/**/*'
]);

predicate('hello.js'); // true
predicate('built/hello.js'); // false

readdirr(path: string): string[]

A recursive synchronous version of readdir. Useful for reading the contents of a directory which may contain other nested directories. Doesn't throw if the specified path doesn't exist or leads to a file.

rmrf(path: string): void

A recursive synchronous version of rm. Useful for deleting directories which may contain other nested directories. Doesn't throw if the specified path doesn't exist.

mkdirr(path: string): void

A recursive synchronous version of mkdir. Useful when creating nested directories.

exists(path: string): boolean

An alias for fs.existsSync.

readFile(path: string): string

Synchronously reads the file at the specified path and returns its content as a UTF-8 encoded string.

writeFile(path: string, content: string): void

Synchronously writes a file at the specified path with the provided content.

unixPath(path: string): string

Given a path that contains forward and/or backward slashes, returns a path with forward slashes only.

fetch(url: string): Promise<string>

Makes an HTTPS GET request and returns the content of the response. Redirects are followed. Currently only GET is supported.

getFlagOption(option: string): boolean

Checks argv and returns whether such an option has been provided.

// argv: [ 'node', 'myscript.js', '--verbose' ]
getFlagOption('--verbose'); // true
getFlagOption('--version'); // false

getValueOption(option: string): string

Retrieves the value provided to option as string.

// argv: [ 'node', 'myscript.js', '--path=built/main.js', '--positioned', 'next-pos' ]
getValueOption('--path'); // 'built/main.js'
getValueOption('--positioned'); // 'next-pos'

match(text: string, rx: RegExp): string[][]

Executes the RegExp rx on text and returns the results of all matches.

trai<T>(f: () => T, def: T): T

An expression varion of try. Executes the provided thunk f and returns its result. In case of an error, returns the default value def.

exit(code: number): never

An alias for process.exit.

fail(msg?: string, marker?: Function): never

An expression version of throw. Throws an Error with the provided message.
NOTE: If a marker function is provided and the host environment allows stack trace manipulation, all frames following and including marker will be omitted.

die(msg: string): never

Akin to fail but calls process.exit(1) instead of throwing an exception. The message is logged on stderr.