Skip to content

Commit 735c6b3

Browse files
chore: v20.2.6 release
1 parent 686ec15 commit 735c6b3

12 files changed

+2529
-0
lines changed

build/index.cjs

Lines changed: 1032 additions & 0 deletions
Large diffs are not rendered by default.

build/index.cjs.d.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* An object whose all properties have the same type, where each key is a string.
3+
*/
4+
interface Dictionary<T = any> {
5+
[key: string]: T;
6+
}
7+
type ArgsInput = string | any[];
8+
type ArgsOutput = (string | number)[];
9+
interface Arguments {
10+
/** Non-option arguments */
11+
_: ArgsOutput;
12+
/** Arguments after the end-of-options flag `--` */
13+
"--"?: ArgsOutput;
14+
/** All remaining options */
15+
[argName: string]: any;
16+
}
17+
interface DetailedArguments {
18+
/** An object representing the parsed value of `args` */
19+
argv: Arguments;
20+
/** Populated with an error object if an exception occurred during parsing. */
21+
error: Error | null;
22+
/** The inferred list of aliases built by combining lists in opts.alias. */
23+
aliases: Dictionary<string[]>;
24+
/** Any new aliases added via camel-case expansion. */
25+
newAliases: Dictionary<boolean>;
26+
/** Any new argument created by opts.default, no aliases included. */
27+
defaulted: Dictionary<boolean>;
28+
/** The configuration loaded from the yargs stanza in package.json. */
29+
configuration: Configuration;
30+
}
31+
interface Configuration {
32+
/** Should variables prefixed with --no be treated as negations? Default is `true` */
33+
"boolean-negation": boolean;
34+
/** Should hyphenated arguments be expanded into camel-case aliases? Default is `true` */
35+
"camel-case-expansion": boolean;
36+
/** Should arrays be combined when provided by both command line arguments and a configuration file? Default is `false` */
37+
"combine-arrays": boolean;
38+
/** Should keys that contain `.` be treated as objects? Default is `true` */
39+
"dot-notation": boolean;
40+
/** Should arguments be coerced into an array when duplicated? Default is `true` */
41+
"duplicate-arguments-array": boolean;
42+
/** Should array arguments be coerced into a single array when duplicated? Default is `true` */
43+
"flatten-duplicate-arrays": boolean;
44+
/** Should arrays consume more than one positional argument following their flag? Default is `true` */
45+
"greedy-arrays": boolean;
46+
/** Should parsing stop at the first text argument? This is similar to how e.g. ssh parses its command line. Default is `false` */
47+
"halt-at-non-option": boolean;
48+
/** Should nargs consume dash options as well as positional arguments? Default is `false` */
49+
"nargs-eats-options": boolean;
50+
/** The prefix to use for negated boolean variables. Default is `'no-'` */
51+
"negation-prefix": string;
52+
/** Should positional values that look like numbers be parsed? Default is `true` */
53+
"parse-positional-numbers": boolean;
54+
/** Should keys that look like numbers be treated as such? Default is `true` */
55+
"parse-numbers": boolean;
56+
/** Should unparsed flags be stored in -- or _? Default is `false` */
57+
"populate--": boolean;
58+
/** Should a placeholder be added for keys not set via the corresponding CLI argument? Default is `false` */
59+
"set-placeholder-key": boolean;
60+
/** Should a group of short-options be treated as boolean flags? Default is `true` */
61+
"short-option-groups": boolean;
62+
/** Should aliases be removed before returning results? Default is `false` */
63+
"strip-aliased": boolean;
64+
/** Should dashed keys be removed before returning results? This option has no effect if camel-case-expansion is disabled. Default is `false` */
65+
"strip-dashed": boolean;
66+
/** Should unknown options be treated like regular arguments? An unknown option is one that is not configured in opts. Default is `false` */
67+
"unknown-options-as-args": boolean;
68+
}
69+
type ArrayOption = string | {
70+
key: string;
71+
boolean?: boolean;
72+
string?: boolean;
73+
number?: boolean;
74+
integer?: boolean;
75+
};
76+
type CoerceCallback = (arg: any) => any;
77+
type ConfigCallback = (configPath: string) => {
78+
[key: string]: any;
79+
} | Error;
80+
interface Options {
81+
/** An object representing the set of aliases for a key: `{ alias: { foo: ['f']} }`. */
82+
alias: Dictionary<string | string[]>;
83+
/**
84+
* Indicate that keys should be parsed as an array: `{ array: ['foo', 'bar'] }`.
85+
* Indicate that keys should be parsed as an array and coerced to booleans / numbers:
86+
* { array: [ { key: 'foo', boolean: true }, {key: 'bar', number: true} ] }`.
87+
*/
88+
array: ArrayOption | ArrayOption[];
89+
/** Arguments should be parsed as booleans: `{ boolean: ['x', 'y'] }`. */
90+
boolean: string | string[];
91+
/** Indicate a key that represents a path to a configuration file (this file will be loaded and parsed). */
92+
config: string | string[] | Dictionary<boolean | ConfigCallback>;
93+
/** configuration objects to parse, their properties will be set as arguments */
94+
configObjects: Dictionary<any>[];
95+
/** Provide configuration options to the yargs-parser. */
96+
configuration: Partial<Configuration>;
97+
/**
98+
* Provide a custom synchronous function that returns a coerced value from the argument provided (or throws an error), e.g.
99+
* `{ coerce: { foo: function (arg) { return modifiedArg } } }`.
100+
*/
101+
coerce: Dictionary<CoerceCallback>;
102+
/** Indicate a key that should be used as a counter, e.g., `-vvv = {v: 3}`. */
103+
count: string | string[];
104+
/** Provide default values for keys: `{ default: { x: 33, y: 'hello world!' } }`. */
105+
default: Dictionary<any>;
106+
/** Environment variables (`process.env`) with the prefix provided should be parsed. */
107+
envPrefix?: string;
108+
/** Specify that a key requires n arguments: `{ narg: {x: 2} }`. */
109+
narg: Dictionary<number>;
110+
/** `path.normalize()` will be applied to values set to this key. */
111+
normalize: string | string[];
112+
/** Keys should be treated as strings (even if they resemble a number `-x 33`). */
113+
string: string | string[];
114+
/** Keys should be treated as numbers. */
115+
number: string | string[];
116+
/** i18n handler, defaults to util.format */
117+
__: (format: any, ...param: any[]) => string;
118+
/** alias lookup table defaults */
119+
key: Dictionary<any>;
120+
}
121+
interface Parser {
122+
(args: ArgsInput, opts?: Partial<Options>): Arguments;
123+
detailed(args: ArgsInput, opts?: Partial<Options>): DetailedArguments;
124+
camelCase(str: string): string;
125+
decamelize(str: string, joinString?: string): string;
126+
looksLikeNumber(x: null | undefined | number | string): boolean;
127+
}
128+
declare const yargsParser: Parser;
129+
export = yargsParser;

