JSPM

expose-cli

0.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q32590F
  • License MIT

Make your function runnable from CLI

Package Exports

  • expose-cli

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (expose-cli) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

coding  expose-cli

Simple Way To Run Your Local Function From CLI

node index.js localFunction arg1 arg2 arg3

Installation

# NPM
npm i expose-cli

# Yarn
yarn add expose-cli

Example Usage

index.js

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


Execute command

node index.js printSyncWithArg help

Output

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

IMPORTANT!

The help command will display an <unreachable> 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

{
  // 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
  • 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