Skip to content

Commit

Permalink
refactor(ts): move to TypeScript release of yargs-parser (#1696)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Jul 19, 2020
1 parent c06f886 commit f5997e8
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 150 deletions.
2 changes: 1 addition & 1 deletion lib/command.ts
Expand Up @@ -324,7 +324,7 @@ export function command (
populatePositional(maybe, argv, positionalMap)
}

argv._ = context.commands.concat(argv._)
argv._ = context.commands.concat(argv._.map(a => '' + a))

postProcessPositionals(argv, positionalMap, self.cmdToParseOptions(commandHandler.original))

Expand Down
2 changes: 1 addition & 1 deletion lib/completion.ts
Expand Up @@ -5,7 +5,7 @@ import { parseCommand } from './parse-command'
import * as path from 'path'
import { UsageInstance } from './usage'
import { YargsInstance } from './yargs'
import { Arguments, DetailedArguments } from 'yargs-parser'
import { Arguments, DetailedArguments } from 'yargs-parser/build/lib/yargs-parser-types'
import { assertNotStrictEqual } from './common-types'

// add bash completions to your
Expand Down
124 changes: 0 additions & 124 deletions lib/typings/yargs-parser.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion lib/usage.ts
Expand Up @@ -6,7 +6,7 @@ import * as path from 'path'
import { YargsInstance } from './yargs'
import { YError } from './yerror'
import { Y18N } from 'y18n'
import { DetailedArguments } from 'yargs-parser'
import { DetailedArguments } from 'yargs-parser/build/lib/yargs-parser-types'
import decamelize = require('decamelize')
import setBlocking = require('set-blocking')
import stringWidth = require('string-width')
Expand Down
10 changes: 5 additions & 5 deletions lib/validation.ts
Expand Up @@ -4,7 +4,7 @@ import { levenshtein as distance } from './levenshtein'
import { objFilter } from './obj-filter'
import { UsageInstance } from './usage'
import { YargsInstance, Arguments } from './yargs'
import { DetailedArguments } from 'yargs-parser'
import { DetailedArguments } from 'yargs-parser/build/lib/yargs-parser-types'
import { Y18N } from 'y18n'
const specialKeys = ['$0', '--', '_']

Expand Down Expand Up @@ -131,8 +131,8 @@ export function validation (yargs: YargsInstance, usage: UsageInstance, y18n: Y1

if ((currentContext.commands.length > 0) || (commandKeys.length > 0) || isDefaultCommand) {
argv._.slice(currentContext.commands.length).forEach((key) => {
if (commandKeys.indexOf(key) === -1) {
unknown.push(key)
if (commandKeys.indexOf('' + key) === -1) {
unknown.push('' + key)
}
})
}
Expand All @@ -154,8 +154,8 @@ export function validation (yargs: YargsInstance, usage: UsageInstance, y18n: Y1

if ((currentContext.commands.length > 0) || (commandKeys.length > 0)) {
argv._.slice(currentContext.commands.length).forEach((key) => {
if (commandKeys.indexOf(key) === -1) {
unknown.push(key)
if (commandKeys.indexOf('' + key) === -1) {
unknown.push('' + key)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions lib/yargs.ts
Expand Up @@ -7,7 +7,7 @@ import {
Options as ParserOptions,
ConfigCallback,
CoerceCallback
} from 'yargs-parser'
} from 'yargs-parser/build/lib/yargs-parser-types'
import { YError } from './yerror'
import { UsageInstance, FailureFunction, usage as Usage } from './usage'
import { argsert } from './argsert'
Expand Down Expand Up @@ -1230,7 +1230,7 @@ export function Yargs (processArgs: string | string[] = [], cwd = process.cwd(),
.concat(aliases[helpOpt] || [])
.filter(k => k.length > 1)
// check if help should trigger and strip it from _.
if (~helpCmds.indexOf(argv._[argv._.length - 1])) {
if (~helpCmds.indexOf('' + argv._[argv._.length - 1])) {
argv._.pop()
argv[helpOpt] = true
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -28,7 +28,7 @@
"string-width": "^4.2.0",
"which-module": "^2.0.0",
"y18n": "^4.0.0",
"yargs-parser": "^18.1.2"
"yargs-parser": "^19.0.0-beta.1"
},
"devDependencies": {
"@types/chai": "^4.2.11",
Expand Down Expand Up @@ -56,11 +56,11 @@
"yargs-test-extends": "^1.0.1"
},
"scripts": {
"fix": "standardx --fix && standardx --fix **/*.ts",
"fix": "standardx --fix ./*.ts && standardx --fix **/*.ts",
"posttest": "npm run check",
"test": "c8 mocha --require ./test/before.js --timeout=12000 --check-leaks",
"coverage": "c8 report --check-coverage",
"check": "standardx && standardx **/*.ts",
"check": "standardx ./*.ts && standardx **/*.ts",
"compile": "rimraf build && tsc",
"prepare": "npm run compile",
"pretest": "npm run compile -- -p tsconfig.test.json"
Expand Down
52 changes: 39 additions & 13 deletions yargs.d.ts
@@ -1,5 +1,31 @@
/*
* MIT License
*
* Copyright (c) 2016 Martin Poelstra, Mizunashi Mana, Jeffery Grajkowski,
* Jeff Kenney, Jimi (Dimitris) Charalampidis, Steffen Viken Valvåg,
* Emily Marigold Klassen, ExE Boss, and Aankhen.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import { YargsInstance, RebaseFunction } from './build/lib/yargs'
import { Parser, DetailedArguments, Configuration } from 'yargs-parser';
import { Parser, DetailedArguments, Configuration } from 'yargs-parser/build/lib/yargs-parser-types'

declare namespace yargs {
type BuilderCallback<T, R> = ((args: YargsInstance<T>) => PromiseLike<YargsInstance<R>>) | ((args: YargsInstance<T>) => YargsInstance<R>) | ((args: YargsInstance<T>) => void);
Expand Down Expand Up @@ -654,7 +680,7 @@ declare namespace yargs {
skipValidation?: boolean;
/** boolean, interpret option as a string, see `string()` */
string?: boolean;
type?: "array" | "count" | PositionalOptionsType;
type?: 'array' | 'count' | PositionalOptionsType;
}

interface PositionalOptions {
Expand Down Expand Up @@ -702,7 +728,7 @@ declare namespace yargs {

type InferredOptionType<O extends Options | PositionalOptions> =
O extends { default: infer D } ? D :
O extends { type: "count" } ? number :
O extends { type: 'count' } ? number :
O extends { count: true } ? number :
O extends { required: string | true } ? RequiredOptionType<O> :
O extends { require: string | true } ? RequiredOptionType<O> :
Expand All @@ -711,18 +737,18 @@ declare namespace yargs {
RequiredOptionType<O> | undefined;

type RequiredOptionType<O extends Options | PositionalOptions> =
O extends { type: "array", string: true } ? string[] :
O extends { type: "array", number: true } ? number[] :
O extends { type: "array", normalize: true } ? string[] :
O extends { type: "string", array: true } ? string[] :
O extends { type: "number", array: true } ? number[] :
O extends { type: 'array', string: true } ? string[] :
O extends { type: 'array', number: true } ? number[] :
O extends { type: 'array', normalize: true } ? string[] :
O extends { type: 'string', array: true } ? string[] :
O extends { type: 'number', array: true } ? number[] :
O extends { string: true, array: true } ? string[] :
O extends { number: true, array: true } ? number[] :
O extends { normalize: true, array: true } ? string[] :
O extends { type: "array" } ? Array<string | number> :
O extends { type: "boolean" } ? boolean :
O extends { type: "number" } ? number :
O extends { type: "string" } ? string :
O extends { type: 'array' } ? Array<string | number> :
O extends { type: 'boolean' } ? boolean :
O extends { type: 'number' } ? number :
O extends { type: 'string' } ? string :
O extends { array: true } ? Array<string | number> :
O extends { boolean: true } ? boolean :
O extends { number: true } ? number :
Expand Down Expand Up @@ -754,7 +780,7 @@ declare namespace yargs {
type PromiseCompletionFunction = (current: string, argv: any) => Promise<string[]>;
type MiddlewareFunction<T = {}> = (args: Arguments<T>) => void;
type Choices = ReadonlyArray<string | number | true | undefined>;
type PositionalOptionsType = "boolean" | "number" | "string";
type PositionalOptionsType = 'boolean' | 'number' | 'string';

// Aliases for old @types/yargs names
type Argv = YargsInstance;
Expand Down

0 comments on commit f5997e8

Please sign in to comment.