build/lib/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Parser } from './yargs-parser-types.js';
2+
declare const yargsParser: Parser;
3+
export default yargsParser;

build/lib/index.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Main entrypoint for libraries using yargs-parser in Node.js
2+
// CJS and ESM environments:
3+
import { format } from 'util';
4+
import { readFileSync } from 'fs';
5+
import { normalize, resolve } from 'path';
6+
import { camelCase, decamelize, looksLikeNumber } from './string-utils.js';
7+
import { YargsParser } from './yargs-parser.js';
8+
// See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our
9+
// version support policy. The YARGS_MIN_NODE_VERSION is used for testing only.
10+
const minNodeVersion = (process && process.env && process.env.YARGS_MIN_NODE_VERSION)
11+
? Number(process.env.YARGS_MIN_NODE_VERSION)
12+
: 10;
13+
if (process && process.version) {
14+
const major = Number(process.version.match(/v([^.]+)/)[1]);
15+
if (major < minNodeVersion) {
16+
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`);
17+
}
18+
}
19+
// Creates a yargs-parser instance using Node.js standard libraries:
20+
const env = process ? process.env : {};
21+
const parser = new YargsParser({
22+
cwd: process.cwd,
23+
env: () => {
24+
return env;
25+
},
26+
format,
27+
normalize,
28+
resolve,
29+
// TODO: figure out a way to combine ESM and CJS coverage, such that
30+
// we can exercise all the lines below:
31+
require: (path) => {
32+
if (typeof require !== 'undefined') {
33+
return require(path);
34+
}
35+
else if (path.match(/\.json$/)) {
36+
return readFileSync(path, 'utf8');
37+
}
38+
else {
39+
throw Error('only .json config files are supported in ESM');
40+
}
41+
}
42+
});
43+
const yargsParser = function Parser(args, opts) {
44+
const result = parser.parse(args.slice(), opts);
45+
return result.argv;
46+
};
47+
yargsParser.detailed = function (args, opts) {
48+
return parser.parse(args.slice(), opts);
49+
};
50+
yargsParser.camelCase = camelCase;
51+
yargsParser.decamelize = decamelize;
52+
yargsParser.looksLikeNumber = looksLikeNumber;
53+
export default yargsParser;

build/lib/string-utils.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export declare function camelCase(str: string): string;
2+
export declare function decamelize(str: string, joinString?: string): string;
3+
export declare function looksLikeNumber(x: null | undefined | number | string): boolean;

build/lib/string-utils.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export function camelCase(str) {
2+
// Handle the case where an argument is provided as camel case, e.g., fooBar.
3+
// by ensuring that the string isn't already mixed case:
4+
const isCamelCase = str !== str.toLowerCase() && str !== str.toUpperCase();
5+
if (!isCamelCase) {
6+
str = str.toLocaleLowerCase();
7+
}
8+
if (str.indexOf('-') === -1 && str.indexOf('_') === -1) {
9+
return str;
10+
}
11+
else {
12+
let camelcase = '';
13+
let nextChrUpper = false;
14+
const leadingHyphens = str.match(/^-+/);
15+
for (let i = leadingHyphens ? leadingHyphens[0].length : 0; i < str.length; i++) {
16+
let chr = str.charAt(i);
17+
if (nextChrUpper) {
18+
nextChrUpper = false;
19+
chr = chr.toLocaleUpperCase();
20+
}
21+
if (i !== 0 && (chr === '-' || chr === '_')) {
22+
nextChrUpper = true;
23+
}
24+
else if (chr !== '-' && chr !== '_') {
25+
camelcase += chr;
26+
}
27+
}
28+
return camelcase;
29+
}
30+
}
31+
export function decamelize(str, joinString) {
32+
const lowercase = str.toLocaleLowerCase();
33+
joinString = joinString || '-';
34+
let notCamelcase = '';
35+
for (let i = 0; i < str.length; i++) {
36+
const chrLower = lowercase.charAt(i);
37+
const chrString = str.charAt(i);
38+
if (chrLower !== chrString && i > 0) {
39+
notCamelcase += `${joinString}${lowercase.charAt(i)}`;
40+
}
41+
else {
42+
notCamelcase += chrString;
43+
}
44+
}
45+
return notCamelcase;
46+
}
47+
export function looksLikeNumber(x) {
48+
if (x === null || x === undefined)
49+
return false;
50+
// if loaded from config, may already be a number.
51+
if (typeof x === 'number')
52+
return true;
53+
// hexadecimal.
54+
if (/^0x[0-9a-f]+$/i.test(x))
55+
return true;
56+
// don't treat 0123 as a number; as it drops the leading '0'.
57+
if (x.length > 1 && x[0] === '0')
58+
return false;
59+
return /^[-]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
60+
}

build/lib/tokenize-arg-string.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare function tokenizeArgString(argString: string | any[]): string[];

build/lib/tokenize-arg-string.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// take an un-split argv string and tokenize it.
2+
export function tokenizeArgString(argString) {
3+
if (Array.isArray(argString)) {
4+
return argString.map(e => typeof e !== 'string' ? e + '' : e);
5+
}
6+
argString = argString.trim();
7+
let i = 0;
8+
let prevC = null;
9+
let c = null;
10+
let opening = null;
11+
const args = [];
12+
for (let ii = 0; ii < argString.length; ii++) {
13+
prevC = c;
14+
c = argString.charAt(ii);
15+
// split on spaces unless we're in quotes.
16+
if (c === ' ' && !opening) {
17+
if (!(prevC === ' ')) {
18+
i++;
19+
}
20+
continue;
21+
}
22+
// don't split the string if we're in matching
23+
// opening or closing single and double quotes.
24+
if (c === opening) {
25+
opening = null;
26+
}
27+
else if ((c === "'" || c === '"') && !opening) {
28+
opening = c;
29+
}
30+
if (!args[i])
31+
args[i] = '';
32+
args[i] += c;
33+
}
34+
return args;
35+
}

0 commit comments

Comments
 (0)