yargs 15.3.1 → 15.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/LICENSE +2 -3
  3. package/README.md +5 -5
  4. package/build/lib/apply-extends.d.ts +2 -0
  5. package/build/lib/apply-extends.js +65 -0
  6. package/build/lib/argsert.d.ts +2 -0
  7. package/build/lib/argsert.js +65 -0
  8. package/build/lib/command.d.ts +64 -0
  9. package/build/lib/command.js +416 -0
  10. package/build/lib/common-types.d.ts +36 -0
  11. package/build/lib/common-types.js +25 -0
  12. package/build/lib/completion-templates.d.ts +2 -0
  13. package/{lib → build/lib}/completion-templates.js +6 -5
  14. package/build/lib/completion.d.ts +21 -0
  15. package/build/lib/completion.js +135 -0
  16. package/build/lib/is-promise.d.ts +1 -0
  17. package/build/lib/is-promise.js +9 -0
  18. package/build/lib/levenshtein.d.ts +1 -0
  19. package/{lib → build/lib}/levenshtein.js +33 -33
  20. package/build/lib/middleware.d.ts +10 -0
  21. package/build/lib/middleware.js +57 -0
  22. package/build/lib/obj-filter.d.ts +1 -0
  23. package/build/lib/obj-filter.js +14 -0
  24. package/build/lib/parse-command.d.ts +11 -0
  25. package/build/lib/parse-command.js +36 -0
  26. package/build/lib/process-argv.d.ts +2 -0
  27. package/build/lib/process-argv.js +31 -0
  28. package/build/lib/usage.d.ts +49 -0
  29. package/build/lib/usage.js +540 -0
  30. package/build/lib/validation.d.ts +34 -0
  31. package/build/lib/validation.js +330 -0
  32. package/build/lib/yargs.d.ts +274 -0
  33. package/build/lib/yargs.js +1190 -0
  34. package/build/lib/yerror.d.ts +4 -0
  35. package/build/lib/yerror.js +11 -0
  36. package/index.js +1 -1
  37. package/locales/ja.json +6 -4
  38. package/package.json +26 -13
  39. package/yargs.js +2 -1291
  40. package/lib/apply-extends.js +0 -67
  41. package/lib/argsert.js +0 -68
  42. package/lib/command.js +0 -462
  43. package/lib/completion.js +0 -132
  44. package/lib/is-promise.js +0 -3
  45. package/lib/middleware.js +0 -64
  46. package/lib/obj-filter.js +0 -11
  47. package/lib/process-argv.js +0 -34
  48. package/lib/usage.js +0 -571
  49. package/lib/validation.js +0 -394
  50. package/lib/yerror.js +0 -11
