What is @zkochan/cmd-shim?
@zkochan/cmd-shim is an npm package that allows you to create command-line shims. These shims are small scripts that act as a bridge between the command line and a Node.js script, making it easier to run Node.js scripts as if they were native command-line applications.
What are @zkochan/cmd-shim's main functionalities?
Creating a Command Shim
This feature allows you to create a command shim that links a source Node.js script to a target command. When the target command is executed, it will run the source script. This is useful for making Node.js scripts easily executable from the command line.
const cmdShim = require('@zkochan/cmd-shim');
cmdShim('path/to/source.js', 'path/to/target', (err) => {
if (err) throw err;
console.log('Command shim created successfully!');
});
Creating a Command Shim with Options
This feature allows you to create a command shim with additional options. For example, the `createCmdFile` option can be set to true to create a .cmd file on Windows. This provides more control over the behavior of the created shim.
const cmdShim = require('@zkochan/cmd-shim');
const options = { createCmdFile: true };
cmdShim('path/to/source.js', 'path/to/target', options, (err) => {
if (err) throw err;
console.log('Command shim with options created successfully!');
});
Other packages similar to @zkochan/cmd-shim
bin-wrapper
bin-wrapper is a package that helps you download and manage binaries for your Node.js projects. It provides a higher-level abstraction compared to @zkochan/cmd-shim, focusing on managing the entire lifecycle of binaries rather than just creating command shims.
shelljs
shelljs is a package that provides portable Unix shell commands for Node.js. While it is more general-purpose and offers a wide range of shell commands, it can also be used to create command-line interfaces and scripts, similar to what @zkochan/cmd-shim aims to achieve.
npm-run-all
npm-run-all is a package that allows you to run multiple npm scripts sequentially or in parallel. While it is not specifically designed for creating command shims, it helps in managing and running multiple scripts, which can be a complementary functionality to what @zkochan/cmd-shim offers.
@zkochan/cmd-shim
Used in pnpm for command line application support
The cmd-shim used in pnpm to create executable scripts.
Installation
npm install --save @zkochan/cmd-shim
API
cmdShim(src, to, opts?): Promise<void>
Create a cmd shim at to
for the command line program at from
.
e.g.
const cmdShim = require('@zkochan/cmd-shim')
cmdShim(__dirname + '/cli.js', '/usr/bin/command-name')
.catch(err => console.error(err))
cmdShim.ifExists(src, to, opts?): Promise<void>
The same as above, but will just continue if the file does not exist.
Arguments:
opts.preserveSymlinks
- Boolean - if true, --preserve-symlinks
is added to the options passed to NodeJS.opts.nodePath
- String - sets the NODE_PATH env variable.opts.prependToPath
- String - prepends the passed path to PATH before executing the Node.js program.opts.nodeExecPath
- String - sets the path to the Node.js executable.opts.createCmdFile
- Boolean - is true
on Windows by default. If true, creates a cmd file.opts.createPwshFile
- Boolean - is true
by default. If true, creates a powershell file.opts.progArgs
- String - optional arguments that will be prepend to any CLI arguments
const cmdShim = require('@zkochan/cmd-shim')
cmdShim(__dirname + '/cli.js', '/usr/bin/command-name', { preserveSymlinks: true })
.catch(err => console.error(err))
License
BSD-2-Clause © Zoltan Kochan