bandersnatch
Advanced tools
Comparing version 1.0.0-alpha.7 to 1.0.0-alpha.8
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Argument = exports.defaultOptions = exports.argument = void 0; | ||
const baseArg_1 = require("./baseArg"); | ||
@@ -4,0 +5,0 @@ function argument(name, options) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Autocompleter = exports.autocompleter = void 0; | ||
function autocompleter(program) { | ||
@@ -4,0 +5,0 @@ return new Autocompleter(program); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BaseArg = void 0; | ||
class BaseArg { | ||
@@ -4,0 +5,0 @@ constructor(name) { |
@@ -17,2 +17,3 @@ import { Argv, Arguments as BaseArguments } from 'yargs'; | ||
}; | ||
declare type CommandRunner = (command: string) => Promise<unknown>; | ||
export interface HandlerFn<T> { | ||
@@ -30,2 +31,3 @@ (args: Omit<T, '_' | '$0'>): Promise<any> | any; | ||
private handler?; | ||
private parent?; | ||
constructor(command?: string | string[] | undefined, options?: CommandOptions); | ||
@@ -59,2 +61,9 @@ /** | ||
action(fn: HandlerFn<T>): this; | ||
/** | ||
* Set the parent command. This method may change at any time, not | ||
* intended for public use. | ||
* | ||
* @private | ||
*/ | ||
setParentCommand(parentCommand: Command<any>): void; | ||
private getArguments; | ||
@@ -64,8 +73,14 @@ private getOptions; | ||
/** | ||
* Returns a fully qualified command name (including parent command names). | ||
*/ | ||
private getFqn; | ||
/** | ||
* Calls the command() method on the passed in yargs instance and returns it. | ||
* Takes command runner. | ||
* See https://github.com/yargs/yargs/blob/master/docs/advanced.md#providing-a-command-module | ||
*/ | ||
toYargs(yargs: Argv): Argv<T>; | ||
toYargs(yargs: Argv, commandRunner: CommandRunner): Argv<T>; | ||
/** | ||
* Returns a yargs module for this command. | ||
* Returns a yargs module for this command. Takes command runner, which is | ||
* passed down to getHandler and getBuilder functions. | ||
*/ | ||
@@ -77,5 +92,6 @@ private toModule; | ||
*/ | ||
private getCommand; | ||
private toYargsCommand; | ||
/** | ||
* Returns the builder function to be used with `yargs.command()`. | ||
* Returns the builder function to be used with `yargs.command()`. Takes | ||
* command runner. | ||
*/ | ||
@@ -85,2 +101,3 @@ private getBuilder; | ||
* Wraps the actual command handler to insert prompt and async handler logic. | ||
* Takes command runner. | ||
*/ | ||
@@ -87,0 +104,0 @@ private getHandler; |
@@ -14,2 +14,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Command = exports.command = void 0; | ||
const inquirer_1 = require("inquirer"); | ||
@@ -88,2 +89,3 @@ const argument_1 = require("./argument"); | ||
else if (isCommand(obj)) { | ||
obj.setParentCommand(this); | ||
this.args.push(obj); | ||
@@ -110,2 +112,11 @@ } | ||
} | ||
/** | ||
* Set the parent command. This method may change at any time, not | ||
* intended for public use. | ||
* | ||
* @private | ||
*/ | ||
setParentCommand(parentCommand) { | ||
this.parent = parentCommand; | ||
} | ||
getArguments() { | ||
@@ -121,18 +132,33 @@ return this.args.filter(isArgument); | ||
/** | ||
* Returns a fully qualified command name (including parent command names). | ||
*/ | ||
getFqn() { | ||
if (!this.command) { | ||
throw new Error("Can't get command FQN for default commands."); | ||
} | ||
const command = Array.isArray(this.command) ? this.command[0] : this.command; | ||
if (this.parent) { | ||
return `${this.parent.getFqn()} ${command}`; | ||
} | ||
return command; | ||
} | ||
/** | ||
* Calls the command() method on the passed in yargs instance and returns it. | ||
* Takes command runner. | ||
* See https://github.com/yargs/yargs/blob/master/docs/advanced.md#providing-a-command-module | ||
*/ | ||
toYargs(yargs) { | ||
return yargs.command(this.toModule()); | ||
toYargs(yargs, commandRunner) { | ||
return yargs.command(this.toModule(commandRunner)); | ||
} | ||
/** | ||
* Returns a yargs module for this command. | ||
* Returns a yargs module for this command. Takes command runner, which is | ||
* passed down to getHandler and getBuilder functions. | ||
*/ | ||
toModule() { | ||
toModule(commandRunner) { | ||
const module = { | ||
command: this.getCommand(), | ||
command: this.toYargsCommand(), | ||
aliases: [], | ||
describe: this.options.description || '', | ||
builder: this.getBuilder(), | ||
handler: this.getHandler(), | ||
builder: this.getBuilder(commandRunner), | ||
handler: this.getHandler(commandRunner), | ||
}; | ||
@@ -145,3 +171,3 @@ return module; | ||
*/ | ||
getCommand() { | ||
toYargsCommand() { | ||
if (!this.command) { | ||
@@ -161,5 +187,6 @@ throw new Error('Command name must be set'); | ||
/** | ||
* Returns the builder function to be used with `yargs.command()`. | ||
* Returns the builder function to be used with `yargs.command()`. Takes | ||
* command runner. | ||
*/ | ||
getBuilder() { | ||
getBuilder(commandRunner) { | ||
return (yargs) => { | ||
@@ -169,3 +196,3 @@ // Call toYargs on each argument and option to add it to the command. | ||
// Call toYargs on each subcommand to add it to the command. | ||
yargs = this.getCommands().reduce((yargs, cmd) => cmd.toYargs(yargs), yargs); | ||
yargs = this.getCommands().reduce((yargs, cmd) => cmd.toYargs(yargs, commandRunner), yargs); | ||
return yargs; | ||
@@ -176,4 +203,5 @@ }; | ||
* Wraps the actual command handler to insert prompt and async handler logic. | ||
* Takes command runner. | ||
*/ | ||
getHandler() { | ||
getHandler(commandRunner) { | ||
return (argv) => { | ||
@@ -186,9 +214,10 @@ const { _, $0 } = argv, rest = __rest(argv, ["_", "$0"]); | ||
} | ||
chain = chain.then(async (args) => { | ||
// @todo check if command has sub-commands, and if so, do not throw an | ||
// error but maybe show help instead? | ||
if (!this.handler) { | ||
throw new Error('No handler defined for this command.'); | ||
chain = chain.then((args) => { | ||
if (this.handler) { | ||
return this.handler(args); | ||
} | ||
return this.handler(args); | ||
if (this.getCommands().length) { | ||
return commandRunner(`${this.getFqn()} --help`); | ||
} | ||
throw new Error('No handler defined for this command.'); | ||
}); | ||
@@ -195,0 +224,0 @@ // Save promise chain on argv instance, so we can access it in parse |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./argument")); | ||
__export(require("./autocompleter")); | ||
__export(require("./command")); | ||
__export(require("./option")); | ||
__export(require("./program")); | ||
__export(require("./repl")); | ||
__export(require("./utils")); | ||
__exportStar(require("./argument"), exports); | ||
__exportStar(require("./autocompleter"), exports); | ||
__exportStar(require("./command"), exports); | ||
__exportStar(require("./option"), exports); | ||
__exportStar(require("./program"), exports); | ||
__exportStar(require("./repl"), exports); | ||
__exportStar(require("./utils"), exports); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Option = exports.option = void 0; | ||
const baseArg_1 = require("./baseArg"); | ||
@@ -4,0 +5,0 @@ function option(name) { |
@@ -0,4 +1,8 @@ | ||
import TypedEventEmitter from 'typed-emitter'; | ||
import { Argv } from 'yargs'; | ||
import { Command } from './command'; | ||
import { Repl } from './repl'; | ||
interface Events { | ||
run: (command: string | readonly string[]) => void; | ||
} | ||
declare type ProgramOptions = { | ||
@@ -13,6 +17,13 @@ /** | ||
/** | ||
* Sets a custom REPL prompt. Can also be set by calling | ||
* `program().prompt(...)`. | ||
* | ||
* Defaults to `> `. | ||
*/ | ||
prompt?: string; | ||
/** | ||
* Whether or not to add a global help command that displays an overview of | ||
* commands. Can also be enabled by calling `program().withHelp()`. | ||
* commands. | ||
* | ||
* Defaults to `false`. | ||
* Defaults to `true`. | ||
*/ | ||
@@ -22,15 +33,7 @@ help?: boolean; | ||
* Whether or not to add a global version command that displays the version as | ||
* specified in the package.json file. Can also be enabled by calling | ||
* `program().withVersion()`. | ||
* specified in the package.json file. | ||
* | ||
* Defaults to `false`. | ||
* Defaults to `true`. | ||
*/ | ||
version?: boolean; | ||
/** | ||
* Sets a custom REPL prompt. Can also be set by calling | ||
* `program().prompt(...)`. | ||
* | ||
* Defaults to `>`. | ||
*/ | ||
prompt?: string; | ||
}; | ||
@@ -41,3 +44,4 @@ /** | ||
export declare function program(options?: ProgramOptions): Program; | ||
export declare class Program { | ||
declare const Program_base: new () => TypedEventEmitter<Events>; | ||
export declare class Program extends Program_base { | ||
private options; | ||
@@ -52,11 +56,2 @@ private commands; | ||
/** | ||
* Adds a global help command that displays an overview of commands. | ||
*/ | ||
withHelp(): this; | ||
/** | ||
* Adds a global version command that displays the version as specified in the | ||
* package.json file. | ||
*/ | ||
withVersion(): this; | ||
/** | ||
* Sets a custom REPL prompt. | ||
@@ -83,3 +78,3 @@ */ | ||
*/ | ||
run(command?: string | ReadonlyArray<string>): Promise<unknown>; | ||
run(command?: string | readonly string[]): Promise<unknown>; | ||
/** | ||
@@ -86,0 +81,0 @@ * Run event loop which reads command from stdin. |
@@ -6,2 +6,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Program = exports.program = void 0; | ||
const events_1 = require("events"); | ||
const yargs_1 = __importDefault(require("yargs/yargs")); | ||
@@ -21,4 +23,5 @@ const command_1 = require("./command"); | ||
} | ||
class Program { | ||
class Program extends events_1.EventEmitter { | ||
constructor(options = {}) { | ||
super(); | ||
this.options = options; | ||
@@ -35,17 +38,2 @@ this.commands = []; | ||
/** | ||
* Adds a global help command that displays an overview of commands. | ||
*/ | ||
withHelp() { | ||
this.options.help = true; | ||
return this; | ||
} | ||
/** | ||
* Adds a global version command that displays the version as specified in the | ||
* package.json file. | ||
*/ | ||
withVersion() { | ||
this.options.version = true; | ||
return this; | ||
} | ||
/** | ||
* Sets a custom REPL prompt. | ||
@@ -67,5 +55,5 @@ */ | ||
// Help accepts boolean | ||
yargs.help(!!this.options.help); | ||
yargs.help(this.options.help !== false); | ||
// Version must be false or undefined | ||
!!this.options.version ? yargs.version() : yargs.version(false); | ||
this.options.version !== false ? yargs.version() : yargs.version(false); | ||
// Non-configurable options | ||
@@ -83,3 +71,5 @@ yargs.recommendCommands(); | ||
this.commands.forEach((command) => { | ||
command.toYargs(yargs); | ||
command.toYargs(yargs, (command) => { | ||
return this.run(command); | ||
}); | ||
}); | ||
@@ -107,2 +97,3 @@ return yargs; | ||
const cmd = command || extractCommandFromProcess(); | ||
this.emit('run', cmd); | ||
// Return promise resolving to the return value of the command | ||
@@ -109,0 +100,0 @@ // handler. |
@@ -6,9 +6,11 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Repl = exports.repl = void 0; | ||
const repl_1 = __importDefault(require("repl")); | ||
const string_argv_1 = require("string-argv"); | ||
const autocompleter_1 = require("./autocompleter"); | ||
const DEFAULT_PROMPT = '> '; | ||
/** | ||
* Create new REPL instance. | ||
*/ | ||
function repl(program, prefix = '> ') { | ||
function repl(program, prefix = DEFAULT_PROMPT) { | ||
return new Repl(program, prefix); | ||
@@ -18,3 +20,3 @@ } | ||
class Repl { | ||
constructor(program, prompt = '> ') { | ||
constructor(program, prompt = DEFAULT_PROMPT) { | ||
this.program = program; | ||
@@ -21,0 +23,0 @@ this.prompt = prompt; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isPromise = void 0; | ||
// See https://github.com/then/is-promise/blob/master/index.js | ||
@@ -4,0 +5,0 @@ function isPromise(obj) { |
{ | ||
"name": "bandersnatch", | ||
"description": "Simple TypeScript CLI / REPL framework", | ||
"version": "1.0.0-alpha.7", | ||
"version": "1.0.0-alpha.8", | ||
"repository": { | ||
@@ -37,7 +37,7 @@ "type": "git", | ||
"dependencies": { | ||
"@types/inquirer": "6.5.0", | ||
"@types/yargs": "15.0.5", | ||
"inquirer": "7.2.0", | ||
"string-argv": "0.3.1", | ||
"yargs": "15.3.1" | ||
"@types/inquirer": "^6.5.0", | ||
"@types/yargs": "^15.0.5", | ||
"inquirer": "^7.3.0", | ||
"string-argv": "^0.3.1", | ||
"yargs": "^15.4.0" | ||
}, | ||
@@ -49,4 +49,4 @@ "devDependencies": { | ||
"husky": "4.2.5", | ||
"jest": "26.0.1", | ||
"leasot": "11.0.0", | ||
"jest": "26.1.0", | ||
"leasot": "11.1.0", | ||
"mock-argv": "1.1.6", | ||
@@ -57,3 +57,4 @@ "prettier": "2.0.5", | ||
"ts-node": "8.10.2", | ||
"typescript": "3.9.5" | ||
"typed-emitter": "1.2.0", | ||
"typescript": "3.9.6" | ||
}, | ||
@@ -60,0 +61,0 @@ "husky": { |
@@ -42,14 +42,13 @@ # bandersnatch | ||
- [API](#api) | ||
- [`program(description)`](#programdescription) | ||
- [`program(options)`](#programoptions) | ||
- [`program.description(description)`](#programdescriptiondescription) | ||
- [`program.prompt(prompt)`](#programpromptprompt) | ||
- [`program.add(command)`](#programaddcommand) | ||
- [`program.default(command)`](#programdefaultcommand) | ||
- [`program.prompt(prompt)`](#programpromptprompt) | ||
- [`program.withHelp()`](#programwithhelp) | ||
- [`program.withVersion()`](#programwithversion) | ||
- [`program.run(command)`](#programruncommand) | ||
- [`program.repl()`](#programrepl) | ||
- [`program.runOrRepl()`](#programrunorrepl) | ||
- [`command(name, description)`](#commandname-description) | ||
- [`command.argument(name, description, options)`](#commandargumentname-description-options) | ||
- [`command.option(name, description, options)`](#commandoptionname-description-options) | ||
- [`command(name, options)`](#commandname-options) | ||
- [`command.argument(name, options)`](#commandargumentname-options) | ||
- [`command.option(name, options)`](#commandoptionname-options) | ||
- [`command.command(command)`](#commandcommandcommand) | ||
@@ -198,8 +197,19 @@ - [`command.default()`](#commanddefault) | ||
### `program(description)` | ||
### `program(options)` | ||
Creates a new program. | ||
Creates a new program. Options (object, optional) can contain these keys: | ||
- Description (string, optional) is used in --help output. | ||
- `description` (string, optional) is used in --help output. | ||
- `prompt` (string, default: `>`) use this prompt prefix when in REPL mode. | ||
- `help` (boolean, default: true) adds `help` and `--help` to the program which displays program usage information. | ||
- `version` (boolean, default: true) adds `version` and `--version` to the program which displays program version from package.json. | ||
#### `program.description(description)` | ||
Sets the program description (string, required) used in --help output. | ||
#### `program.prompt(prompt)` | ||
Use this prompt prefix (string, required) when in REPL mode. | ||
#### `program.add(command)` | ||
@@ -221,15 +231,2 @@ | ||
#### `program.prompt(prompt)` | ||
Use this prompt prefix (string, required) when in REPL mode. | ||
#### `program.withHelp()` | ||
Adds `help` and `--help` to the program which displays program usage information. | ||
#### `program.withVersion()` | ||
Adds `version` and `--version` to the program which displays program version from | ||
package.json. | ||
#### `program.run(command)` | ||
@@ -258,3 +255,3 @@ | ||
Invokes `run()` if arguments are passed in, `repl()` otherwise. | ||
Invokes `run()` if process.argv is set, `repl()` otherwise. | ||
@@ -267,3 +264,3 @@ ```ts | ||
### `command(name, description)` | ||
### `command(name, options)` | ||
@@ -274,19 +271,17 @@ Creates a new command. | ||
default command, a name is required. | ||
- Description (string, optional) is used in --help output. | ||
- Options (object, optional) can contain these keys: | ||
- `description` (string, optional) is used in --help output. | ||
#### `command.argument(name, description, options)` | ||
#### `command.argument(name, options)` | ||
Adds a positional argument to the command. | ||
- Name (string, required) is used to identify the argument. Can also be an array | ||
of strings, in which case subsequent items will be treated as command aliases. | ||
- Description (string, optional) is used in --help output. | ||
- Options can be provided to change the behavior of the | ||
argument. Object with any of these keys: | ||
- Name (string, required) is used to identify the argument. Can also be an array of strings, in which case subsequent items will be treated as command aliases. | ||
- Options can be provided to change the behavior of the argument. Object with any of these keys: | ||
- `description` (string, optional) is used in --help output. | ||
- `optional` (boolean) makes this argument optional. | ||
- `variadic` (boolean) eagerly take all remaining arguments and parse as an array. | ||
Only valid for the last argument. | ||
- `variadic` (boolean) eagerly take all remaining arguments and parse as an array. Only valid for the last argument. | ||
- ... | ||
#### `command.option(name, description, options)` | ||
#### `command.option(name, options)` | ||
@@ -296,5 +291,4 @@ Adds an option to the command. | ||
- Name (string, required) is used to identify the option. | ||
- Description (string, optional) is used in --help output. | ||
- Options (OptionOptions) can be provided to change the behavior of the | ||
option. Object with any of these keys: | ||
- Options (object, optional) can be provided to change the behavior of the option. Object with any of these keys: | ||
- `description` (string, optional) is used in --help output. | ||
- `alias` (string or array of strings) alias(es) for the option key. | ||
@@ -313,5 +307,7 @@ - ... | ||
Function to execute when the command is invoked. Is called with one argument: an | ||
object containing key/value pairs of parsed arguments and options. | ||
Function which executes when the command is invoked. Is called with these arguments: | ||
1. Args (object) is an object containing key/value pairs of parsed arguments and options. | ||
2. Command runner (function) can be invoked with one (string) parameter to execute another command. | ||
## Bundle | ||
@@ -318,0 +314,0 @@ |
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
50066
1031
13
487
+ Added@types/yargs@15.0.19(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedcli-width@3.0.0(transitive)
+ Addedinquirer@7.3.3(transitive)
+ Addedstring-argv@0.3.2(transitive)
+ Addedyargs@15.4.1(transitive)
- Removed@types/yargs@15.0.5(transitive)
- Removedchalk@3.0.0(transitive)
- Removedcli-width@2.2.1(transitive)
- Removedinquirer@7.2.0(transitive)
- Removedstring-argv@0.3.1(transitive)
- Removedyargs@15.3.1(transitive)
Updated@types/inquirer@^6.5.0
Updated@types/yargs@^15.0.5
Updatedinquirer@^7.3.0
Updatedstring-argv@^0.3.1
Updatedyargs@^15.4.0