package/CHANGELOG.md CHANGED
@@ -2,6 +2,27 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [15.4.0](https://www.github.com/yargs/yargs/compare/v15.3.1...v15.4.0) (2020-06-30)
6
+
7
+
8
+ ### Features
9
+
10
+ * adds deprecation option for commands ([027a636](https://www.github.com/yargs/yargs/commit/027a6365b737e13116811a8ef43670196e1fa00a))
11
+ * support array of examples ([#1682](https://www.github.com/yargs/yargs/issues/1682)) ([225ab82](https://www.github.com/yargs/yargs/commit/225ab8271938bed3a48d23175f3d580ce8cd1306))
12
+
13
+
14
+ ### Bug Fixes
15
+
16
+ * **docs:** describe usage of `.check()` in more detail ([932cd11](https://www.github.com/yargs/yargs/commit/932cd1177e93f5cc99edfe57a4028e30717bf8fb))
17
+ * **i18n:** Japanese translation phrasing ([#1619](https://www.github.com/yargs/yargs/issues/1619)) ([0894175](https://www.github.com/yargs/yargs/commit/089417550ef5a5b8ce3578dd2a989191300b64cd))
18
+ * **strict mode:** report default command unknown arguments ([#1626](https://www.github.com/yargs/yargs/issues/1626)) ([69f29a9](https://www.github.com/yargs/yargs/commit/69f29a9cd429d4bb99481238305390107ac75b02))
19
+ * **usage:** translate 'options' group only when displaying help ([#1600](https://www.github.com/yargs/yargs/issues/1600)) ([e60b39b](https://www.github.com/yargs/yargs/commit/e60b39b9d3a912c06db43f87c86ba894142b6c1c))
20
+
21
+
22
+ ### Reverts
23
+
24
+ * Revert "chore(deps): update dependency eslint to v7 (#1656)" (#1673) ([34949f8](https://www.github.com/yargs/yargs/commit/34949f89ee7cdf88f7b315659df4b5f62f714842)), closes [#1656](https://www.github.com/yargs/yargs/issues/1656) [#1673](https://www.github.com/yargs/yargs/issues/1673)
25
+
5
26
  ### [15.3.1](https://www.github.com/yargs/yargs/compare/v15.3.0...v15.3.1) (2020-03-16)
6
27
 
7
28
 
package/LICENSE CHANGED
@@ -1,7 +1,6 @@
1
- Copyright 2010 James Halliday (mail@substack.net)
2
- Modified work Copyright 2014 Contributors (ben@npmjs.com)
1
+ MIT License
3
2
 
4
- This project is free software released under the MIT/X11 license:
3
+ Copyright 2010 James Halliday (mail@substack.net); Modified work Copyright 2014 Contributors (ben@npmjs.com)
5
4
 
6
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
7
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -44,16 +44,16 @@ npm i yargs@next
44
44
 
45
45
  ### Simple Example
46
46
 
47
- ````javascript
47
+ ```javascript
48
48
  #!/usr/bin/env node
49
- const argv = require('yargs').argv
49
+ const {argv} = require('yargs')
50
50
 
51
51
  if (argv.ships > 3 && argv.distance < 53.5) {
52
52
  console.log('Plunder more riffiwobbles!')
53
53
  } else {
54
54
  console.log('Retreat from the xupptumblers!')
55
55
  }
56
- ````
56
+ ```
57
57
 
58
58
  ```bash
59
59
  $ ./plunder.js --ships=4 --distance=22
@@ -136,5 +136,5 @@ Having problems? want to contribute? join our [community slack](http://devtoolsc
136
136
  [slack-image]: http://devtoolscommunity.herokuapp.com/badge.svg
137
137
  [slack-url]: http://devtoolscommunity.herokuapp.com
138
138
  [type-definitions]: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/yargs
139
- [coverage-image]: https://img.shields.io/badge/dynamic/json?color=brightgreen&label=coverage&query=branches&suffix=%25&url=https%3A%2F%2Fraw.githubusercontent.com%2Fyargs%2Fyargs%2Fmaster%2F.nycrc&cacheSeconds=3600
140
- [coverage-url]: https://github.com/yargs/yargs/blob/master/.nycrc
139
+ [coverage-image]: https://img.shields.io/nycrc/yargs/yargs
140
+ [coverage-url]: https://github.com/yargs/yargs/blob/master/.nycrc
@@ -0,0 +1,2 @@
1
+ import { Dictionary } from './common-types';
2
+ export declare function applyExtends(config: Dictionary, cwd: string, mergeExtends?: boolean): Dictionary;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyExtends = void 0;
4
+ const fs = require("fs");
5
+ const path = require("path");
6
+ const yerror_1 = require("./yerror");
7
+ let previouslyVisitedConfigs = [];
8
+ function checkForCircularExtends(cfgPath) {
9
+ if (previouslyVisitedConfigs.indexOf(cfgPath) > -1) {
10
+ throw new yerror_1.YError(`Circular extended configurations: '${cfgPath}'.`);
11
+ }
12
+ }
13
+ function getPathToDefaultConfig(cwd, pathToExtend) {
14
+ return path.resolve(cwd, pathToExtend);
15
+ }
16
+ function mergeDeep(config1, config2) {
17
+ const target = {};
18
+ function isObject(obj) {
19
+ return obj && typeof obj === 'object' && !Array.isArray(obj);
20
+ }
21
+ Object.assign(target, config1);
22
+ for (const key of Object.keys(config2)) {
23
+ if (isObject(config2[key]) && isObject(target[key])) {
24
+ target[key] = mergeDeep(config1[key], config2[key]);
25
+ }
26
+ else {
27
+ target[key] = config2[key];
28
+ }
29
+ }
30
+ return target;
31
+ }
32
+ function applyExtends(config, cwd, mergeExtends = false) {
33
+ let defaultConfig = {};
34
+ if (Object.prototype.hasOwnProperty.call(config, 'extends')) {
35
+ if (typeof config.extends !== 'string')
36
+ return defaultConfig;
37
+ const isPath = /\.json|\..*rc$/.test(config.extends);
38
+ let pathToDefault = null;
39
+ if (!isPath) {
40
+ try {
41
+ pathToDefault = require.resolve(config.extends);
42
+ }
43
+ catch (err) {
44
+ // most likely this simply isn't a module.
45
+ }
46
+ }
47
+ else {
48
+ pathToDefault = getPathToDefaultConfig(cwd, config.extends);
49
+ }
50
+ // maybe the module uses key for some other reason,
51
+ // err on side of caution.
52
+ if (!pathToDefault && !isPath)
53
+ return config;
54
+ if (!pathToDefault)
55
+ throw new yerror_1.YError(`Unable to find extended config '${config.extends}' in '${cwd}'.`);
56
+ checkForCircularExtends(pathToDefault);
57
+ previouslyVisitedConfigs.push(pathToDefault);
58
+ defaultConfig = isPath ? JSON.parse(fs.readFileSync(pathToDefault, 'utf8')) : require(config.extends);
59
+ delete config.extends;
60
+ defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault), mergeExtends);
61
+ }
62
+ previouslyVisitedConfigs = [];
63
+ return mergeExtends ? mergeDeep(defaultConfig, config) : Object.assign({}, defaultConfig, config);
64
+ }
65
+ exports.applyExtends = applyExtends;
@@ -0,0 +1,2 @@
1
+ export declare function argsert(callerArguments: any[], length?: number): void;
2
+ export declare function argsert(expected: string, callerArguments: any[], length?: number): void;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.argsert = void 0;
4
+ const yerror_1 = require("./yerror");
5
+ const parse_command_1 = require("./parse-command");
6
+ const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'];
7
+ function argsert(arg1, arg2, arg3) {
8
+ function parseArgs() {
9
+ return typeof arg1 === 'object'
10
+ ? [{ demanded: [], optional: [] }, arg1, arg2]
11
+ : [parse_command_1.parseCommand(`cmd ${arg1}`), arg2, arg3];
12
+ }
13
+ // TODO: should this eventually raise an exception.
14
+ try {
15
+ // preface the argument description with "cmd", so
16
+ // that we can run it through yargs' command parser.
17
+ let position = 0;
18
+ let [parsed, callerArguments, length] = parseArgs();
19
+ const args = [].slice.call(callerArguments);
20
+ while (args.length && args[args.length - 1] === undefined)
21
+ args.pop();
22
+ length = length || args.length;
23
+ if (length < parsed.demanded.length) {
24
+ throw new yerror_1.YError(`Not enough arguments provided. Expected ${parsed.demanded.length} but received ${args.length}.`);
25
+ }
26
+ const totalCommands = parsed.demanded.length + parsed.optional.length;
27
+ if (length > totalCommands) {
28
+ throw new yerror_1.YError(`Too many arguments provided. Expected max ${totalCommands} but received ${length}.`);
29
+ }
30
+ parsed.demanded.forEach((demanded) => {
31
+ const arg = args.shift();
32
+ const observedType = guessType(arg);
33
+ const matchingTypes = demanded.cmd.filter(type => type === observedType || type === '*');
34
+ if (matchingTypes.length === 0)
35
+ argumentTypeError(observedType, demanded.cmd, position);
36
+ position += 1;
37
+ });
38
+ parsed.optional.forEach((optional) => {
39
+ if (args.length === 0)
40
+ return;
41
+ const arg = args.shift();
42
+ const observedType = guessType(arg);
43
+ const matchingTypes = optional.cmd.filter(type => type === observedType || type === '*');
44
+ if (matchingTypes.length === 0)
45
+ argumentTypeError(observedType, optional.cmd, position);
46
+ position += 1;
47
+ });
48
+ }
49
+ catch (err) {
50
+ console.warn(err.stack);
51
+ }
52
+ }
53
+ exports.argsert = argsert;
54
+ function guessType(arg) {
55
+ if (Array.isArray(arg)) {
56
+ return 'array';
57
+ }
58
+ else if (arg === null) {
59
+ return 'null';
60
+ }
61
+ return typeof arg;
62
+ }
63
+ function argumentTypeError(observedType, allowedTypes, position) {
64
+ throw new yerror_1.YError(`Invalid ${positionName[position] || 'manyith'} argument. Expected ${allowedTypes.join(' or ')} but received ${observedType}.`);
65
+ }
@@ -0,0 +1,64 @@
1
+ /// <reference types="node" />
2
+ import { Dictionary } from './common-types';
3
+ import { Middleware } from './middleware';
4
+ import { Positional } from './parse-command';
5
+ import { RequireDirectoryOptions } from 'require-directory';
6
+ import { UsageInstance } from './usage';
7
+ import { ValidationInstance } from './validation';
8
+ import { YargsInstance, Options, OptionDefinition, Context, Arguments, DetailedArguments } from './yargs';
9
+ export declare function command(yargs: YargsInstance, usage: UsageInstance, validation: ValidationInstance, globalMiddleware?: Middleware[]): CommandInstance;
10
+ /** Instance of the command module. */
11
+ export interface CommandInstance {
12
+ addDirectory(dir: string, context: Context, req: NodeRequireFunction, callerFile: string, opts?: RequireDirectoryOptions<any>): void;
13
+ addHandler(handler: CommandHandlerDefinition): void;
14
+ addHandler(cmd: string | string[], description: CommandHandler['description'], builder?: CommandBuilderDefinition | CommandBuilder, handler?: CommandHandlerCallback, commandMiddleware?: Middleware[], deprecated?: boolean): void;
15
+ cmdToParseOptions(cmdString: string): Positionals;
16
+ freeze(): void;
17
+ getCommandHandlers(): Dictionary<CommandHandler>;
18
+ getCommands(): string[];
19
+ hasDefaultCommand(): boolean;
20
+ reset(): CommandInstance;
21
+ runCommand(command: string | null, yargs: YargsInstance, parsed: DetailedArguments, commandIndex?: number): Arguments | Promise<Arguments>;
22
+ runDefaultBuilderOn(yargs: YargsInstance): void;
23
+ unfreeze(): void;
24
+ }
25
+ export interface CommandHandlerDefinition extends Partial<Pick<CommandHandler, 'deprecated' | 'description' | 'handler' | 'middlewares'>> {
26
+ aliases?: string[];
27
+ builder?: CommandBuilder | CommandBuilderDefinition;
28
+ command?: string | string[];
29
+ desc?: CommandHandler['description'];
30
+ describe?: CommandHandler['description'];
31
+ }
32
+ export declare function isCommandHandlerDefinition(cmd: string | string[] | CommandHandlerDefinition): cmd is CommandHandlerDefinition;
33
+ export interface CommandBuilderDefinition {
34
+ builder?: CommandBuilder;
35
+ deprecated?: boolean;
36
+ handler: CommandHandlerCallback;
37
+ middlewares?: Middleware[];
38
+ }
39
+ export declare function isCommandBuilderDefinition(builder?: CommandBuilder | CommandBuilderDefinition): builder is CommandBuilderDefinition;
40
+ export interface CommandHandlerCallback {
41
+ (argv: Arguments): any;
42
+ }
43
+ export interface CommandHandler {
44
+ builder: CommandBuilder;
45
+ demanded: Positional[];
46
+ deprecated?: boolean;
47
+ description?: string | false;
48
+ handler: CommandHandlerCallback;
49
+ middlewares: Middleware[];
50
+ optional: Positional[];
51
+ original: string;
52
+ }
53
+ export declare type CommandBuilder = CommandBuilderCallback | Dictionary<OptionDefinition>;
54
+ interface CommandBuilderCallback {
55
+ (y: YargsInstance): YargsInstance | void;
56
+ }
57
+ export declare function isCommandBuilderCallback(builder: CommandBuilder): builder is CommandBuilderCallback;
58
+ interface Positionals extends Pick<Options, 'alias' | 'array' | 'default'> {
59
+ demand: Dictionary<boolean>;
60
+ }
61
+ export interface FinishCommandHandler {
62
+ (handlerResult: any): any;
63
+ }
64
+ export {};