@handy-common-utils/oclif-utils
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -26,7 +26,25 @@ import * as Config from '@oclif/config'; | ||
* @param commandInstance instance of the Command | ||
* @param opts format options | ||
* @param options format options | ||
* @return help content | ||
*/ | ||
static generateHelpText(commandInstance: Command, opts?: Partial<HelpOptions>): string; | ||
static injectHelpTextIntoReadmeMd(commandInstance: Command, opts?: Partial<HelpOptions>): Promise<void>; | ||
static generateHelpText(commandInstance: Command, options?: Partial<HelpOptions>): string; | ||
static injectHelpTextIntoReadmeMd(commandInstance: Command, options?: Partial<HelpOptions>): Promise<void>; | ||
static parseCommandLine<T extends { | ||
args: Array<{ | ||
name: string; | ||
}>; | ||
new (...args: any): any; | ||
}>(commandInstance: InstanceType<T>): CommandOptions<T>; | ||
/** | ||
* Reconstruct the command line from already parsed options. | ||
* @param commandInstance When calling from the subclass of `Command`, just pass `this` | ||
* @param options already parsed options | ||
* @returns the command line string corresponding to the parsed options | ||
*/ | ||
static reconstructCommandLine<T extends { | ||
args: Array<{ | ||
name: string; | ||
}>; | ||
new (...args: any): any; | ||
}>(commandInstance: InstanceType<T>, options?: CommandOptions<T>): string; | ||
} | ||
@@ -36,2 +54,4 @@ export declare const prependCliToExamples: typeof OclifUtils.prependCliToExamples; | ||
export declare const injectHelpTextIntoReadmeMd: typeof OclifUtils.injectHelpTextIntoReadmeMd; | ||
export declare const parseCommandLine: typeof OclifUtils.parseCommandLine; | ||
export declare const reconstructCommandLine: typeof OclifUtils.reconstructCommandLine; | ||
export declare type CommandFlags<T> = T extends Parser.Input<infer F> ? F : never; | ||
@@ -38,0 +58,0 @@ export declare type CommandArgNames<T> = T extends { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.injectHelpTextIntoReadmeMd = exports.generateHelpText = exports.prependCliToExamples = exports.OclifUtils = exports.getCommandConfig = void 0; | ||
exports.reconstructCommandLine = exports.parseCommandLine = exports.injectHelpTextIntoReadmeMd = exports.generateHelpText = exports.prependCliToExamples = exports.OclifUtils = exports.getCommandConfig = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -8,5 +8,6 @@ const fs = tslib_1.__importStar(require("fs-extra")); | ||
const plugin_help_1 = require("@oclif/plugin-help"); | ||
const Parser = tslib_1.__importStar(require("@oclif/parser")); | ||
function getCommandConfig(commandInstance) { | ||
const cmd = Config.Command.toCached(commandInstance.ctor); | ||
if (cmd.id == null) { | ||
if (cmd.id === undefined) { | ||
cmd.id = ''; | ||
@@ -18,4 +19,4 @@ } | ||
class SingleCommandHelp extends plugin_help_1.Help { | ||
constructor(commandInstance, opts) { | ||
super(commandInstance.config, opts); | ||
constructor(commandInstance, options) { | ||
super(commandInstance.config, options); | ||
this.commandInstance = commandInstance; | ||
@@ -29,2 +30,8 @@ } | ||
} | ||
const quoteIfNeeded = (text) => { | ||
if (typeof text !== 'string' || /^[\d+,A-Za-z-]+$/.test(text)) { | ||
return `${text}`; | ||
} | ||
return `'${text}'`; | ||
}; | ||
class OclifUtils { | ||
@@ -43,3 +50,3 @@ static getCommandConfig(commandInstance) { | ||
if (Array.isArray(cmd.examples)) { // so that we don't have to hard code command name in the examples | ||
cmd.examples = cmd.examples.map(str => str.startsWith('^ ') ? str.replace(/\^/g, commandInstance.config.bin) : str); | ||
cmd.examples = cmd.examples.map(s => s.startsWith('^ ') ? s.replace('^', commandInstance.config.bin) : s); | ||
} | ||
@@ -50,18 +57,18 @@ } | ||
* @param commandInstance instance of the Command | ||
* @param opts format options | ||
* @param options format options | ||
* @return help content | ||
*/ | ||
static generateHelpText(commandInstance, opts) { | ||
static generateHelpText(commandInstance, options) { | ||
const help = new SingleCommandHelp(commandInstance, { | ||
stripAnsi: true, | ||
maxWidth: 80, | ||
...opts, | ||
...options, | ||
}); | ||
return help.generateHelpText(); | ||
} | ||
static async injectHelpTextIntoReadmeMd(commandInstance, opts) { | ||
static async injectHelpTextIntoReadmeMd(commandInstance, options) { | ||
const helpText = OclifUtils.generateHelpText(commandInstance, { | ||
stripAnsi: true, | ||
maxWidth: 80, | ||
...opts, | ||
...options, | ||
}); | ||
@@ -76,2 +83,40 @@ const helpTextMd = '```\n' + helpText + '\n```\n'; | ||
} | ||
static parseCommandLine(commandInstance) { | ||
return Parser.parse(commandInstance.argv, { context: commandInstance, ...commandInstance.ctor }); | ||
} | ||
/** | ||
* Reconstruct the command line from already parsed options. | ||
* @param commandInstance When calling from the subclass of `Command`, just pass `this` | ||
* @param options already parsed options | ||
* @returns the command line string corresponding to the parsed options | ||
*/ | ||
static reconstructCommandLine(commandInstance, options) { | ||
var _a; | ||
if (options === undefined) { | ||
options = OclifUtils.parseCommandLine(commandInstance); | ||
} | ||
const args = new Array(); | ||
args.push(commandInstance.config.bin); | ||
if (((_a = options.argv) === null || _a === void 0 ? void 0 : _a.length) > 0) { | ||
args.push(...options.argv.map(x => quoteIfNeeded(x))); | ||
} | ||
if (options.flags) { | ||
for (const flagName of Object.keys(options.flags)) { | ||
const flagValue = options.flags[flagName]; | ||
if (flagValue !== false) { // no need if the flag is not toggled on | ||
args.push(`--${flagName}`); | ||
if (typeof flagValue !== 'boolean') { | ||
// eslint-disable-next-line max-depth | ||
if (Array.isArray(flagValue)) { | ||
flagValue.forEach(value => args.push(`${quoteIfNeeded(value)}`)); | ||
} | ||
else { | ||
args.push(`${quoteIfNeeded(flagValue)}`); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return args.join(' '); | ||
} | ||
} | ||
@@ -82,1 +127,3 @@ exports.OclifUtils = OclifUtils; | ||
exports.injectHelpTextIntoReadmeMd = OclifUtils.injectHelpTextIntoReadmeMd; | ||
exports.parseCommandLine = OclifUtils.parseCommandLine; | ||
exports.reconstructCommandLine = OclifUtils.reconstructCommandLine; |
{ | ||
"name": "@handy-common-utils/oclif-utils", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "oclif related utilities", | ||
@@ -26,3 +26,3 @@ "scripts": { | ||
"devDependencies": { | ||
"@handy-common-utils/dev-dependencies": "^1.0.11", | ||
"@handy-common-utils/dev-dependencies": "^1.0.12", | ||
"@types/fs-extra": "^8.1.1", | ||
@@ -29,0 +29,0 @@ "es-check": "^5.1.2" |
@@ -51,2 +51,5 @@ # @handy-common-utils/oclif-utils | ||
} | ||
// This would be helpful if a complex command line needs to be shared | ||
console.log(`Command line: ${OclifUtils.reconstructedcommandLine(this, options)}`); | ||
// You can add this in the scripts section of your package.json: "preversion": "./bin/run --update-readme.md && git add README.md" | ||
@@ -100,3 +103,5 @@ | ||
* [injectHelpTextIntoReadmeMd](#injecthelptextintoreadmemd) | ||
* [parseCommandLine](#parsecommandline) | ||
* [prependCliToExamples](#prependclitoexamples) | ||
* [reconstructCommandLine](#reconstructcommandline) | ||
@@ -106,2 +111,3 @@ #### Functions | ||
* [getCommandConfig](#getcommandconfig) | ||
* [quoteIfNeeded](#quoteifneeded) | ||
@@ -170,2 +176,8 @@ ### Type aliases | ||
#### parseCommandLine | ||
• `Const` **parseCommandLine**: [parseCommandLine](#parsecommandline) = OclifUtils.parseCommandLine | ||
___ | ||
#### prependCliToExamples | ||
@@ -175,2 +187,8 @@ | ||
___ | ||
#### reconstructCommandLine | ||
• `Const` **reconstructCommandLine**: [reconstructCommandLine](#reconstructcommandline) = OclifUtils.reconstructCommandLine | ||
### Functions | ||
@@ -190,2 +208,16 @@ | ||
___ | ||
#### quoteIfNeeded | ||
▸ `Const`**quoteIfNeeded**(`text`: any): string | ||
##### Parameters: | ||
Name | Type | | ||
------ | ------ | | ||
`text` | any | | ||
**Returns:** string | ||
## Classes | ||
@@ -213,3 +245,5 @@ | ||
* [injectHelpTextIntoReadmeMd](#injecthelptextintoreadmemd) | ||
* [parseCommandLine](#parsecommandline) | ||
* [prependCliToExamples](#prependclitoexamples) | ||
* [reconstructCommandLine](#reconstructcommandline) | ||
@@ -220,3 +254,3 @@ #### Methods | ||
▸ `Static` **generateHelpText**(`commandInstance`: Command, `opts?`: Partial\<HelpOptions>): string | ||
▸ `Static` **generateHelpText**(`commandInstance`: Command, `options?`: Partial\<HelpOptions>): string | ||
@@ -230,3 +264,3 @@ Generate formatted text content of help to a command | ||
`commandInstance` | Command | instance of the Command | | ||
`opts?` | Partial\<HelpOptions> | format options | | ||
`options?` | Partial\<HelpOptions> | format options | | ||
@@ -255,3 +289,3 @@ **Returns:** string | ||
▸ `Static` **injectHelpTextIntoReadmeMd**(`commandInstance`: Command, `opts?`: Partial\<HelpOptions>): Promise\<void> | ||
▸ `Static` **injectHelpTextIntoReadmeMd**(`commandInstance`: Command, `options?`: Partial\<HelpOptions>): Promise\<void> | ||
@@ -263,3 +297,3 @@ ###### Parameters: | ||
`commandInstance` | Command | | ||
`opts?` | Partial\<HelpOptions> | | ||
`options?` | Partial\<HelpOptions> | | ||
@@ -270,2 +304,22 @@ **Returns:** Promise\<void> | ||
##### parseCommandLine | ||
▸ `Static` **parseCommandLine**\<T>(`commandInstance`: InstanceType\<T>): [CommandOptions](#commandoptions)\<T> | ||
###### Type parameters: | ||
Name | Type | | ||
------ | ------ | | ||
`T` | { constructor: (...args: any) => any ; args: Array\<{ name: string }> } | | ||
###### Parameters: | ||
Name | Type | | ||
------ | ------ | | ||
`commandInstance` | InstanceType\<T> | | ||
**Returns:** [CommandOptions](#commandoptions)\<T> | ||
___ | ||
##### prependCliToExamples | ||
@@ -288,3 +342,28 @@ | ||
___ | ||
##### reconstructCommandLine | ||
▸ `Static` **reconstructCommandLine**\<T>(`commandInstance`: InstanceType\<T>, `options?`: [CommandOptions](#commandoptions)\<T>): string | ||
Reconstruct the command line from already parsed options. | ||
###### Type parameters: | ||
Name | Type | | ||
------ | ------ | | ||
`T` | { constructor: (...args: any) => any ; args: Array\<{ name: string }> } | | ||
###### Parameters: | ||
Name | Type | Description | | ||
------ | ------ | ------ | | ||
`commandInstance` | InstanceType\<T> | When calling from the subclass of `Command`, just pass `this` | | ||
`options?` | [CommandOptions](#commandoptions)\<T> | already parsed options | | ||
**Returns:** string | ||
the command line string corresponding to the parsed options | ||
<a name="classessinglecommandhelpmd"></a> | ||
@@ -340,3 +419,3 @@ | ||
\+ **new SingleCommandHelp**(`commandInstance`: Command, `opts?`: Partial\<HelpOptions>): [SingleCommandHelp](#classessinglecommandhelpmd) | ||
\+ **new SingleCommandHelp**(`commandInstance`: Command, `options?`: Partial\<HelpOptions>): [SingleCommandHelp](#classessinglecommandhelpmd) | ||
@@ -350,3 +429,3 @@ *Overrides void* | ||
`commandInstance` | Command | | ||
`opts?` | Partial\<HelpOptions> | | ||
`options?` | Partial\<HelpOptions> | | ||
@@ -353,0 +432,0 @@ **Returns:** [SingleCommandHelp](#classessinglecommandhelpmd) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36408
192
695