clipanion
Advanced tools
Comparing version 3.2.0-rc.6 to 3.2.0-rc.7
@@ -16,2 +16,9 @@ /// <reference types="node" /> | ||
/** | ||
* Environment variables. | ||
* | ||
* @default | ||
* process.env | ||
*/ | ||
env: Record<string, string | undefined>; | ||
/** | ||
* The input stream of the CLI. | ||
@@ -48,3 +55,5 @@ * | ||
export declare type PartialContext<Context extends BaseContext> = UserContextKeys<Context> extends never ? Partial<Pick<Context, keyof BaseContext>> | undefined | void : Partial<Pick<Context, keyof BaseContext>> & UserContext<Context>; | ||
export declare type RunContext<Context extends BaseContext> = Context | PartialContext<Context>; | ||
export declare type RunContext<Context extends BaseContext> = Partial<Pick<Context, keyof BaseContext>> & UserContext<Context>; | ||
export declare type RunCommand<Context extends BaseContext> = Array<CommandClass<Context>> | CommandClass<Context>; | ||
export declare type RunCommandNoContext<Context extends BaseContext> = UserContextKeys<Context> extends never ? RunCommand<Context> : never; | ||
export declare type CliOptions = Readonly<{ | ||
@@ -111,3 +120,3 @@ /** | ||
*/ | ||
process(input: Array<string>): Command<Context>; | ||
process(input: Array<string>, context?: Partial<Context>): Command<Context>; | ||
/** | ||
@@ -134,6 +143,22 @@ * Runs a command. | ||
}; | ||
export declare function runExit<Context extends BaseContext = BaseContext>(commandClasses: RunCommandNoContext<Context>): Promise<void>; | ||
export declare function runExit<Context extends BaseContext = BaseContext>(commandClasses: RunCommand<Context>, context: RunContext<Context>): Promise<void>; | ||
export declare function runExit<Context extends BaseContext = BaseContext>(options: Partial<CliOptions>, commandClasses: RunCommandNoContext<Context>): Promise<void>; | ||
export declare function runExit<Context extends BaseContext = BaseContext>(options: Partial<CliOptions>, commandClasses: RunCommand<Context>, context: RunContext<Context>): Promise<void>; | ||
export declare function runExit<Context extends BaseContext = BaseContext>(commandClasses: RunCommandNoContext<Context>, argv: Array<string>): Promise<void>; | ||
export declare function runExit<Context extends BaseContext = BaseContext>(commandClasses: RunCommand<Context>, argv: Array<string>, context: RunContext<Context>): Promise<void>; | ||
export declare function runExit<Context extends BaseContext = BaseContext>(options: Partial<CliOptions>, commandClasses: RunCommandNoContext<Context>, argv: Array<string>): Promise<void>; | ||
export declare function runExit<Context extends BaseContext = BaseContext>(options: Partial<CliOptions>, commandClasses: RunCommand<Context>, argv: Array<string>, context: RunContext<Context>): Promise<void>; | ||
export declare function run<Context extends BaseContext = BaseContext>(commandClasses: RunCommandNoContext<Context>): Promise<number>; | ||
export declare function run<Context extends BaseContext = BaseContext>(commandClasses: RunCommand<Context>, context: RunContext<Context>): Promise<number>; | ||
export declare function run<Context extends BaseContext = BaseContext>(options: Partial<CliOptions>, commandClasses: RunCommandNoContext<Context>): Promise<number>; | ||
export declare function run<Context extends BaseContext = BaseContext>(options: Partial<CliOptions>, commandClasses: RunCommand<Context>, context: RunContext<Context>): Promise<number>; | ||
export declare function run<Context extends BaseContext = BaseContext>(commandClasses: RunCommandNoContext<Context>, argv: Array<string>): Promise<number>; | ||
export declare function run<Context extends BaseContext = BaseContext>(commandClasses: RunCommand<Context>, argv: Array<string>, context: RunContext<Context>): Promise<number>; | ||
export declare function run<Context extends BaseContext = BaseContext>(options: Partial<CliOptions>, commandClasses: RunCommandNoContext<Context>, argv: Array<string>): Promise<number>; | ||
export declare function run<Context extends BaseContext = BaseContext>(options: Partial<CliOptions>, commandClasses: RunCommand<Context>, argv: Array<string>, context: RunContext<Context>): Promise<number>; | ||
/** | ||
* @template Context The context shared by all commands. Contexts are a set of values, defined when calling the `run`/`runExit` functions from the CLI instance, that will be made available to the commands via `this.context`. | ||
*/ | ||
export declare class Cli<Context extends BaseContext = BaseContext> implements Omit<MiniCli<Context>, `run`> { | ||
export declare class Cli<Context extends BaseContext = BaseContext> implements Omit<MiniCli<Context>, `process` | `run`> { | ||
/** | ||
@@ -145,2 +170,3 @@ * The default context of the CLI. | ||
static defaultContext: { | ||
env: NodeJS.ProcessEnv; | ||
stdin: NodeJS.ReadStream & { | ||
@@ -174,3 +200,3 @@ fd: 0; | ||
*/ | ||
static from<Context extends BaseContext = BaseContext>(commandClasses: Array<CommandClass<Context>>, options?: Partial<CliOptions>): Cli<Context>; | ||
static from<Context extends BaseContext = BaseContext>(commandClasses: RunCommand<Context>, options?: Partial<CliOptions>): Cli<Context>; | ||
constructor({ binaryLabel, binaryName: binaryNameOpt, binaryVersion, enableCapture, enableColors }?: Partial<CliOptions>); | ||
@@ -181,3 +207,4 @@ /** | ||
register(commandClass: CommandClass<Context>): void; | ||
process(input: Array<string>): Command<Context>; | ||
process(input: Array<string>, context: VoidIfEmpty<Omit<Context, keyof BaseContext>>): Command<Context>; | ||
process(input: Array<string>, context: MakeOptional<Context, keyof BaseContext>): Command<Context>; | ||
run(input: Command<Context> | Array<string>, context: VoidIfEmpty<Omit<Context, keyof BaseContext>>): Promise<number>; | ||
@@ -195,3 +222,2 @@ run(input: Command<Context> | Array<string>, context: MakeOptional<Context, keyof BaseContext>): Promise<number>; | ||
runExit(input: Command<Context> | Array<string>, context: MakeOptional<Context, keyof BaseContext>): Promise<void>; | ||
suggest(input: Array<string>, partial: boolean): string[][]; | ||
definitions({ colored }?: { | ||
@@ -198,0 +224,0 @@ colored?: boolean; |
@@ -7,3 +7,2 @@ 'use strict'; | ||
var Command = require('./Command.js'); | ||
var tty = require('tty'); | ||
var core = require('../core.js'); | ||
@@ -13,7 +12,8 @@ var format = require('../format.js'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var tty__default = /*#__PURE__*/_interopDefaultLegacy(tty); | ||
const errorCommandSymbol = Symbol(`clipanion/errorCommand`); | ||
let tty; | ||
try { | ||
tty = require(`tty`); | ||
} | ||
catch (_a) { } | ||
function getDefaultColorDepth() { | ||
@@ -28,2 +28,75 @@ if (process.env.FORCE_COLOR === `0`) | ||
} | ||
async function runExit(...args) { | ||
const { resolvedOptions, resolvedCommandClasses, resolvedArgv, resolvedContext, } = resolveRunParameters(args); | ||
const cli = Cli.from(resolvedCommandClasses, resolvedOptions); | ||
return cli.runExit(resolvedArgv, resolvedContext); | ||
} | ||
async function run(...args) { | ||
const { resolvedOptions, resolvedCommandClasses, resolvedArgv, resolvedContext, } = resolveRunParameters(args); | ||
const cli = Cli.from(resolvedCommandClasses, resolvedOptions); | ||
return cli.run(resolvedArgv, resolvedContext); | ||
} | ||
function resolveRunParameters(args) { | ||
let resolvedOptions; | ||
let resolvedCommandClasses; | ||
let resolvedArgv; | ||
let resolvedContext; | ||
switch (args.length) { | ||
case 1: | ||
{ | ||
resolvedCommandClasses = args[0]; | ||
} | ||
break; | ||
case 2: | ||
{ | ||
if (args[0] && (args[0].prototype instanceof Command.Command) || Array.isArray(args[0])) { | ||
resolvedCommandClasses = args[0]; | ||
if (Array.isArray(args[1])) { | ||
resolvedArgv = args[1]; | ||
} | ||
else { | ||
resolvedContext = args[1]; | ||
} | ||
} | ||
else { | ||
resolvedOptions = args[0]; | ||
resolvedCommandClasses = args[1]; | ||
} | ||
} | ||
break; | ||
case 3: | ||
{ | ||
if (Array.isArray(args[2])) { | ||
resolvedOptions = args[0]; | ||
resolvedCommandClasses = args[1]; | ||
resolvedArgv = args[2]; | ||
} | ||
else if (args[1] && (args[1].prototype instanceof Command.Command) || Array.isArray(args[1])) { | ||
resolvedOptions = args[0]; | ||
resolvedCommandClasses = args[1]; | ||
resolvedContext = args[2]; | ||
} | ||
else { | ||
resolvedCommandClasses = args[0]; | ||
resolvedArgv = args[1]; | ||
resolvedContext = args[2]; | ||
} | ||
} | ||
break; | ||
default: | ||
{ | ||
resolvedOptions = args[0]; | ||
resolvedCommandClasses = args[1]; | ||
resolvedArgv = args[2]; | ||
resolvedContext = args[3]; | ||
} | ||
break; | ||
} | ||
return { | ||
resolvedOptions, | ||
resolvedCommandClasses, | ||
resolvedArgv, | ||
resolvedContext, | ||
}; | ||
} | ||
/** | ||
@@ -50,3 +123,6 @@ * @template Context The context shared by all commands. Contexts are a set of values, defined when calling the `run`/`runExit` functions from the CLI instance, that will be made available to the commands via `this.context`. | ||
const cli = new Cli(options); | ||
for (const commandClass of commandClasses) | ||
const resolvedCommandClasses = Array.isArray(commandClasses) | ||
? commandClasses | ||
: [commandClasses]; | ||
for (const commandClass of resolvedCommandClasses) | ||
cli.register(commandClass); | ||
@@ -81,9 +157,15 @@ return cli; | ||
} | ||
process(input) { | ||
process(input, userContext) { | ||
const { contexts, process } = this.builder.compile(); | ||
const state = process(input); | ||
const context = { | ||
...Cli.defaultContext, | ||
...userContext, | ||
}; | ||
switch (state.selectedIndex) { | ||
case constants.HELP_COMMAND_INDEX: | ||
{ | ||
return HelpCommand.HelpCommand.from(state, contexts); | ||
const command = HelpCommand.HelpCommand.from(state, contexts); | ||
command.context = context; | ||
return command; | ||
} | ||
@@ -97,6 +179,7 @@ default: | ||
const command = new commandClass(); | ||
command.context = context; | ||
command.path = state.path; | ||
try { | ||
for (const [key, { transformer }] of record.specs.entries()) | ||
command[key] = transformer(record.builder, key, state); | ||
command[key] = transformer(record.builder, key, state, context); | ||
return command; | ||
@@ -125,3 +208,3 @@ } | ||
try { | ||
command = this.process(input); | ||
command = this.process(input, context); | ||
} | ||
@@ -147,3 +230,3 @@ catch (error) { | ||
format: colored => this.format(colored), | ||
process: input => this.process(input), | ||
process: (input, subContext) => this.process(input, { ...context, ...subContext }), | ||
run: (input, subContext) => this.run(input, { ...context, ...subContext }), | ||
@@ -168,6 +251,2 @@ usage: (command, opts) => this.usage(command, opts), | ||
} | ||
suggest(input, partial) { | ||
const { suggest } = this.builder.compile(); | ||
return suggest(input, partial); | ||
} | ||
definitions({ colored = false } = {}) { | ||
@@ -377,7 +456,8 @@ const data = []; | ||
Cli.defaultContext = { | ||
env: process.env, | ||
stdin: process.stdin, | ||
stdout: process.stdout, | ||
stderr: process.stderr, | ||
colorDepth: `getColorDepth` in tty__default['default'].WriteStream.prototype | ||
? tty__default['default'].WriteStream.prototype.getColorDepth() | ||
colorDepth: tty && `getColorDepth` in tty.WriteStream.prototype | ||
? tty.WriteStream.prototype.getColorDepth() | ||
: getDefaultColorDepth(), | ||
@@ -417,1 +497,3 @@ }; | ||
exports.Cli = Cli; | ||
exports.run = run; | ||
exports.runExit = runExit; |
@@ -6,3 +6,4 @@ export { Command } from './Command'; | ||
export { formatMarkdownish, ColorFormat } from '../format'; | ||
export { run, runExit } from './Cli'; | ||
export * as Builtins from './builtins'; | ||
export * as Option from './options'; |
@@ -18,3 +18,5 @@ 'use strict'; | ||
exports.Cli = Cli.Cli; | ||
exports.run = Cli.run; | ||
exports.runExit = Cli.runExit; | ||
exports.Builtins = index; | ||
exports.Option = index$1; |
import { StrictValidator } from "typanion"; | ||
import { CommandOptionReturn, GeneralOptionFlags, WithArity } from "./utils"; | ||
export declare type StringOptionNoBoolean<T, Arity extends number = 1> = GeneralOptionFlags & { | ||
env?: string; | ||
validator?: StrictValidator<unknown, T>; | ||
@@ -9,2 +10,3 @@ tolerateBoolean?: false; | ||
export declare type StringOptionTolerateBoolean<T> = GeneralOptionFlags & { | ||
env?: string; | ||
validator?: StrictValidator<unknown, T>; | ||
@@ -11,0 +13,0 @@ tolerateBoolean: boolean; |
@@ -23,5 +23,9 @@ 'use strict'; | ||
}, | ||
transformer(builder, key, state) { | ||
transformer(builder, key, state, context) { | ||
let usedName; | ||
let currentValue = initialValue; | ||
if (typeof opts.env !== `undefined` && context.env[opts.env]) { | ||
usedName = opts.env; | ||
currentValue = context.env[opts.env]; | ||
} | ||
for (const { name, value } of state.options) { | ||
@@ -28,0 +32,0 @@ if (!nameSet.has(name)) |
@@ -17,3 +17,3 @@ import { StrictValidator } from 'typanion'; | ||
definition: <Context extends BaseContext>(builder: CommandBuilder<CliContext<Context>>, key: string) => void; | ||
transformer: <Context extends BaseContext>(builder: CommandBuilder<CliContext<Context>>, key: string, state: RunState) => T; | ||
transformer: <Context extends BaseContext>(builder: CommandBuilder<CliContext<Context>>, key: string, state: RunState, context: Context) => T; | ||
}; | ||
@@ -20,0 +20,0 @@ export declare type CommandOptionReturn<T> = T; |
@@ -14,3 +14,3 @@ { | ||
], | ||
"version": "3.2.0-rc.6", | ||
"version": "3.2.0-rc.7", | ||
"main": "lib/advanced/index", | ||
@@ -17,0 +17,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
216803
5079
10