Default to the locally installed version of your CLI, use the global one if not found.
npm install fallback-cli> 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.
```
$ npm install --save fallback-cli
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:
`js`
#!/usr/bin/env node
require('fallback-cli')('my-module/bin/cli.js');
Next update your package.json to point to cli-shim.js.
`json`
{
"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.fallback-cli
In most cases this will even be compatible with old versions of your CLI before you introduced .
Note: cli.js and cli-shim.js are arbitrary file names, use whatever you want.
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. relativePath
If that is not possible, you may use to describe where it is relative to the shim.
`js`
fallbackCli('my-module/bin/the-cli.js', '../bin/the-cli.js');
relativePath
If specified, 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:
js`
function defaultRunner(runnerOptions) {
require(runnerOptions.cli);
}
customRunner
#### runnerOptions
If you specify a 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