Skip to content

Commit f78d2b9

Browse files
authored
refactor(ts)!: move index.js to TypeScript (#292)
BREAKING CHANGE: projects using `@types/yargs-parser` may see variations in type definitions.
1 parent 5634c67 commit f78d2b9

File tree

7 files changed

+52
-42
lines changed

7 files changed

+52
-42
lines changed

index.js

-14
This file was deleted.

lib/index.ts renamed to index.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path'
22
import * as util from 'util'
3-
import { tokenizeArgString } from './tokenize-arg-string'
3+
import { tokenizeArgString } from './lib/tokenize-arg-string'
44
import type {
55
ArgsInput,
66
Arguments,
@@ -22,14 +22,23 @@ import type {
2222
Options,
2323
OptionsDefault,
2424
Parser
25-
} from './yargs-parser-types'
26-
import type { Dictionary, ValueOf } from './common-types'
25+
} from './lib/yargs-parser-types'
26+
import type { Dictionary, ValueOf } from './lib/common-types'
27+
28+
// See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our
29+
// version support policy. The YARGS_MIN_NODE_VERSION is used for testing only.
30+
const minNodeVersion = (process && process.env && process.env.YARGS_MIN_NODE_VERSION)
31+
? Number(process.env.YARGS_MIN_NODE_VERSION) : 10
32+
if (process && process.version) {
33+
const major = Number(process.version.match(/v([^.]+)/)![1])
34+
if (major < minNodeVersion) {
35+
throw Error(`yargs parser supports a minimum Node.js version of ${minNodeVersion}. Read our version support policy: https://github.com/yargs/yargs-parser#supported-nodejs-versions`)
36+
}
37+
}
2738
import camelCase = require('camelcase')
2839
import decamelize = require('decamelize')
2940

30-
export type { Arguments, DetailedArguments, Configuration, Options, Parser }
31-
32-
function parse (argsInput: ArgsInput, options?: Partial<Options>): DetailedArguments {
41+
function parse (argsInput: ArgsInput, options?: Options): DetailedArguments {
3342
const opts: Partial<Options> = Object.assign({
3443
alias: undefined,
3544
array: undefined,
@@ -1100,15 +1109,15 @@ function sanitizeKey (key: string): string {
11001109
return key
11011110
}
11021111

1103-
const yargsParser: Parser = function Parser (args: ArgsInput, opts?: Partial<Options>): Arguments {
1112+
const yargsParser: Parser = function Parser (args: ArgsInput, opts?: Options): Arguments {
11041113
const result = parse(args.slice(), opts)
11051114
return result.argv
11061115
}
11071116

11081117
// parse arguments and return detailed
11091118
// meta information, aliases, etc.
1110-
yargsParser.detailed = function (args: ArgsInput, opts?: Partial<Options>): DetailedArguments {
1119+
yargsParser.detailed = function (args: ArgsInput, opts?: Options): DetailedArguments {
11111120
return parse(args.slice(), opts)
11121121
}
11131122

1114-
export { yargsParser }
1123+
export = yargsParser

lib/yargs-parser-types.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -73,44 +73,44 @@ export type ConfigCallback = (configPath: string) => { [key: string]: any } | Er
7373

7474
export interface Options {
7575
/** An object representing the set of aliases for a key: `{ alias: { foo: ['f']} }`. */
76-
alias: Dictionary<string | string[]>;
76+
alias?: Dictionary<string | string[]>;
7777
/**
7878
* Indicate that keys should be parsed as an array: `{ array: ['foo', 'bar'] }`.
7979
* Indicate that keys should be parsed as an array and coerced to booleans / numbers:
8080
* { array: [ { key: 'foo', boolean: true }, {key: 'bar', number: true} ] }`.
8181
*/
82-
array: ArrayOption | ArrayOption[];
82+
array?: ArrayOption | ArrayOption[];
8383
/** Arguments should be parsed as booleans: `{ boolean: ['x', 'y'] }`. */
84-
boolean: string | string[];
84+
boolean?: string | string[];
8585
/** Indicate a key that represents a path to a configuration file (this file will be loaded and parsed). */
86-
config: string | string[] | Dictionary<boolean | ConfigCallback>;
86+
config?: string | string[] | Dictionary<boolean | ConfigCallback>;
8787
/** configuration objects to parse, their properties will be set as arguments */
88-
configObjects: Dictionary<any>[];
88+
configObjects?: Dictionary<any>[];
8989
/** Provide configuration options to the yargs-parser. */
90-
configuration: Partial<Configuration>;
90+
configuration?: Partial<Configuration>;
9191
/**
9292
* Provide a custom synchronous function that returns a coerced value from the argument provided (or throws an error), e.g.
9393
* `{ coerce: { foo: function (arg) { return modifiedArg } } }`.
9494
*/
95-
coerce: Dictionary<CoerceCallback>;
95+
coerce?: Dictionary<CoerceCallback>;
9696
/** Indicate a key that should be used as a counter, e.g., `-vvv = {v: 3}`. */
97-
count: string | string[];
97+
count?: string | string[];
9898
/** Provide default values for keys: `{ default: { x: 33, y: 'hello world!' } }`. */
99-
default: Dictionary<any>;
99+
default?: Dictionary<any>;
100100
/** Environment variables (`process.env`) with the prefix provided should be parsed. */
101-
envPrefix: string;
101+
envPrefix?: string;
102102
/** Specify that a key requires n arguments: `{ narg: {x: 2} }`. */
103-
narg: Dictionary<number>;
103+
narg?: Dictionary<number>;
104104
/** `path.normalize()` will be applied to values set to this key. */
105-
normalize: string | string[];
105+
normalize?: string | string[];
106106
/** Keys should be treated as strings (even if they resemble a number `-x 33`). */
107-
string: string | string[];
107+
string?: string | string[];
108108
/** Keys should be treated as numbers. */
109-
number: string | string[];
109+
number?: string | string[];
110110
/** i18n handler, defaults to util.format */
111-
__: (format: any, ...param: any[]) => string;
111+
__?: (format: any, ...param: any[]) => string;
112112
/** alias lookup table defaults */
113-
key: Dictionary<any>;
113+
key?: Dictionary<any>;
114114
}
115115

116116
export type OptionsDefault = ValueOf<Pick<Required<Options>, 'default'>>;

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
"name": "yargs-parser",
33
"version": "18.1.3",
44
"description": "the mighty option parser used by yargs",
5-
"main": "index.js",
5+
"main": "build/index.js",
66
"scripts": {
7-
"fix": "standardx --fix && standardx --fix **/*.ts",
7+
"fix": "standardx --fix ./*.ts && standardx --fix **/*.ts",
88
"pretest": "npm run compile -- -p tsconfig.test.json",
99
"test": "c8 --reporter=text --reporter=html mocha test/*.js",
1010
"posttest": "npm run check",
1111
"coverage": "c8 report --check-coverage",
12-
"check": "standardx && standardx **/*.ts",
12+
"check": "standardx ./*.ts && standardx **/*.ts",
1313
"compile": "rimraf build && tsc",
1414
"prepare": "npm run compile"
1515
},

test/types.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* global describe, it */
2+
3+
import * as yargsParser from '../'
4+
import * as assert from 'assert'
5+
6+
describe('types', function () {
7+
it('allows a partial options object to be provided', () => {
8+
const argv = yargsParser('--foo 99', {
9+
string: 'foo'
10+
})
11+
assert.strictEqual(argv.foo, '99')
12+
})
13+
})

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"sourceMap": false
77
},
88
"include": [
9+
"./*.ts",
910
"lib/**/*.ts"
1011
]
1112
}

tsconfig.test.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"sourceMap": true
55
},
66
"include": [
7+
"./*.ts",
78
"lib/**/*.ts",
89
"test/**/*.ts"
910
]

0 commit comments

Comments
 (0)