Skip to content

Commit

Permalink
refactor(ts): group type definitions and helpers (#1632)
Browse files Browse the repository at this point in the history
* Group all type definitions and helpers in using modules
* Move .d.ts to typings directory
* Get rid of types directory
  • Loading branch information
mleguen committed Apr 30, 2020
1 parent 027a636 commit 522b019
Show file tree
Hide file tree
Showing 36 changed files with 198 additions and 251 deletions.
2 changes: 1 addition & 1 deletion 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[] = []
Expand Down
3 changes: 1 addition & 2 deletions 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
Expand Down
23 changes: 23 additions & 0 deletions 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<CommandHandler>
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'
}
File renamed without changes.
38 changes: 28 additions & 10 deletions 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.
Expand Down Expand Up @@ -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<string[]>
}

interface AsyncCompletionFunction {
(current: string, argv: Arguments, done: (completions: string[]) => any): any
}

function isSyncCompletionFunction (completionFunction: CompletionFunction): completionFunction is SyncCompletionFunction {
return completionFunction.length < 3
}
2 changes: 1 addition & 1 deletion lib/obj-filter.ts
@@ -1,4 +1,4 @@
import { Dictionary } from './types'
import { Dictionary } from './common-types'

export function objFilter<T = any> (original: Dictionary<T>, filter: (k: string, v: T) => boolean = () => true) {
const obj: Dictionary<T> = {}
Expand Down
13 changes: 11 additions & 2 deletions 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+(?![^[]*]|[^<]*>)/)
Expand Down Expand Up @@ -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
}
9 changes: 7 additions & 2 deletions 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
Expand Down Expand Up @@ -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
}
}
5 changes: 0 additions & 5 deletions lib/type-helpers/command-builder.ts

This file was deleted.

5 changes: 0 additions & 5 deletions lib/type-helpers/completion-function.ts

This file was deleted.

2 changes: 0 additions & 2 deletions lib/type-helpers/index.ts

This file was deleted.

8 changes: 0 additions & 8 deletions lib/types/command-builder.ts

This file was deleted.

5 changes: 0 additions & 5 deletions lib/types/command-handler.ts

This file was deleted.

8 changes: 0 additions & 8 deletions lib/types/command-instance.ts

This file was deleted.

11 changes: 0 additions & 11 deletions lib/types/completion-function.ts

This file was deleted.

11 changes: 0 additions & 11 deletions lib/types/completion-instance.ts

This file was deleted.

4 changes: 0 additions & 4 deletions lib/types/context.ts

This file was deleted.

6 changes: 0 additions & 6 deletions lib/types/custom-check.ts

This file was deleted.

6 changes: 0 additions & 6 deletions lib/types/electron-process.ts

This file was deleted.

6 changes: 0 additions & 6 deletions lib/types/failure-function.ts

This file was deleted.

12 changes: 0 additions & 12 deletions lib/types/frozen-usage-instance.ts

This file was deleted.

9 changes: 0 additions & 9 deletions lib/types/frozen-validation-instance.ts

This file was deleted.

20 changes: 0 additions & 20 deletions lib/types/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/types/key-or-pos.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/types/logger-instance.ts

This file was deleted.

20 changes: 0 additions & 20 deletions lib/types/options.ts

This file was deleted.

7 changes: 0 additions & 7 deletions lib/types/parsed-command.ts

This file was deleted.

6 changes: 0 additions & 6 deletions lib/types/parser-configuration.ts

This file was deleted.

4 changes: 0 additions & 4 deletions lib/types/positional.ts

This file was deleted.

36 changes: 0 additions & 36 deletions lib/types/usage-instance.ts

This file was deleted.

29 changes: 0 additions & 29 deletions lib/types/validation-instance.ts

This file was deleted.

File renamed without changes.
File renamed without changes.

0 comments on commit 522b019

Please sign in to comment.