diff --git a/index.js b/index.js deleted file mode 100644 index d6e1bb04..00000000 --- a/index.js +++ /dev/null @@ -1,14 +0,0 @@ -const { yargsParser } = require('./build/lib') - -// See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our -// version support policy. The YARGS_MIN_NODE_VERSION is used for testing only. -const minNodeVersion = (process && process.env && process.env.YARGS_MIN_NODE_VERSION) - ? Number(process.env.YARGS_MIN_NODE_VERSION) : 10 -if (process && process.version) { - const major = Number(process.version.match(/v([^.]+)/)[1]) - if (major < minNodeVersion) { - 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`) - } -} - -module.exports = yargsParser diff --git a/lib/index.ts b/index.ts similarity index 97% rename from lib/index.ts rename to index.ts index 4166b3b2..558dcf71 100644 --- a/lib/index.ts +++ b/index.ts @@ -1,6 +1,6 @@ import * as path from 'path' import * as util from 'util' -import { tokenizeArgString } from './tokenize-arg-string' +import { tokenizeArgString } from './lib/tokenize-arg-string' import type { ArgsInput, Arguments, @@ -22,14 +22,23 @@ import type { Options, OptionsDefault, Parser -} from './yargs-parser-types' -import type { Dictionary, ValueOf } from './common-types' +} from './lib/yargs-parser-types' +import type { Dictionary, ValueOf } from './lib/common-types' + +// See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our +// version support policy. The YARGS_MIN_NODE_VERSION is used for testing only. +const minNodeVersion = (process && process.env && process.env.YARGS_MIN_NODE_VERSION) + ? Number(process.env.YARGS_MIN_NODE_VERSION) : 10 +if (process && process.version) { + const major = Number(process.version.match(/v([^.]+)/)![1]) + if (major < minNodeVersion) { + 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`) + } +} import camelCase = require('camelcase') import decamelize = require('decamelize') -export type { Arguments, DetailedArguments, Configuration, Options, Parser } - -function parse (argsInput: ArgsInput, options?: Partial): DetailedArguments { +function parse (argsInput: ArgsInput, options?: Options): DetailedArguments { const opts: Partial = Object.assign({ alias: undefined, array: undefined, @@ -1100,15 +1109,15 @@ function sanitizeKey (key: string): string { return key } -const yargsParser: Parser = function Parser (args: ArgsInput, opts?: Partial): Arguments { +const yargsParser: Parser = function Parser (args: ArgsInput, opts?: Options): Arguments { const result = parse(args.slice(), opts) return result.argv } // parse arguments and return detailed // meta information, aliases, etc. -yargsParser.detailed = function (args: ArgsInput, opts?: Partial): DetailedArguments { +yargsParser.detailed = function (args: ArgsInput, opts?: Options): DetailedArguments { return parse(args.slice(), opts) } -export { yargsParser } +export = yargsParser diff --git a/lib/yargs-parser-types.ts b/lib/yargs-parser-types.ts index 6e218948..8edf6837 100644 --- a/lib/yargs-parser-types.ts +++ b/lib/yargs-parser-types.ts @@ -73,44 +73,44 @@ export type ConfigCallback = (configPath: string) => { [key: string]: any } | Er export interface Options { /** An object representing the set of aliases for a key: `{ alias: { foo: ['f']} }`. */ - alias: Dictionary; + alias?: Dictionary; /** * Indicate that keys should be parsed as an array: `{ array: ['foo', 'bar'] }`. * Indicate that keys should be parsed as an array and coerced to booleans / numbers: * { array: [ { key: 'foo', boolean: true }, {key: 'bar', number: true} ] }`. */ - array: ArrayOption | ArrayOption[]; + array?: ArrayOption | ArrayOption[]; /** Arguments should be parsed as booleans: `{ boolean: ['x', 'y'] }`. */ - boolean: string | string[]; + boolean?: string | string[]; /** Indicate a key that represents a path to a configuration file (this file will be loaded and parsed). */ - config: string | string[] | Dictionary; + config?: string | string[] | Dictionary; /** configuration objects to parse, their properties will be set as arguments */ - configObjects: Dictionary[]; + configObjects?: Dictionary[]; /** Provide configuration options to the yargs-parser. */ - configuration: Partial; + configuration?: Partial; /** * Provide a custom synchronous function that returns a coerced value from the argument provided (or throws an error), e.g. * `{ coerce: { foo: function (arg) { return modifiedArg } } }`. */ - coerce: Dictionary; + coerce?: Dictionary; /** Indicate a key that should be used as a counter, e.g., `-vvv = {v: 3}`. */ - count: string | string[]; + count?: string | string[]; /** Provide default values for keys: `{ default: { x: 33, y: 'hello world!' } }`. */ - default: Dictionary; + default?: Dictionary; /** Environment variables (`process.env`) with the prefix provided should be parsed. */ - envPrefix: string; + envPrefix?: string; /** Specify that a key requires n arguments: `{ narg: {x: 2} }`. */ - narg: Dictionary; + narg?: Dictionary; /** `path.normalize()` will be applied to values set to this key. */ - normalize: string | string[]; + normalize?: string | string[]; /** Keys should be treated as strings (even if they resemble a number `-x 33`). */ - string: string | string[]; + string?: string | string[]; /** Keys should be treated as numbers. */ - number: string | string[]; + number?: string | string[]; /** i18n handler, defaults to util.format */ - __: (format: any, ...param: any[]) => string; + __?: (format: any, ...param: any[]) => string; /** alias lookup table defaults */ - key: Dictionary; + key?: Dictionary; } export type OptionsDefault = ValueOf, 'default'>>; diff --git a/package.json b/package.json index 08c358c5..7dd03ccc 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,14 @@ "name": "yargs-parser", "version": "18.1.3", "description": "the mighty option parser used by yargs", - "main": "index.js", + "main": "build/index.js", "scripts": { - "fix": "standardx --fix && standardx --fix **/*.ts", + "fix": "standardx --fix ./*.ts && standardx --fix **/*.ts", "pretest": "npm run compile -- -p tsconfig.test.json", "test": "c8 --reporter=text --reporter=html mocha test/*.js", "posttest": "npm run check", "coverage": "c8 report --check-coverage", - "check": "standardx && standardx **/*.ts", + "check": "standardx ./*.ts && standardx **/*.ts", "compile": "rimraf build && tsc", "prepare": "npm run compile" }, diff --git a/test/types.ts b/test/types.ts new file mode 100644 index 00000000..e29ff542 --- /dev/null +++ b/test/types.ts @@ -0,0 +1,13 @@ +/* global describe, it */ + +import * as yargsParser from '../' +import * as assert from 'assert' + +describe('types', function () { + it('allows a partial options object to be provided', () => { + const argv = yargsParser('--foo 99', { + string: 'foo' + }) + assert.strictEqual(argv.foo, '99') + }) +}) diff --git a/tsconfig.json b/tsconfig.json index c28e1966..8e20a300 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ "sourceMap": false }, "include": [ + "./*.ts", "lib/**/*.ts" ] } diff --git a/tsconfig.test.json b/tsconfig.test.json index 75a19fe0..62fec111 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -4,6 +4,7 @@ "sourceMap": true }, "include": [ + "./*.ts", "lib/**/*.ts", "test/**/*.ts" ]