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

fallback-cli

v2.0.2

Published

Default to the locally installed version of your CLI, use the global one if not found.

Downloads

2,223

Readme

fallback-cli Build Status

Default to the locally installed version of your CLI, use the global one if not found.

This allows users to install your tool globally, but revert to whatever version is installed locally in node_modules. npm already handles this when running scripts defined in package.json, but this works even when invoking your CLI command directly from the console.

Install

$ npm install --save fallback-cli

Usage

The following assumes you have a package named my-module, and the CLI script is bin/cli.js.

First, create cli-shim.js, and place it in the same directory as your current CLI script.

bin/cli-shim.js:

#!/usr/bin/env node
require('fallback-cli')('my-module/bin/cli.js');

Next update your package.json to point to cli-shim.js.

{
  "name": "my-module",
  "bin": "bin/cli-shim.js"
}

That is it!

Your globally installed CLI will use the local version from node_modules if it exists. In most cases this will even be compatible with old versions of your CLI before you introduced fallback-cli.

Note: cli.js and cli-shim.js are arbitrary file names, use whatever you want.

API

fallbackCli(path, [relativePath], [customRunner])

Only path is required.

  • path: must be a string describing your packages name, and the path to your CLI within the package (i.e. my-module/bin/my-cli.js).

  • relativePath: It is highly recommended that your "shim" to be installed in the same directory as the actual CLI. If that is not possible, you may use relativePath to describe where it is relative to the shim.

    fallbackCli('my-module/bin/the-cli.js', '../bin/the-cli.js');

If specified, relativePath must point to the same file as path.

  • customRunner: A callback that that will be invoked with a single runnerOptions object. It should launch your CLI implementation.

The default runner is simple:

function defaultRunner(runnerOptions) {
  require(runnerOptions.cli);
}

runnerOptions

If you specify a customRunner function, it is passed an object with these properties:

  • localCli: The absolute path of the CLI script in the locally installed module. It will be null if there is no local install.

  • globalCli: The absolute path of the CLI script in the globally installed module. It will be null if the locally installed script was invoked directly (i.e. via npm run <script>, or ./node_modules/.bin/my-cli).

  • localPkg: The absolute path of package.json in the local module. Useful for determining the version of the local install. It will be null if there is no local install, or in the very unlikely event package.json could not be found in the locally installed module.

  • globalPkg: The absolute path of package.json in the global module. Useful for determining the version of the global install. It will be null if the locally installed script was invoked directly, or if package.json could not be found in the globally installed module.

It is possible to confuse the algorithm that finds the global package.json if you do not put the shim in the same directory as the CLI script and use the relativePath argument. For this reason, it is highly recommended you put them both in the same directory and avoid using relativePath.

  • cli: The same as localCli if found, otherwise falls back to be the same as globalCli. Convenience property for implementing a graceful fallback.

  • pkg: The same as localPkg if found, otherwise falls back to be the same as globalPkg.

  • location: Either "local" or "global" depending on which is found (local takes precedence).

Note: All properties are of type string. All properties except location can also be null.

Alternate API

fallbackCli(options)

As an alternative to individual arguments, you can provide a single options argument.

options.path

required

Type: string

Same as path described in fallbackCli(path) above.

options.relative

optional

Type: string

Same as relativePath described in fallbackCli(path, relativePath) above.

options.run

optional

Type: callback(runnerOptions)

Same as customRunner described in fallbackCli(path, customRunner) above.

License

MIT © James Talmage