Security News
Namecheap Takes Down Polyfill.io Service Following Supply Chain Attack
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
@handy-common-utils/oclif-utils
Advanced tools
Readme
oclif (https://oclif.io/) related utilities
With this utility library, you will be able to:
options.args
available./bin/run --update-readme.md
for inserting properly formated CLI manual informationCliConsole
for logging with and without colouringIf 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';
Ƭ CommandOptions<T
>: Interfaces.ParserOutput
<CommandFlags
<T
>, CommandArgs
<T
>>
Name | Type |
---|---|
T | extends Object |
▸ generateHelpText(commandInstance
, options?
): Promise
<string
>
Name | Type |
---|---|
commandInstance | default |
options? | HelpOptions |
Promise
<string
>
▸ getCommandConfig(commandInstance
): Promise
<Command
>
Name | Type |
---|---|
commandInstance | default |
Promise
<Command
>
▸ injectHelpTextIntoReadmeMd(commandInstance
, options?
): Promise
<void
>
Name | Type |
---|---|
commandInstance | default |
options? | HelpOptions |
Promise
<void
>
▸ prependCliToExamples(commandInstance
): void
Name | Type |
---|---|
commandInstance | default |
void
▸ reconstructCommandLine<T
>(commandInstance
, options
): string
Name | Type |
---|---|
T | extends Object |
Name | Type |
---|---|
commandInstance | InstanceType <T > |
options | CommandOptions <T > |
string
oclif-utils.OclifUtils
• new OclifUtils()
▸ Static
generateHelpText(commandInstance
, options?
): Promise
<string
>
Generate formatted text content of help to a command
Name | Type | Description |
---|---|---|
commandInstance | default | instance of the Command |
options? | HelpOptions | format options |
Promise
<string
>
help content
▸ Static
getCommandConfig(commandInstance
): Promise
<Command
>
Name | Type |
---|---|
commandInstance | default |
Promise
<Command
>
▸ Static
injectHelpTextIntoReadmeMd(commandInstance
, options?
): Promise
<void
>
Name | Type |
---|---|
commandInstance | default |
options? | HelpOptions |
Promise
<void
>
▸ 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.
Name | Type | Description |
---|---|---|
commandInstance | default | instance of the Command |
void
void
▸ Static
reconstructCommandLine<T
>(commandInstance
, options
): string
Reconstruct the command line from already parsed options.
Name | Type |
---|---|
T | extends Object |
Name | Type | Description |
---|---|---|
commandInstance | InstanceType <T > | When calling from the subclass of Command , just pass this |
options | CommandOptions <T > | Already parsed options. It can be got with const options = await OclifUtils.parseCommandLine(commandInstance); |
string
the command line string corresponding to the parsed options
FAQs
oclif related utilities
The npm package @handy-common-utils/oclif-utils receives a total of 58 weekly downloads. As such, @handy-common-utils/oclif-utils popularity was classified as not popular.
We found that @handy-common-utils/oclif-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.