Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@handy-common-utils/oclif-utils

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@handy-common-utils/oclif-utils

oclif related utilities

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@handy-common-utils/oclif-utils

oclif (https://oclif.io/) related utilities

Version Downloads/week CI codecov

Features

With this utility library, you will be able to:

  • Make type information of options.args available
  • Update README.md file by ./bin/run --update-readme.md for inserting properly formated CLI manual information
  • Prepend command line name to the examples
  • Reconstruct the full command line as a string
  • Handy CliConsole for logging with and without colouring

How to use

If you are using latest versions of @oclif/core(introduction, migration), just add latest version of this package as dependency:

npm install @handy-common-utils/oclif-utils@latest

Otherwise if the versions of oclif components you are using are older (that means you are still using @oclif/config, @oclif/command, @oclif/parser), you need to use the older version of this package:

npm install @handy-common-utils/oclif-utils@1.0.9

Then you can use it in the code:

import { Command, Flags } from '@oclif/core';
import { OclifUtils, cliConsole, cliConsoleWithColour } from '@handy-common-utils/oclif-utils';

class AwsServerlessDataflow extends Command {
  // You can use "typeof AwsServerlessDataflow.Options" in other places to refer to the type, if you want this convenience
  static Options: CommandOptions<typeof AwsServerlessDataflow>

  // ... other code ...

  static flags = {
    version: Flags.version({ char: 'v' }),
    help: { ...Flags.help({ char: 'h' }), parse: async (_: any, cmd: Command) => {
      cmd.log(await OclifUtils.generateHelpText(cmd));
      cmd.exit(0);
    } },
    'update-readme.md': flags.boolean({ hidden: true, description: 'For developers only, don\'t use' }),
    debug: Flags.boolean({ char: 'd', name: 'debug' }),
    // ... other code ...
  }

  static args = [
    { name: 'path' as const, default: 'dataflow', description: 'path for putting generated website files' },
    //             ^----- this is needed for the "path" property of options.args to be known to the compiler
  ];

  static examples = [
    '^ -r ap-southeast-2 -s',
    `^ -r ap-southeast-2 -s -i '*boi*' -i '*datahub*' \\
      -x '*jameshu*' -c`,
    `^ -r ap-southeast-2 -s -i '*lr-*' \\
      -i '*lead*' -x '*slack*' -x '*lead-prioritization*' \\
      -x '*lead-scor*' -x '*LeadCapture*' -c`,
  ];

  protected async init(): Promise<any> {
    OclifUtils.prependCliToExamples(this);  // "^" at the beginning of the examples will be replaced by the actual command
    return super.init();
  }

  async run(): Promise<void> {
    const options = await this.parse() as CommandOptions<typeof AwsServerlessDataflow>; // as typeof AwsServerlessDataflow.Options
    if (options.flags['update-readme.md']) {
      OclifUtils.injectHelpTextIntoReadmeMd(this); // you need to have <!-- help start -->...<!-- help end --> in your README.md
      return;
    }
    // This would be helpful if a complex command line needs to be shared
    if (options.flags.debug) {
      cliConsoleWithColour.info(`Command line: ${OclifUtils.reconstructCommandLine(this, options)}`);
    }

    // Now the compiler knows that options.args has a property named "path"
    cliConsole.log(options.args.path);

    // You can add this in the scripts section of your package.json:  "preversion": "./bin/run --update-readme.md && git add README.md"

    // ... other code ...
  }
}
export = AwsServerlessDataflow

You can either import and use the class as shown above, or you can import individual functions directly like below:

import { prependCliToExamples } from '@handy-common-utils/oclif-utils';

API

@handy-common-utils/oclif-utils

Modules

Classes

Class: CliConsole<DEBUG_FUNC, INFO_FUNC, WARN_FUNC, ERROR_FUNC>

cli-console.CliConsole

Encapsulation of console output functions.

Type parameters
NameType
DEBUG_FUNCextends Function
INFO_FUNCextends Function
WARN_FUNCextends Function
ERROR_FUNCextends Function
Constructors
constructor

new CliConsole<DEBUG_FUNC, INFO_FUNC, WARN_FUNC, ERROR_FUNC>(debugFunction, infoFunction, warnFunction, errorFunction, isDebug?, isQuiet?)

Constructor

Type parameters
NameType
DEBUG_FUNCextends Function
INFO_FUNCextends Function
WARN_FUNCextends Function
ERROR_FUNCextends Function
Parameters
NameTypeDefault valueDescription
debugFunctionDEBUG_FUNCundefinedfunction for outputting debug information
infoFunctionINFO_FUNCundefinedfunction for outputting info information
warnFunctionWARN_FUNCundefinedfunction for outputting warn information
errorFunctionERROR_FUNCundefinedfunction for outputting error information
isDebugbooleanfalseis debug output enabled or not
isQuietbooleanfalseis quiet mode enabled or not. When quiet mode is enabled, debug and info output would be discarded.
Properties
PropertyDescription
debug: DEBUG_FUNC
error: ERROR_FUNC
info: INFO_FUNC
isDebug: boolean = false
isQuiet: boolean = false
warn: WARN_FUNC
Static Protected NO_OP_FUNC: () => voidType declaration:
▸ (): void

Returns:
void
Methods
default

Static default<FLAGS>(flags, debugFlagName?, quietFlagName?): CliConsole<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

Build an instance with console.log/info/warn/error.

Type parameters
NameType
FLAGSextends Record<string, any>
Parameters
NameTypeDefault valueDescription
flagsFLAGSundefinedThe flag object that contains fields for knowning whether debug is enabled and whether quiet mode is enabled. Those fields are evaluated only once within the function. They are not evaluated when debug/info/warn/error functions are called.
debugFlagNamekeyof FLAGS'debug'Name of the debug field in the flags object
quietFlagNamekeyof FLAGS'quiet'Name of the quiet field in the flags object
Returns

CliConsole<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

An instance that uses console.log/info/warn/error.


withColour

Static withColour<FLAGS, COLOURER>(flags, colourer, debugColourFuncName?, infoColourFuncName?, warnColourFuncName?, errorColourFuncName?, debugFlagName?, quietFlagName?): CliConsole<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

Build an instance with console.log/info/warn/error and chalk/colors/cli-color.

Type parameters
NameType
FLAGSextends Record<string, any>
COLOURERextends Record<string, any>
Parameters
NameTypeDefault valueDescription
flagsFLAGSundefinedThe flag object that contains fields for knowning whether debug is enabled and whether quiet mode is enabled. Those fields are evaluated only once within the function. They are not evaluated when debug/info/warn/error functions are called.
colourerCOLOURERundefinedSupplier of the colouring function, such as chalk or colors or cli-color
debugColourFuncNamekeyof COLOURER'grey'Name of the function within colourer that will be used to add colour to debug messages, or null if colouring is not desired.
infoColourFuncName?keyof COLOURERundefinedName of the function within colourer that will be used to add colour to info messages, or null if colouring is not desired.
warnColourFuncNamekeyof COLOURER'yellow'Name of the function within colourer that will be used to add colour to warn messages, or null if colouring is not desired.
errorColourFuncNamekeyof COLOURER'red'Name of the function within colourer that will be used to add colour to error messages, or null if colouring is not desired.
debugFlagNamekeyof FLAGS'debug'Name of the debug field in the flags object
quietFlagNamekeyof FLAGS'quiet'Name of the quiet field in the flags object
Returns

CliConsole<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

An instance that uses console.log/info/warn/error and also adds colour to the messages using chalk/colors/cli-color.

Class: OclifUtils

oclif-utils.OclifUtils

Constructors
constructor

new OclifUtils()

Methods
generateHelpText

Static generateHelpText(commandInstance, options?): Promise<string>

Generate formatted text content of help to a command

Parameters
NameTypeDescription
commandInstancedefaultinstance of the Command
options?HelpOptionsformat options
Returns

Promise<string>

help content


getCommandConfig

Static getCommandConfig(commandInstance): Promise<Command>

Parameters
NameType
commandInstancedefault
Returns

Promise<Command>


injectHelpTextIntoReadmeMd

Static injectHelpTextIntoReadmeMd(commandInstance, options?): Promise<void>

Parameters
NameType
commandInstancedefault
options?HelpOptions
Returns

Promise<void>


prependCliToExamples

Static prependCliToExamples(commandInstance): void

Use this function to prepend command line to examples, so that we don't have to hard code command name in the examples. This function needs to be called from init() function of the Command.

Parameters
NameTypeDescription
commandInstancedefaultinstance of the Command
Returns

void

void


reconstructCommandLine

Static reconstructCommandLine<T>(commandInstance, options): string

Reconstruct the command line from already parsed options.

Type parameters
NameType
Textends Object
Parameters
NameTypeDescription
commandInstanceInstanceType<T>When calling from the subclass of Command, just pass this
optionsCommandOptions<T>Already parsed options. It can be got with const options = await OclifUtils.parseCommandLine(commandInstance);
Returns

string

the command line string corresponding to the parsed options

Modules

Module: cli-console

Classes
Type aliases
DefaultCliConsole

Ƭ DefaultCliConsole: ReturnType<typeof default>

CliConsole that has function signatures based on console.log/info/warn/error.

Functions
cliConsole

cliConsole<FLAGS>(flags, debugFlagName?, quietFlagName?): CliConsole<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

Build an encapsulation of console output functions with console.log/info/warn/error.

Type parameters
NameType
FLAGSextends Record<string, any>
Parameters
NameTypeDefault valueDescription
flagsFLAGSundefinedThe flag object that contains fields for knowning whether debug is enabled and whether quiet mode is enabled. Those fields are evaluated only once within the function. They are not evaluated when debug/info/warn/error functions are called.
debugFlagNamekeyof FLAGS'debug'Name of the debug field in the flags object
quietFlagNamekeyof FLAGS'quiet'Name of the quiet field in the flags object
Returns

CliConsole<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

An CliConsole instance that uses console.log/info/warn/error.


cliConsoleWithColour

cliConsoleWithColour<FLAGS, COLOURER>(flags, colourer, debugColourFuncName?, infoColourFuncName?, warnColourFuncName?, errorColourFuncName?, debugFlagName?, quietFlagName?): CliConsole<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

Build an encapsulation of console output functions with console.log/info/warn/error and chalk/colors/cli-color.

Type parameters
NameType
FLAGSextends Record<string, any>
COLOURERextends Record<string, any>
Parameters
NameTypeDefault valueDescription
flagsFLAGSundefinedThe flag object that contains fields for knowning whether debug is enabled and whether quiet mode is enabled. Those fields are evaluated only once within the function. They are not evaluated when debug/info/warn/error functions are called.
colourerCOLOURERundefinedSupplier of the colouring function, such as chalk or colors or cli-color
debugColourFuncNamekeyof COLOURER'grey'Name of the function within colourer that will be used to add colour to debug messages, or null if colouring is not desired.
infoColourFuncName?keyof COLOURERundefinedName of the function within colourer that will be used to add colour to info messages, or null if colouring is not desired.
warnColourFuncNamekeyof COLOURER'yellow'Name of the function within colourer that will be used to add colour to warn messages, or null if colouring is not desired.
errorColourFuncNamekeyof COLOURER'red'Name of the function within colourer that will be used to add colour to error messages, or null if colouring is not desired.
debugFlagNamekeyof FLAGS'debug'Name of the debug field in the flags object
quietFlagNamekeyof FLAGS'quiet'Name of the quiet field in the flags object
Returns

CliConsole<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

An CliConsole instance that uses console.log/info/warn/error and also adds colour to the messages using chalk/colors/cli-color.

Module: oclif-utils

Classes
Type aliases
CommandOptions

Ƭ CommandOptions<T>: Interfaces.ParserOutput<CommandFlags<T>, CommandArgs<T>>

Type parameters
NameType
Textends Object
Functions
generateHelpText

generateHelpText(commandInstance, options?): Promise<string>

Parameters
NameType
commandInstancedefault
options?HelpOptions
Returns

Promise<string>


getCommandConfig

getCommandConfig(commandInstance): Promise<Command>

Parameters
NameType
commandInstancedefault
Returns

Promise<Command>


injectHelpTextIntoReadmeMd

injectHelpTextIntoReadmeMd(commandInstance, options?): Promise<void>

Parameters
NameType
commandInstancedefault
options?HelpOptions
Returns

Promise<void>


prependCliToExamples

prependCliToExamples(commandInstance): void

Parameters
NameType
commandInstancedefault
Returns

void


reconstructCommandLine

reconstructCommandLine<T>(commandInstance, options): string

Type parameters
NameType
Textends Object
Parameters
NameType
commandInstanceInstanceType<T>
optionsCommandOptions<T>
Returns

string

Keywords

FAQs

Package last updated on 03 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc