An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# coding  **expose-cli**

## 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**

Screenshot-from-2019-12-22-17-10-58

#### **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