npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@handy-common-utils/oclif-utils

v3.0.0

Published

oclif related utilities

Downloads

74

Readme

@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:

  • Print out pretty full help/usage information
  • Reconstruct the full command line as a string
  • Insert help/usage information into README.md file automatically

Installation

This library has been verified to be working with @oclif/core v2 and v3, you just need to add it as a dependency:

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

If the versions of oclif components you are using are older (For example, @oclif/[email protected], @oclif/[email protected], @oclif/[email protected], [email protected]), you need to use version 1.1.3 of this package. Or if you are using really old versions of oclif components (that means you are still using @oclif/config, @oclif/command, @oclif/parser), you need to use version 1.0.9 of this package.

Usage

Print out full help/usage information

The function withEnhancedFlagsHandled(...) checks whether '-h' or '--help' or '-v' or '--version' is the only command line argument. If that is the case, it will build the help or version information, print it out, then exit with exit code 0. In such case, your command processing code after it won't get executed.

To use it, just need to add this as the first line in the run() function of your command class:

const options = await withEnhancedFlagsHandled(this, () => this.parse(<Your command class name>));

And, the --help/-h flag needs to be defined, like this:

static flags = {
  ...enhancedFlags,
  // your other flags
};

Below is a full example:

import { Command, Flags } from '@oclif/core'
import { enhancedFlags, withEnhancedFlagsHandled } from '@handy-common-utils/oclif-utils';

class Hello extends Command {

  // Feel free to define description, examples, etc.
  // They will be printed out as part of the help/usage information.

  static flags = {
    ...enhancedFlags,
    // and other flags ...
  }

  // and args ...

  async run(): Promise<void> {
    const options = await withEnhancedFlagsHandled(this, () => this.parse(Hello));

    // your command processing code ...
  }
}

Print out version information

The function withEnhancedFlagsHandled(...) checks whether '-h' or '--help' or '-v' or '--version' is the only command line argument. If that is the case, it will build the help or version information, print it out, then exit with exit code 0. In such case, your command processing code after it won't get executed.

The example in previous section handles '-v' and '--version' too.

Don't use the Flags.version(...) provided by oclif, it is buggy.

Reconstruct the full command line as a string

Sometimes it would be useful to record or print out the full command line. The reconstructCommandLine(...) can return a string containing the full command line.

Below is an example:

import { Command, Flags } from '@oclif/core'
import { reconstructCommandLine, withEnhancedFlagsHandled } from '@handy-common-utils/oclif-utils';

class Hello extends Command {
  // other code ...

  async run(): Promise<void> {
    const options = await withEnhancedFlagsHandled(this, () => this.parse(Hello));
    const fullCommandLine = reconstructCommandLine(this, options);

    // your command processing code ...
  }
}

Insert help/usage information into README.md

In many occasions it would be handy if help/usage information can be inserted into README.md automatically. This can be achieved in two steps.

Step 1: Add --update-readme.md support in your command

As long as enhancedFlags and withEnhancedFlagsHandled(...) are used, then --update-readme.md is supported automatically. See the examples above for details.

Step 2: Run ./bin/run --update-readme.md as part of your workflow

In your CI/CD workflow, you can just run your command with --update-readme.md, then the command will update README.md automatically. You may want to commit the change to README.md after it is updated.

Below are example scripts in package.json:

  "scripts": {
    "preversion": "./bin/run --update-readme.md && git add README.md"
  },

API

Module: oclif-utils

Type Aliases

CommandOptions

Ƭ CommandOptions<C>: C extends Input<infer _F, infer _B, infer _A> ? Awaited<ParsedOutput<FBA<C>>> : never

Typical usage: CommandOptions<typeof YourCommand>

Type parameters

| Name | | :------ | | C |

Variables

enhancedFlags

Const enhancedFlags: Object

Flags of '--help'/'-h' and '--version'/'-v' and --update-readme.md'

Type declaration

| Name | Type | | :------ | :------ | | help | BooleanFlag<boolean> | | update-readme.md | BooleanFlag<boolean> | | version | BooleanFlag<boolean> |

Functions

generateHelpText

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

Generate formatted text content of help to a command

Type parameters

| Name | Type | | :------ | :------ | | T | extends Command |

Parameters

| Name | Type | Description | | :------ | :------ | :------ | | commandInstance | T | instance of the Command | | options? | Partial<HelpOptions> | (optional) format options |

Returns

Promise<string>

help content


injectHelpTextIntoReadmeMd

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

Replace the help text in the README.md file. Help text is marked by <!-- help start --> and <!-- help end -->.

Type parameters

| Name | Type | | :------ | :------ | | T | extends Command |

Parameters

| Name | Type | Description | | :------ | :------ | :------ | | commandInstance | T | instance of the Command | | options? | Partial<HelpOptions> | (optional) format options |

Returns

Promise<void>

void


reconstructCommandLine

reconstructCommandLine<T>(commandInstance, options): string

Reconstruct the command line from already parsed options.

Type parameters

| Name | Type | | :------ | :------ | | T | extends (...args: any) => any |

Parameters

| Name | Type | Description | | :------ | :------ | :------ | | commandInstance | InstanceType<T> | When calling from the subclass of Command, just pass this | | options | ParserOutput<FlagOutput, FlagOutput, ArgOutput> | Already parsed options. When calling from the subclass of Command, it is the return value of this.parse(...). |

Returns

string

the command line string corresponding to the parsed options


withEnhancedFlagsHandled

withEnhancedFlagsHandled<T, O>(commandInstance, parse, helpOptions?, additionalHandler?): Promise<O>

Parse command line arguments, with enhanced flags handled

Type parameters

| Name | Type | | :------ | :------ | | T | extends (...args: any) => any | | O | O |

Parameters

| Name | Type | Description | | :------ | :------ | :------ | | commandInstance | InstanceType<T> | Instance of the command class, usually 'this' | | parse | () => Promise<O> | The function to call 'this.parse()| |helpOptions?|Partial\<HelpOptions\> | Optional options to customise the formatting of help text | | additionalHandler? | (commandInstance: InstanceType\<T\>, cliOptions: undefined\|O) => Promise\<void`> | Optional additional handler |

Returns

Promise<O>

Output from 'this.parse()'.

Throws

Error if the command line arguments are considered invalid by 'this.parse()'.