From 522b019c9a50924605986a1e6e0cb716d47bcbca Mon Sep 17 00:00:00 2001 From: Mael Le Guen Date: Thu, 30 Apr 2020 08:57:53 +0200 Subject: [PATCH] refactor(ts): group type definitions and helpers (#1632) * Group all type definitions and helpers in using modules * Move .d.ts to typings directory * Get rid of types directory --- lib/apply-extends.ts | 2 +- lib/argsert.ts | 3 +- lib/command-types.ts | 23 +++++++++ lib/{types/dictionary.ts => common-types.ts} | 0 lib/completion.ts | 38 ++++++++++---- lib/obj-filter.ts | 2 +- lib/parse-command.ts | 13 ++++- lib/process-argv.ts | 9 +++- lib/type-helpers/command-builder.ts | 5 -- lib/type-helpers/completion-function.ts | 5 -- lib/type-helpers/index.ts | 2 - lib/types/command-builder.ts | 8 --- lib/types/command-handler.ts | 5 -- lib/types/command-instance.ts | 8 --- lib/types/completion-function.ts | 11 ---- lib/types/completion-instance.ts | 11 ---- lib/types/context.ts | 4 -- lib/types/custom-check.ts | 6 --- lib/types/electron-process.ts | 6 --- lib/types/failure-function.ts | 6 --- lib/types/frozen-usage-instance.ts | 12 ----- lib/types/frozen-validation-instance.ts | 9 ---- lib/types/index.ts | 20 -------- lib/types/key-or-pos.ts | 1 - lib/types/logger-instance.ts | 1 - lib/types/options.ts | 20 -------- lib/types/parsed-command.ts | 7 --- lib/types/parser-configuration.ts | 6 --- lib/types/positional.ts | 4 -- lib/types/usage-instance.ts | 36 ------------- lib/types/validation-instance.ts | 29 ----------- lib/{types => typings}/set-blocking.d.ts | 0 lib/{types => typings}/y18n.d.ts | 0 lib/usage.ts | 51 ++++++++++++++++++- lib/validation.ts | 44 +++++++++++++++- .../yargs-instance.ts => yargs-types.ts} | 42 ++++++++++++--- 36 files changed, 198 insertions(+), 251 deletions(-) create mode 100644 lib/command-types.ts rename lib/{types/dictionary.ts => common-types.ts} (100%) delete mode 100644 lib/type-helpers/command-builder.ts delete mode 100644 lib/type-helpers/completion-function.ts delete mode 100644 lib/type-helpers/index.ts delete mode 100644 lib/types/command-builder.ts delete mode 100644 lib/types/command-handler.ts delete mode 100644 lib/types/command-instance.ts delete mode 100644 lib/types/completion-function.ts delete mode 100644 lib/types/completion-instance.ts delete mode 100644 lib/types/context.ts delete mode 100644 lib/types/custom-check.ts delete mode 100644 lib/types/electron-process.ts delete mode 100644 lib/types/failure-function.ts delete mode 100644 lib/types/frozen-usage-instance.ts delete mode 100644 lib/types/frozen-validation-instance.ts delete mode 100644 lib/types/index.ts delete mode 100644 lib/types/key-or-pos.ts delete mode 100644 lib/types/logger-instance.ts delete mode 100644 lib/types/options.ts delete mode 100644 lib/types/parsed-command.ts delete mode 100644 lib/types/parser-configuration.ts delete mode 100644 lib/types/positional.ts delete mode 100644 lib/types/usage-instance.ts delete mode 100644 lib/types/validation-instance.ts rename lib/{types => typings}/set-blocking.d.ts (100%) rename lib/{types => typings}/y18n.d.ts (100%) rename lib/{types/yargs-instance.ts => yargs-types.ts} (58%) diff --git a/lib/apply-extends.ts b/lib/apply-extends.ts index c7ff717b3..d235ac9a7 100644 --- a/lib/apply-extends.ts +++ b/lib/apply-extends.ts @@ -1,7 +1,7 @@ +import { Dictionary } from './common-types' import * as fs from 'fs' import * as path from 'path' -import { Dictionary } from './types' import { YError } from './yerror' let previouslyVisitedConfigs: string[] = [] diff --git a/lib/argsert.ts b/lib/argsert.ts index caaa7d358..9c0d2d3ab 100644 --- a/lib/argsert.ts +++ b/lib/argsert.ts @@ -1,6 +1,5 @@ import { YError } from './yerror' -import { parseCommand } from './parse-command' -import { ParsedCommand } from './types' +import { parseCommand, ParsedCommand } from './parse-command' const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'] export function argsert (callerArguments: any[], length?: number): void diff --git a/lib/command-types.ts b/lib/command-types.ts new file mode 100644 index 000000000..6a1ecd906 --- /dev/null +++ b/lib/command-types.ts @@ -0,0 +1,23 @@ +import { Dictionary } from './common-types' +import { YargsInstance } from './yargs-types' + +/** Instance of the command module. */ +export interface CommandInstance { + getCommandHandlers (): Dictionary + getCommands (): string[] +} + +interface CommandHandler { + builder: CommandBuilder +} + +// To be completed later with other CommandBuilder flavours +type CommandBuilder = FunctionCommandBuilder + +interface FunctionCommandBuilder { + (y: YargsInstance): YargsInstance +} + +export function isFunctionCommandBuilder (builder: CommandBuilder): builder is FunctionCommandBuilder { + return typeof builder === 'function' +} diff --git a/lib/types/dictionary.ts b/lib/common-types.ts similarity index 100% rename from lib/types/dictionary.ts rename to lib/common-types.ts diff --git a/lib/completion.ts b/lib/completion.ts index 8c7be094e..2f40d3f50 100644 --- a/lib/completion.ts +++ b/lib/completion.ts @@ -1,16 +1,11 @@ -import * as path from 'path' +import { CommandInstance, isFunctionCommandBuilder } from './command-types' import * as templates from './completion-templates' import { isPromise } from './is-promise' import { parseCommand } from './parse-command' -import { - CommandInstance, - CompletionFunction, - CompletionInstance, - UsageInstance, - YargsInstance -} from './types' -import { isSyncCompletionFunction, isFunctionCommandBuilder } from './type-helpers' -import { DetailedArguments } from 'yargs-parser' +import * as path from 'path' +import { UsageInstance } from './usage' +import { YargsInstance } from './yargs-types' +import { Arguments, DetailedArguments } from 'yargs-parser' // add bash completions to your // yargs-powered applications. @@ -140,3 +135,26 @@ export function completion (yargs: YargsInstance, usage: UsageInstance, command: return self } + +/** Instance of the completion module. */ +interface CompletionInstance { + completionKey: string + generateCompletionScript($0: string, cmd: string): string + getCompletion(args: string[], done: (completions: string[]) => any): any + registerFunction(fn: CompletionFunction): void + setParsed(parsed: DetailedArguments): void +} + +type CompletionFunction = SyncCompletionFunction | AsyncCompletionFunction + +interface SyncCompletionFunction { + (current: string, argv: Arguments): string[] | Promise +} + +interface AsyncCompletionFunction { + (current: string, argv: Arguments, done: (completions: string[]) => any): any +} + +function isSyncCompletionFunction (completionFunction: CompletionFunction): completionFunction is SyncCompletionFunction { + return completionFunction.length < 3 +} diff --git a/lib/obj-filter.ts b/lib/obj-filter.ts index 328aff03d..e8cfa2112 100644 --- a/lib/obj-filter.ts +++ b/lib/obj-filter.ts @@ -1,4 +1,4 @@ -import { Dictionary } from './types' +import { Dictionary } from './common-types' export function objFilter (original: Dictionary, filter: (k: string, v: T) => boolean = () => true) { const obj: Dictionary = {} diff --git a/lib/parse-command.ts b/lib/parse-command.ts index 3e2873aed..8b0c907ad 100644 --- a/lib/parse-command.ts +++ b/lib/parse-command.ts @@ -1,5 +1,3 @@ -import { ParsedCommand } from './types' - export function parseCommand (cmd: string) { const extraSpacesStrippedCommand = cmd.replace(/\s{2,}/g, ' ') const splitCommand = extraSpacesStrippedCommand.split(/\s+(?![^[]*]|[^<]*>)/) @@ -31,3 +29,14 @@ export function parseCommand (cmd: string) { }) return parsedCommand } + +export interface ParsedCommand { + cmd: string + demanded: Positional[] + optional: Positional[] +} + +interface Positional { + cmd: string[] + variadic: boolean +} diff --git a/lib/process-argv.ts b/lib/process-argv.ts index 48f789d86..190673f94 100644 --- a/lib/process-argv.ts +++ b/lib/process-argv.ts @@ -1,5 +1,3 @@ -import { ElectronProcess } from './types' - function getProcessArgvBinIndex () { // The binary name is the first command line argument for: // - bundled Electron apps: bin argv1 argv2 ... argvn @@ -29,3 +27,10 @@ export function getProcessArgvWithoutBin () { export function getProcessArgvBin () { return process.argv[getProcessArgvBinIndex()] } + +interface ElectronProcess extends NodeJS.Process { + defaultApp?: boolean + versions: NodeJS.ProcessVersions & { + electron: string + } +} diff --git a/lib/type-helpers/command-builder.ts b/lib/type-helpers/command-builder.ts deleted file mode 100644 index 69e7383ab..000000000 --- a/lib/type-helpers/command-builder.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { CommandBuilder, FunctionCommandBuilder } from '../types' - -export function isFunctionCommandBuilder (builder: CommandBuilder): builder is FunctionCommandBuilder { - return typeof builder === 'function' -} diff --git a/lib/type-helpers/completion-function.ts b/lib/type-helpers/completion-function.ts deleted file mode 100644 index 3602c43fa..000000000 --- a/lib/type-helpers/completion-function.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { CompletionFunction, SyncCompletionFunction } from '../types' - -export function isSyncCompletionFunction (completionFunction: CompletionFunction): completionFunction is SyncCompletionFunction { - return completionFunction.length < 3 -} diff --git a/lib/type-helpers/index.ts b/lib/type-helpers/index.ts deleted file mode 100644 index 6f2702046..000000000 --- a/lib/type-helpers/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './completion-function' -export * from './command-builder' diff --git a/lib/types/command-builder.ts b/lib/types/command-builder.ts deleted file mode 100644 index 377f04eeb..000000000 --- a/lib/types/command-builder.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { YargsInstance } from './yargs-instance' - -// To be completed later with other CommandBuilder flavours -export type CommandBuilder = FunctionCommandBuilder - -export interface FunctionCommandBuilder { - (y: YargsInstance): YargsInstance -} diff --git a/lib/types/command-handler.ts b/lib/types/command-handler.ts deleted file mode 100644 index 695a2966d..000000000 --- a/lib/types/command-handler.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { CommandBuilder } from './command-builder' - -export interface CommandHandler { - builder: CommandBuilder -} diff --git a/lib/types/command-instance.ts b/lib/types/command-instance.ts deleted file mode 100644 index 3ef7f0328..000000000 --- a/lib/types/command-instance.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { CommandHandler } from './command-handler' -import { Dictionary } from './dictionary' - -/** Instance of the command module. */ -export interface CommandInstance { - getCommandHandlers (): Dictionary - getCommands (): string[] -} diff --git a/lib/types/completion-function.ts b/lib/types/completion-function.ts deleted file mode 100644 index 93828658f..000000000 --- a/lib/types/completion-function.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Arguments } from 'yargs-parser' - -export type CompletionFunction = SyncCompletionFunction | AsyncCompletionFunction - -export interface SyncCompletionFunction { - (current: string, argv: Arguments): string[] | Promise -} - -export interface AsyncCompletionFunction { - (current: string, argv: Arguments, done: (completions: string[]) => any): any -} diff --git a/lib/types/completion-instance.ts b/lib/types/completion-instance.ts deleted file mode 100644 index 1c8f09bfb..000000000 --- a/lib/types/completion-instance.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { CompletionFunction } from './completion-function' -import { DetailedArguments } from 'yargs-parser' - -/** Instance of the completion module. */ -export interface CompletionInstance { - completionKey: string - generateCompletionScript ($0: string, cmd: string): string - getCompletion (args: string[], done: (completions: string[]) => any): any - registerFunction(fn: CompletionFunction): void - setParsed (parsed: DetailedArguments): void -} diff --git a/lib/types/context.ts b/lib/types/context.ts deleted file mode 100644 index 281aa2caa..000000000 --- a/lib/types/context.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** Yargs' context. */ -export interface Context { - commands: string[] -} diff --git a/lib/types/custom-check.ts b/lib/types/custom-check.ts deleted file mode 100644 index 063c817b7..000000000 --- a/lib/types/custom-check.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Arguments, DetailedArguments } from 'yargs-parser' - -export interface CustomCheck { - func: (argv: Arguments, aliases: DetailedArguments['aliases']) => any - global: boolean -} diff --git a/lib/types/electron-process.ts b/lib/types/electron-process.ts deleted file mode 100644 index f72ee6cf9..000000000 --- a/lib/types/electron-process.ts +++ /dev/null @@ -1,6 +0,0 @@ -export interface ElectronProcess extends NodeJS.Process { - defaultApp?: boolean - versions: NodeJS.ProcessVersions & { - electron: string - } -} diff --git a/lib/types/failure-function.ts b/lib/types/failure-function.ts deleted file mode 100644 index b16d43060..000000000 --- a/lib/types/failure-function.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { UsageInstance } from './usage-instance' -import { YError } from '../yerror' - -export interface FailureFunction { - (msg: string | undefined | null, err: YError | string | undefined, usage: UsageInstance): void -} diff --git a/lib/types/frozen-usage-instance.ts b/lib/types/frozen-usage-instance.ts deleted file mode 100644 index d384ad3d4..000000000 --- a/lib/types/frozen-usage-instance.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Dictionary } from './dictionary' - -export interface FrozenUsageInstance { - failMessage: string | undefined | null - failureOutput: boolean - usages: [string, string][] - usageDisabled: boolean - epilogs: string[] - examples: [string, string][] - commands: [string, string, boolean, string[], boolean][] - descriptions: Dictionary -} diff --git a/lib/types/frozen-validation-instance.ts b/lib/types/frozen-validation-instance.ts deleted file mode 100644 index e4b4f9377..000000000 --- a/lib/types/frozen-validation-instance.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { CustomCheck } from './custom-check' -import { Dictionary } from './dictionary' -import { KeyOrPos } from './key-or-pos' - -export interface FrozenValidationInstance { - implied: Dictionary - checks: CustomCheck[] - conflicting: Dictionary<(string | undefined)[]> -} diff --git a/lib/types/index.ts b/lib/types/index.ts deleted file mode 100644 index b09b7f1ce..000000000 --- a/lib/types/index.ts +++ /dev/null @@ -1,20 +0,0 @@ -export * from './command-builder' -export * from './command-handler' -export * from './command-instance' -export * from './completion-function' -export * from './completion-instance' -export * from './custom-check' -export * from './context' -export * from './dictionary' -export * from './electron-process' -export * from './failure-function' -export * from './frozen-usage-instance' -export * from './frozen-validation-instance' -export * from './key-or-pos' -export * from './logger-instance' -export * from './options' -export * from './parsed-command' -export * from './parser-configuration' -export * from './usage-instance' -export * from './validation-instance' -export * from './yargs-instance' diff --git a/lib/types/key-or-pos.ts b/lib/types/key-or-pos.ts deleted file mode 100644 index dbfe30146..000000000 --- a/lib/types/key-or-pos.ts +++ /dev/null @@ -1 +0,0 @@ -export type KeyOrPos = string | number | undefined diff --git a/lib/types/logger-instance.ts b/lib/types/logger-instance.ts deleted file mode 100644 index 223c1a0c2..000000000 --- a/lib/types/logger-instance.ts +++ /dev/null @@ -1 +0,0 @@ -export type LoggerInstance = Pick diff --git a/lib/types/options.ts b/lib/types/options.ts deleted file mode 100644 index 6236da7d9..000000000 --- a/lib/types/options.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Dictionary } from './dictionary' -import { ParserConfiguration } from './parser-configuration' - -export interface Options { - array: string[] - alias: Dictionary - boolean: string[] - choices: Dictionary - configuration: ParserConfiguration - count: string[] - default: Dictionary - defaultDescription: Dictionary - hiddenOptions: string[] - /** Manually set keys */ - key: Dictionary - normalize: string[] - number: string[] - showHiddenOpt: string - string: string[] -} diff --git a/lib/types/parsed-command.ts b/lib/types/parsed-command.ts deleted file mode 100644 index 16b02950f..000000000 --- a/lib/types/parsed-command.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Positional } from './positional' - -export interface ParsedCommand { - cmd: string - demanded: Positional[] - optional: Positional[] -} diff --git a/lib/types/parser-configuration.ts b/lib/types/parser-configuration.ts deleted file mode 100644 index 61dae3608..000000000 --- a/lib/types/parser-configuration.ts +++ /dev/null @@ -1,6 +0,0 @@ -import yargsParser = require('yargs-parser') - -export interface ParserConfiguration extends yargsParser.Configuration { - /** Should command be sorted in help */ - 'sort-commands': boolean -} diff --git a/lib/types/positional.ts b/lib/types/positional.ts deleted file mode 100644 index 6fd563026..000000000 --- a/lib/types/positional.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface Positional { - cmd: string[] - variadic: boolean -} diff --git a/lib/types/usage-instance.ts b/lib/types/usage-instance.ts deleted file mode 100644 index c2cbf4136..000000000 --- a/lib/types/usage-instance.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Dictionary } from './dictionary' -import { FailureFunction } from './failure-function' -import { YError } from '../yerror' - -/** Instance of the usage module. */ -export interface UsageInstance { - cacheHelpMessage (): void - clearCachedHelpMessage (): void - command (cmd: string, description: string | undefined, isDefault: boolean, aliases: string[], deprecated: boolean): void - deferY18nLookup (str: string): string - describe (key: string, desc?: string): void - describe (keys: Dictionary): void - epilog (msg: string): void - example (cmd: string, description?: string): void - fail (msg?: string | null, err?: YError | string): void - failFn (f: FailureFunction): void - freeze (): void - functionDescription (fn: { name?: string }): string - getCommands (): [string, string, boolean, string[], boolean][] - getDescriptions (): Dictionary - getPositionalGroupName (): string - getUsage (): [string, string][] - getUsageDisabled (): boolean - help (): string - reset (localLookup: Dictionary): UsageInstance - showHelp (level: 'error' | 'log'): void - showHelp (level: (message: string) => void): void - showHelpOnFail (message?: string): UsageInstance - showHelpOnFail (enabled: boolean, message: string): UsageInstance - showVersion (): void - stringifiedValues (values?: any[], separator?: string): string - unfreeze (): void - usage (msg: string | null, description?: string): UsageInstance - version (ver: any): void - wrap (cols: number): void -} diff --git a/lib/types/validation-instance.ts b/lib/types/validation-instance.ts deleted file mode 100644 index 5e206e47f..000000000 --- a/lib/types/validation-instance.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { CustomCheck } from './custom-check' -import { Dictionary } from './dictionary' -import { KeyOrPos } from './key-or-pos' -import { Arguments, DetailedArguments } from 'yargs-parser' - -/** Instance of the validation module. */ -export interface ValidationInstance { - check (f: CustomCheck['func'], global: boolean): void - conflicting (argv: Arguments): void - conflicts (key: string, value: string | string[]): void - conflicts (key: Dictionary): void - customChecks (argv: Arguments, aliases: DetailedArguments['aliases']): void - freeze (): void - getConflicting (): Dictionary<(string | undefined)[]> - getImplied (): Dictionary - implications (argv: Arguments): void - implies (key: string, value: KeyOrPos | KeyOrPos[]): void - implies (key: Dictionary): void - isValidAndSomeAliasIsNotNew (key: string, aliases: DetailedArguments['aliases']): boolean - limitedChoices (argv: Arguments): void - nonOptionCount (argv: Arguments): void - positionalCount (required: number, observed: number): void - recommendCommands (cmd: string, potentialCommands: string[]): void - requiredArguments (argv: Arguments): void - reset (localLookup: Dictionary): ValidationInstance - unfreeze (): void - unknownArguments (argv: Arguments, aliases: DetailedArguments['aliases'], positionalMap: Dictionary): void - unknownCommands (argv: Arguments): boolean -} diff --git a/lib/types/set-blocking.d.ts b/lib/typings/set-blocking.d.ts similarity index 100% rename from lib/types/set-blocking.d.ts rename to lib/typings/set-blocking.d.ts diff --git a/lib/types/y18n.d.ts b/lib/typings/y18n.d.ts similarity index 100% rename from lib/types/y18n.d.ts rename to lib/typings/y18n.d.ts diff --git a/lib/usage.ts b/lib/usage.ts index a9e38b756..d7876e6a9 100644 --- a/lib/usage.ts +++ b/lib/usage.ts @@ -1,8 +1,9 @@ // this file handles outputting usage instructions, // failures, etc. keeps logging in one place. +import { Dictionary } from './common-types' import { objFilter } from './obj-filter' import * as path from 'path' -import { FailureFunction, UsageInstance, YargsInstance, Dictionary, FrozenUsageInstance } from './types' +import { YargsInstance } from './yargs-types' import { YError } from './yerror' import decamelize = require('decamelize') import setBlocking = require('set-blocking') @@ -578,3 +579,51 @@ export function usage (yargs: YargsInstance, y18n: Y18N) { return self } + +/** Instance of the usage module. */ +export interface UsageInstance { + cacheHelpMessage(): void + clearCachedHelpMessage(): void + command(cmd: string, description: string | undefined, isDefault: boolean, aliases: string[], deprecated: boolean): void + deferY18nLookup(str: string): string + describe(key: string, desc?: string): void + describe(keys: Dictionary): void + epilog(msg: string): void + example(cmd: string, description?: string): void + fail(msg?: string | null, err?: YError | string): void + failFn(f: FailureFunction): void + freeze(): void + functionDescription(fn: { name?: string }): string + getCommands(): [string, string, boolean, string[], boolean][] + getDescriptions(): Dictionary + getPositionalGroupName(): string + getUsage(): [string, string][] + getUsageDisabled(): boolean + help(): string + reset(localLookup: Dictionary): UsageInstance + showHelp(level: 'error' | 'log'): void + showHelp(level: (message: string) => void): void + showHelpOnFail(message?: string): UsageInstance + showHelpOnFail(enabled: boolean, message: string): UsageInstance + showVersion(): void + stringifiedValues(values?: any[], separator?: string): string + unfreeze(): void + usage(msg: string | null, description?: string): UsageInstance + version(ver: any): void + wrap(cols: number): void +} + +interface FailureFunction { + (msg: string | undefined | null, err: YError | string | undefined, usage: UsageInstance): void +} + +export interface FrozenUsageInstance { + failMessage: string | undefined | null + failureOutput: boolean + usages: [string, string][] + usageDisabled: boolean + epilogs: string[] + examples: [string, string][] + commands: [string, string, boolean, string[], boolean][] + descriptions: Dictionary +} diff --git a/lib/validation.ts b/lib/validation.ts index 7aa6c47da..8277e4374 100644 --- a/lib/validation.ts +++ b/lib/validation.ts @@ -1,8 +1,10 @@ import { argsert } from './argsert' +import { Dictionary } from './common-types' import { levenshtein as distance } from './levenshtein' import { objFilter } from './obj-filter' -import { YargsInstance, UsageInstance, ValidationInstance, Dictionary, CustomCheck, KeyOrPos, FrozenValidationInstance } from './types' -import { Arguments } from 'yargs-parser' +import { UsageInstance } from './usage' +import { YargsInstance } from './yargs-types' +import { Arguments, DetailedArguments } from 'yargs-parser' import Y18N = require('y18n') const specialKeys = ['$0', '--', '_'] @@ -401,3 +403,41 @@ export function validation (yargs: YargsInstance, usage: UsageInstance, y18n: Y1 return self } + +/** Instance of the validation module. */ +interface ValidationInstance { + check(f: CustomCheck['func'], global: boolean): void + conflicting(argv: Arguments): void + conflicts(key: string, value: string | string[]): void + conflicts(key: Dictionary): void + customChecks(argv: Arguments, aliases: DetailedArguments['aliases']): void + freeze(): void + getConflicting(): Dictionary<(string | undefined)[]> + getImplied(): Dictionary + implications(argv: Arguments): void + implies(key: string, value: KeyOrPos | KeyOrPos[]): void + implies(key: Dictionary): void + isValidAndSomeAliasIsNotNew(key: string, aliases: DetailedArguments['aliases']): boolean + limitedChoices(argv: Arguments): void + nonOptionCount(argv: Arguments): void + positionalCount(required: number, observed: number): void + recommendCommands(cmd: string, potentialCommands: string[]): void + requiredArguments(argv: Arguments): void + reset(localLookup: Dictionary): ValidationInstance + unfreeze(): void + unknownArguments(argv: Arguments, aliases: DetailedArguments['aliases'], positionalMap: Dictionary): void + unknownCommands(argv: Arguments): boolean +} + +interface CustomCheck { + func: (argv: Arguments, aliases: DetailedArguments['aliases']) => any + global: boolean +} + +interface FrozenValidationInstance { + implied: Dictionary + checks: CustomCheck[] + conflicting: Dictionary<(string | undefined)[]> +} + +type KeyOrPos = string | number | undefined diff --git a/lib/types/yargs-instance.ts b/lib/yargs-types.ts similarity index 58% rename from lib/types/yargs-instance.ts rename to lib/yargs-types.ts index d79d0f81d..32a17a832 100644 --- a/lib/types/yargs-instance.ts +++ b/lib/yargs-types.ts @@ -1,11 +1,7 @@ -import { CommandInstance } from './command-instance' -import { Context } from './context' -import { Dictionary } from './dictionary' -import { LoggerInstance } from './logger-instance' -import { Options } from './options' -import { ParserConfiguration } from './parser-configuration' -import { YError } from '../yerror' -import { Arguments, DetailedArguments } from 'yargs-parser' +import { CommandInstance } from './command-types' +import { Dictionary } from './common-types' +import { Arguments, DetailedArguments, Configuration } from 'yargs-parser' +import { YError } from './yerror' /** Instance of the yargs module. */ export interface YargsInstance { @@ -43,3 +39,33 @@ export interface YargsInstance { showHelp (level: string): YargsInstance string (key: string): YargsInstance } + +/** Yargs' context. */ +interface Context { + commands: string[] +} + +type LoggerInstance = Pick + +interface Options { + array: string[] + alias: Dictionary + boolean: string[] + choices: Dictionary + configuration: ParserConfiguration + count: string[] + default: Dictionary + defaultDescription: Dictionary + hiddenOptions: string[] + /** Manually set keys */ + key: Dictionary + normalize: string[] + number: string[] + showHiddenOpt: string + string: string[] +} + +interface ParserConfiguration extends Configuration { + /** Should command be sorted in help */ + 'sort-commands': boolean +}