https://github.com/lamualfa/nodejs-expose-cli
Make your function runnable from CLI
https://github.com/lamualfa/nodejs-expose-cli
cli expose-cli function nodejs
Last synced: about 2 months ago
JSON representation
Make your function runnable from CLI
- Host: GitHub
- URL: https://github.com/lamualfa/nodejs-expose-cli
- Owner: lamualfa
- License: mit
- Created: 2019-12-16T09:33:18.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-12-22T12:02:17.000Z (over 6 years ago)
- Last Synced: 2025-09-07T06:44:43.553Z (9 months ago)
- Topics: cli, expose-cli, function, nodejs
- Language: JavaScript
- Size: 8.79 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Simple Way To Run Your Local Function From CLI
`node index.js localFunction arg1 arg2 arg3`
## Installation
```bash
# NPM
npm i expose-cli
# Yarn
yarn add expose-cli
```
## Example Usage
`index.js`
```javascript
const exposeCli = require('./index');
function printSync() {
console.log('Printed from `printSync`');
}
async function printAsync() {
console.log('Printed from `printAsync`');
}
function printPromise() {
return new Promise(resolve => {
console.log('Printed from `printPromise`');
resolve();
});
}
const printClosure = () => {
console.log('Printed from `printClosure`');
};
function printSyncWithArg(arg1) {
console.log(`Printed from \`printSyncWithArg\` with arg1: ${arg1}`);
}
async function printAsyncWithArg(arg1) {
console.log(`Printed from \`printAsyncWithArg\` with arg1: ${arg1}`);
}
function printSyncWithRestArgs(...args) {
console.log(
`Printed from \`printSyncWithRestArgs\` with args: ${args.join(', ')}`
);
}
async function printAsyncWithRestArgs(...args) {
console.log(
`Printed from \`printAsyncWithRestArgs\` with args: ${args.join(', ')}`
);
}
function returnSync() {
return 'Returned from `returnSync`';
}
async function returnAsync() {
return 'Returned from `returnAsync`';
}
function returnPromise() {
return new Promise(resolve => resolve('Returned from `returnPromise`'));
}
exposeCli(
{
printSync,
printAsync,
printPromise,
printClosure,
printSyncWithArg,
printAsyncWithArg,
printSyncWithRestArgs,
printAsyncWithRestArgs,
returnSync,
returnAsync,
returnPromise
},
{
printReturn: true
}
);
```
### Run local function
**Execute**
`node index.js printSyncWithArg world`
**Output**
`Printed from printSyncWithArg: world`
### Print all listed handler with command `help`
**Execute command**
`node index.js printSyncWithArg help`
**Output**
#### **IMPORTANT!**
The `help` command will display an `` message if you use:
- Closure function. Ex: `() => {}`
- Rest arguments. Ex: `function(...args)`
- Function stored in a variable. Ex: `const handler= function() {}`
- A method in a Class
- Anonymous function
## Calling Format
### `exposeCli(handlers, [config])`
#### `handlers`
Format (Object):
`command` : `handler`
- `command`: `string`
- `handler`: `function` or `object`
- name: `string`
- args: `array`
- description: `string`
- handler: `function`
#### `config`
```javascript
{
// Print returned value from function called
printReturn: false, // default
// Trigger process.exit() when finished
exitOnSuccess: true, // default
// Trigger process.exit(1) when error
exitOnError: true, // default
// cutom log handler
customConsoleLog: console.log, // default
customConsoleError: console.error, // default
// custom command `help` name
customHelpName: 'help', // default
// custom additional command `help` options
customHelp: {
handler: defaultHelpHandler,
name: 'help',
args: '',
description: 'Show all the functions listed.'
} // default
}
```
## Support
- Can be used using [webpack](https://github.com/webpack/webpack)
- Supports calling `async` function or function that return `Promise` or closure
- Supports function that `throw` an error
## Change Log
### v0.0.2
- Fixed bugs
- Added default handler for `help`
- Support `object` format for `handlers` arguments

