inray 0.0.4 → 0.0.6

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 (98) hide show
  1. package/LICENSE.md +29 -27
  2. package/README.md +57 -29
  3. package/dist/cli/mod.d.ts +2 -0
  4. package/dist/cli/mod.d.ts.map +1 -0
  5. package/dist/cli/mod.js +2 -0
  6. package/dist/cli/mod.js.map +1 -0
  7. package/dist/cli/src/index.d.ts +25 -0
  8. package/dist/cli/src/index.d.ts.map +1 -0
  9. package/dist/cli/src/index.js +25 -0
  10. package/dist/cli/src/index.js.map +1 -0
  11. package/dist/cli/src/lib/argument.d.ts +82 -0
  12. package/dist/cli/src/lib/argument.d.ts.map +1 -0
  13. package/dist/cli/src/lib/argument.js +143 -0
  14. package/dist/cli/src/lib/argument.js.map +1 -0
  15. package/dist/cli/src/lib/command.d.ts +1051 -0
  16. package/dist/cli/src/lib/command.d.ts.map +1 -0
  17. package/dist/cli/src/lib/command.js +2208 -0
  18. package/dist/cli/src/lib/command.js.map +1 -0
  19. package/dist/cli/src/lib/error.d.ts +35 -0
  20. package/dist/cli/src/lib/error.d.ts.map +1 -0
  21. package/dist/cli/src/lib/error.js +49 -0
  22. package/dist/cli/src/lib/error.js.map +1 -0
  23. package/dist/cli/src/lib/help.d.ts +250 -0
  24. package/dist/cli/src/lib/help.d.ts.map +1 -0
  25. package/dist/cli/src/lib/help.js +583 -0
  26. package/dist/cli/src/lib/help.js.map +1 -0
  27. package/dist/cli/src/lib/option.d.ts +189 -0
  28. package/dist/cli/src/lib/option.d.ts.map +1 -0
  29. package/dist/cli/src/lib/option.js +364 -0
  30. package/dist/cli/src/lib/option.js.map +1 -0
  31. package/dist/cli/src/lib/suggestSimilar.d.ts +18 -0
  32. package/dist/cli/src/lib/suggestSimilar.d.ts.map +1 -0
  33. package/dist/cli/src/lib/suggestSimilar.js +99 -0
  34. package/dist/cli/src/lib/suggestSimilar.js.map +1 -0
  35. package/dist/db/postgres/src/bytes.js +1 -0
  36. package/dist/db/postgres/src/connection.js +1 -0
  37. package/dist/db/postgres/src/errors.js +1 -0
  38. package/dist/db/postgres/src/index.js +1 -0
  39. package/dist/db/postgres/src/large.js +1 -0
  40. package/dist/db/postgres/src/query.js +1 -0
  41. package/dist/db/postgres/src/queue.js +1 -0
  42. package/dist/db/postgres/src/result.js +1 -0
  43. package/dist/db/postgres/src/subscribe.js +1 -0
  44. package/dist/db/postgres/src/types.js +1 -0
  45. package/dist/db-migrator/src/bin.d.ts +3 -0
  46. package/dist/db-migrator/src/bin.d.ts.map +1 -0
  47. package/dist/db-migrator/src/bin.js +72 -0
  48. package/dist/db-migrator/src/bin.js.map +1 -0
  49. package/dist/db-migrator/src/index.d.ts +5 -0
  50. package/dist/db-migrator/src/index.d.ts.map +1 -0
  51. package/dist/db-migrator/src/index.js +4 -0
  52. package/dist/db-migrator/src/index.js.map +1 -0
  53. package/dist/db-migrator/src/lib/MigrationError.d.ts +5 -0
  54. package/dist/db-migrator/src/lib/MigrationError.d.ts.map +1 -0
  55. package/dist/db-migrator/src/lib/MigrationError.js +12 -0
  56. package/dist/db-migrator/src/lib/MigrationError.js.map +1 -0
  57. package/dist/db-migrator/src/lib/clients/index.d.ts +9 -0
  58. package/dist/db-migrator/src/lib/clients/index.d.ts.map +1 -0
  59. package/dist/db-migrator/src/lib/clients/index.js +10 -0
  60. package/dist/db-migrator/src/lib/clients/index.js.map +1 -0
  61. package/dist/db-migrator/src/lib/clients/postgres.d.ts +3 -0
  62. package/dist/db-migrator/src/lib/clients/postgres.d.ts.map +1 -0
  63. package/dist/db-migrator/src/lib/clients/postgres.js +44 -0
  64. package/dist/db-migrator/src/lib/clients/postgres.js.map +1 -0
  65. package/dist/db-migrator/src/lib/migrator.log.d.ts +4 -0
  66. package/dist/db-migrator/src/lib/migrator.log.d.ts.map +1 -0
  67. package/dist/db-migrator/src/lib/migrator.log.js +46 -0
  68. package/dist/db-migrator/src/lib/migrator.log.js.map +1 -0
  69. package/dist/db-migrator/src/lib/migrator.sql.d.ts +5 -0
  70. package/dist/db-migrator/src/lib/migrator.sql.d.ts.map +1 -0
  71. package/dist/db-migrator/src/lib/migrator.sql.js +13 -0
  72. package/dist/db-migrator/src/lib/migrator.sql.js.map +1 -0
  73. package/dist/db-migrator/src/lib/migrator.types.d.ts +55 -0
  74. package/dist/db-migrator/src/lib/migrator.types.d.ts.map +1 -0
  75. package/dist/db-migrator/src/lib/migrator.types.js +2 -0
  76. package/dist/db-migrator/src/lib/migrator.types.js.map +1 -0
  77. package/dist/db-migrator/src/lib/util.d.ts +9 -0
  78. package/dist/db-migrator/src/lib/util.d.ts.map +1 -0
  79. package/dist/db-migrator/src/lib/util.js +93 -0
  80. package/dist/db-migrator/src/lib/util.js.map +1 -0
  81. package/dist/db-migrator/src/migrator.d.ts +6 -0
  82. package/dist/db-migrator/src/migrator.d.ts.map +1 -0
  83. package/dist/db-migrator/src/migrator.js +121 -0
  84. package/dist/db-migrator/src/migrator.js.map +1 -0
  85. package/dist/utils/mkdirs.d.ts +15 -0
  86. package/dist/utils/mkdirs.d.ts.map +1 -0
  87. package/dist/utils/mkdirs.js +42 -0
  88. package/dist/utils/mkdirs.js.map +1 -0
  89. package/dist/utils/mod.d.ts +2 -0
  90. package/dist/utils/mod.d.ts.map +1 -1
  91. package/dist/utils/mod.js +2 -0
  92. package/dist/utils/mod.js.map +1 -1
  93. package/dist/utils/totalist.d.ts +13 -0
  94. package/dist/utils/totalist.d.ts.map +1 -0
  95. package/dist/utils/totalist.js +39 -0
  96. package/dist/utils/totalist.js.map +1 -0
  97. package/dist/utils/validation/vEnvBoolean.d.ts +1 -1
  98. package/package.json +15 -3
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Original library: commander.js (v15.0.0)
3
+ * - Copyright (c) 2011 TJ Holowaychuk
4
+ * - Copyright (c) John Gee and "commander.js" authors
5
+ *
6
+ * Modified and integrated in "Inray":
7
+ * - Evgeniy Bushin
8
+ */
9
+ /***/
10
+ import type { ArgumentParseFunction } from './argument.ts';
11
+ export type OptionValues = Record<string, any>;
12
+ export declare class Option {
13
+ flags: string;
14
+ description: string;
15
+ required: boolean;
16
+ optional: boolean;
17
+ variadic: boolean;
18
+ mandatory: boolean;
19
+ short?: string;
20
+ long?: string;
21
+ negate: boolean;
22
+ defaultValue?: any;
23
+ defaultValueDescription?: string;
24
+ presetArg?: unknown;
25
+ envVar?: string;
26
+ parseArg?: ArgumentParseFunction;
27
+ hidden: boolean;
28
+ argChoices?: string[];
29
+ helpGroupHeading?: string;
30
+ conflictsWith: string[];
31
+ implied: OptionValues | undefined;
32
+ /**
33
+ * Initialize a new `Option` with the given `flags` and `description`.
34
+ *
35
+ * @param {string} flags
36
+ * @param {string} [description]
37
+ */
38
+ constructor(flags: string, description?: string);
39
+ /**
40
+ * Set the default value, and optionally supply the description to be displayed in the help.
41
+ *
42
+ * @param {*} value
43
+ * @param {string} [description]
44
+ * @return {Option}
45
+ */
46
+ default(value: unknown, description?: string): this;
47
+ /**
48
+ * Preset to use when option used without option-argument, especially optional but also boolean and negated.
49
+ * The custom processing (parseArg) is called.
50
+ *
51
+ * @example
52
+ * new Option('--color').default('GREYSCALE').preset('RGB');
53
+ * new Option('--donate [amount]').preset('20').argParser(parseFloat);
54
+ *
55
+ * @param {*} arg
56
+ * @return {Option}
57
+ */
58
+ preset(arg: unknown): this;
59
+ /**
60
+ * Add option name(s) that conflict with this option.
61
+ * An error will be displayed if conflicting options are found during parsing.
62
+ *
63
+ * @example
64
+ * new Option('--rgb').conflicts('cmyk');
65
+ * new Option('--js').conflicts(['ts', 'jsx']);
66
+ *
67
+ * @param {(string | string[])} names
68
+ * @return {Option}
69
+ */
70
+ conflicts(names: string | string[]): this;
71
+ /**
72
+ * Specify implied option values for when this option is set and the implied options are not.
73
+ *
74
+ * The custom processing (parseArg) is not called on the implied values.
75
+ *
76
+ * @example
77
+ * program
78
+ * .addOption(new Option('--log', 'write logging information to file'))
79
+ * .addOption(new Option('--trace', 'log extra details').implies({ log: 'trace.txt' }));
80
+ *
81
+ * @param {object} impliedOptionValues
82
+ * @return {Option}
83
+ */
84
+ implies(impliedOptionValues: OptionValues): this;
85
+ /**
86
+ * Set environment variable to check for option value.
87
+ *
88
+ * An environment variable is only used if when processed the current option value is
89
+ * undefined, or the source of the current value is 'default' or 'config' or 'env'.
90
+ *
91
+ * @param {string} name
92
+ * @return {Option}
93
+ */
94
+ env(name: string): this;
95
+ /**
96
+ * Set the custom handler for processing CLI option arguments into option values.
97
+ *
98
+ * @param {Function} [fn]
99
+ * @return {Option}
100
+ */
101
+ argParser(fn: ArgumentParseFunction): this;
102
+ /**
103
+ * Whether the option is mandatory and must have a value after parsing.
104
+ *
105
+ * @param {boolean} [mandatory=true]
106
+ * @return {Option}
107
+ */
108
+ makeOptionMandatory(mandatory?: boolean): this;
109
+ /**
110
+ * Hide option in help.
111
+ *
112
+ * @param {boolean} [hide=true]
113
+ * @return {Option}
114
+ */
115
+ hideHelp(hide?: boolean): this;
116
+ /**
117
+ * @package
118
+ */
119
+ _collectValue(value: any, previous: any | Array<any>): Array<any>;
120
+ /**
121
+ * Only allow option value to be one of choices.
122
+ *
123
+ * @param {string[]} values
124
+ * @return {Option}
125
+ */
126
+ choices(values: readonly string[]): this;
127
+ /**
128
+ * Return option name.
129
+ *
130
+ * @return {string}
131
+ */
132
+ name(): string;
133
+ /**
134
+ * Return option name, in a camelcase format that can be used
135
+ * as an object attribute key.
136
+ *
137
+ * @return {string}
138
+ */
139
+ attributeName(): string;
140
+ /**
141
+ * Set the help group heading.
142
+ *
143
+ * @param {string} heading
144
+ * @return {Option}
145
+ */
146
+ helpGroup(heading: string): this;
147
+ /**
148
+ * Check if `arg` matches the short or long flag.
149
+ *
150
+ * @param {string} arg
151
+ * @return {boolean}
152
+ * @package
153
+ */
154
+ is(arg: string): boolean;
155
+ /**
156
+ * Return whether a boolean option.
157
+ *
158
+ * Options are one of boolean, negated, required argument, or optional argument.
159
+ *
160
+ * @return {boolean}
161
+ * @package
162
+ */
163
+ isBoolean(): boolean;
164
+ }
165
+ /**
166
+ * This class is to make it easier to work with dual options, without changing the existing
167
+ * implementation. We support separate dual options for separate positive and negative options,
168
+ * like `--build` and `--no-build`, which share a single option value. This works nicely for some
169
+ * use cases, but is tricky for others where we want separate behaviours despite
170
+ * the single shared option value.
171
+ */
172
+ export declare class DualOptions {
173
+ positiveOptions: Map<string, Option>;
174
+ negativeOptions: Map<string, Option>;
175
+ dualOptions: Set<string>;
176
+ /**
177
+ * @param {Option[]} options
178
+ */
179
+ constructor(options: readonly Option[]);
180
+ /**
181
+ * Did the value come from the option, and not from possible matching dual option?
182
+ *
183
+ * @param {*} value
184
+ * @param {Option} option
185
+ * @returns {boolean}
186
+ */
187
+ valueFromOption(value: any, option: Option): boolean;
188
+ }
189
+ //# sourceMappingURL=option.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"option.d.ts","sourceRoot":"","sources":["../../../../src/cli/src/lib/option.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,KAAK;AAEL,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAG3D,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAE/C,qBAAa,MAAM;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IAEpB,QAAQ,EAAE,OAAO,CAAC;IAClB,QAAQ,EAAE,OAAO,CAAC;IAClB,QAAQ,EAAE,OAAO,CAAC;IAClB,SAAS,EAAE,OAAO,CAAC;IAEnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,MAAM,EAAE,OAAO,CAAC;IAEhB,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,uBAAuB,CAAC,EAAE,MAAM,CAAC;IACjC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IA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,GAAG,IAAI;IAKhC;;;;;;OAMG;IACH,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIxB;;;;;;;OAOG;IACH,SAAS,IAAI,OAAO;CAGrB;AAED;;;;;;GAMG;AACH,qBAAa,WAAW;IACtB,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAEzB;;OAEG;gBACS,OAAO,EAAE,SAAS,MAAM,EAAE;IAkBtC;;;;;;OAMG;IACH,eAAe,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO;CASrD"}
@@ -0,0 +1,364 @@
1
+ /**
2
+ * Original library: commander.js (v15.0.0)
3
+ * - Copyright (c) 2011 TJ Holowaychuk
4
+ * - Copyright (c) John Gee and "commander.js" authors
5
+ *
6
+ * Modified and integrated in "Inray":
7
+ * - Evgeniy Bushin
8
+ */
9
+ /***/
10
+ import { InvalidArgumentError } from "./error.js";
11
+ export class Option {
12
+ flags;
13
+ description;
14
+ required;
15
+ optional;
16
+ variadic;
17
+ mandatory;
18
+ short;
19
+ long;
20
+ negate;
21
+ defaultValue;
22
+ defaultValueDescription;
23
+ presetArg;
24
+ envVar;
25
+ parseArg; // TODO с T , Function
26
+ hidden;
27
+ argChoices;
28
+ helpGroupHeading;
29
+ conflictsWith;
30
+ implied;
31
+ /**
32
+ * Initialize a new `Option` with the given `flags` and `description`.
33
+ *
34
+ * @param {string} flags
35
+ * @param {string} [description]
36
+ */
37
+ constructor(flags, description) {
38
+ this.flags = flags;
39
+ this.description = description || '';
40
+ this.required = flags.includes('<'); // A value must be supplied when the option is specified.
41
+ this.optional = flags.includes('['); // A value is optional when the option is specified.
42
+ // variadic test ignores <value,...> et al which might be used to describe custom splitting of single argument
43
+ this.variadic = /\w\.\.\.[>\]]$/.test(flags); // The option can take multiple values.
44
+ this.mandatory = false; // The option must have a value after parsing, which usually means it must be specified on command line.
45
+ const optionFlags = splitOptionFlags(flags);
46
+ this.short = optionFlags.shortFlag; // May be a short flag, undefined, or even a long flag (if option has two long flags).
47
+ this.long = optionFlags.longFlag;
48
+ this.negate = false;
49
+ if (this.long) {
50
+ this.negate = this.long.startsWith('--no-');
51
+ }
52
+ this.defaultValue = undefined;
53
+ this.defaultValueDescription = undefined;
54
+ this.presetArg = undefined;
55
+ this.envVar = undefined;
56
+ this.parseArg = undefined;
57
+ this.hidden = false;
58
+ this.argChoices = undefined;
59
+ this.conflictsWith = [];
60
+ this.implied = undefined;
61
+ this.helpGroupHeading = undefined; // soft initialised when option added to command
62
+ }
63
+ /**
64
+ * Set the default value, and optionally supply the description to be displayed in the help.
65
+ *
66
+ * @param {*} value
67
+ * @param {string} [description]
68
+ * @return {Option}
69
+ */
70
+ default(value, description) {
71
+ this.defaultValue = value;
72
+ this.defaultValueDescription = description;
73
+ return this;
74
+ }
75
+ /**
76
+ * Preset to use when option used without option-argument, especially optional but also boolean and negated.
77
+ * The custom processing (parseArg) is called.
78
+ *
79
+ * @example
80
+ * new Option('--color').default('GREYSCALE').preset('RGB');
81
+ * new Option('--donate [amount]').preset('20').argParser(parseFloat);
82
+ *
83
+ * @param {*} arg
84
+ * @return {Option}
85
+ */
86
+ preset(arg) {
87
+ this.presetArg = arg;
88
+ return this;
89
+ }
90
+ /**
91
+ * Add option name(s) that conflict with this option.
92
+ * An error will be displayed if conflicting options are found during parsing.
93
+ *
94
+ * @example
95
+ * new Option('--rgb').conflicts('cmyk');
96
+ * new Option('--js').conflicts(['ts', 'jsx']);
97
+ *
98
+ * @param {(string | string[])} names
99
+ * @return {Option}
100
+ */
101
+ conflicts(names) {
102
+ this.conflictsWith = this.conflictsWith.concat(names);
103
+ return this;
104
+ }
105
+ /**
106
+ * Specify implied option values for when this option is set and the implied options are not.
107
+ *
108
+ * The custom processing (parseArg) is not called on the implied values.
109
+ *
110
+ * @example
111
+ * program
112
+ * .addOption(new Option('--log', 'write logging information to file'))
113
+ * .addOption(new Option('--trace', 'log extra details').implies({ log: 'trace.txt' }));
114
+ *
115
+ * @param {object} impliedOptionValues
116
+ * @return {Option}
117
+ */
118
+ implies(impliedOptionValues) {
119
+ let newImplied = impliedOptionValues;
120
+ if (typeof impliedOptionValues === 'string') {
121
+ // string is not documented, but easy mistake and we can do what user probably intended.
122
+ newImplied = { [impliedOptionValues]: true };
123
+ }
124
+ this.implied = Object.assign(this.implied || {}, newImplied);
125
+ return this;
126
+ }
127
+ /**
128
+ * Set environment variable to check for option value.
129
+ *
130
+ * An environment variable is only used if when processed the current option value is
131
+ * undefined, or the source of the current value is 'default' or 'config' or 'env'.
132
+ *
133
+ * @param {string} name
134
+ * @return {Option}
135
+ */
136
+ env(name) {
137
+ this.envVar = name;
138
+ return this;
139
+ }
140
+ /**
141
+ * Set the custom handler for processing CLI option arguments into option values.
142
+ *
143
+ * @param {Function} [fn]
144
+ * @return {Option}
145
+ */
146
+ argParser(fn) {
147
+ this.parseArg = fn;
148
+ return this;
149
+ }
150
+ /**
151
+ * Whether the option is mandatory and must have a value after parsing.
152
+ *
153
+ * @param {boolean} [mandatory=true]
154
+ * @return {Option}
155
+ */
156
+ makeOptionMandatory(mandatory = true) {
157
+ this.mandatory = !!mandatory;
158
+ return this;
159
+ }
160
+ /**
161
+ * Hide option in help.
162
+ *
163
+ * @param {boolean} [hide=true]
164
+ * @return {Option}
165
+ */
166
+ hideHelp(hide = true) {
167
+ this.hidden = !!hide;
168
+ return this;
169
+ }
170
+ /**
171
+ * @package
172
+ */
173
+ _collectValue(value, previous) {
174
+ if (previous === this.defaultValue || !Array.isArray(previous)) {
175
+ return [value];
176
+ }
177
+ previous.push(value);
178
+ return previous;
179
+ }
180
+ /**
181
+ * Only allow option value to be one of choices.
182
+ *
183
+ * @param {string[]} values
184
+ * @return {Option}
185
+ */
186
+ choices(values) {
187
+ this.argChoices = values.slice();
188
+ this.parseArg = (arg, previous) => {
189
+ if (this.argChoices && !this.argChoices.includes(arg)) {
190
+ throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(', ')}.`);
191
+ }
192
+ if (this.variadic) {
193
+ return this._collectValue(arg, previous);
194
+ }
195
+ return arg;
196
+ };
197
+ return this;
198
+ }
199
+ /**
200
+ * Return option name.
201
+ *
202
+ * @return {string}
203
+ */
204
+ name() {
205
+ if (this.long) {
206
+ return this.long.replace(/^--/, '');
207
+ }
208
+ return this.short?.replace(/^-/, '') ?? ''; // TODO
209
+ }
210
+ /**
211
+ * Return option name, in a camelcase format that can be used
212
+ * as an object attribute key.
213
+ *
214
+ * @return {string}
215
+ */
216
+ attributeName() {
217
+ if (this.negate) {
218
+ return camelcase(this.name().replace(/^no-/, ''));
219
+ }
220
+ return camelcase(this.name());
221
+ }
222
+ /**
223
+ * Set the help group heading.
224
+ *
225
+ * @param {string} heading
226
+ * @return {Option}
227
+ */
228
+ helpGroup(heading) {
229
+ this.helpGroupHeading = heading;
230
+ return this;
231
+ }
232
+ /**
233
+ * Check if `arg` matches the short or long flag.
234
+ *
235
+ * @param {string} arg
236
+ * @return {boolean}
237
+ * @package
238
+ */
239
+ is(arg) {
240
+ return this.short === arg || this.long === arg;
241
+ }
242
+ /**
243
+ * Return whether a boolean option.
244
+ *
245
+ * Options are one of boolean, negated, required argument, or optional argument.
246
+ *
247
+ * @return {boolean}
248
+ * @package
249
+ */
250
+ isBoolean() {
251
+ return !this.required && !this.optional && !this.negate;
252
+ }
253
+ }
254
+ /**
255
+ * This class is to make it easier to work with dual options, without changing the existing
256
+ * implementation. We support separate dual options for separate positive and negative options,
257
+ * like `--build` and `--no-build`, which share a single option value. This works nicely for some
258
+ * use cases, but is tricky for others where we want separate behaviours despite
259
+ * the single shared option value.
260
+ */
261
+ export class DualOptions {
262
+ positiveOptions;
263
+ negativeOptions;
264
+ dualOptions;
265
+ /**
266
+ * @param {Option[]} options
267
+ */
268
+ constructor(options) {
269
+ this.positiveOptions = new Map();
270
+ this.negativeOptions = new Map();
271
+ this.dualOptions = new Set();
272
+ options.forEach((option) => {
273
+ if (option.negate) {
274
+ this.negativeOptions.set(option.attributeName(), option);
275
+ }
276
+ else {
277
+ this.positiveOptions.set(option.attributeName(), option);
278
+ }
279
+ });
280
+ this.negativeOptions.forEach((_value, key) => {
281
+ if (this.positiveOptions.has(key)) {
282
+ this.dualOptions.add(key);
283
+ }
284
+ });
285
+ }
286
+ /**
287
+ * Did the value come from the option, and not from possible matching dual option?
288
+ *
289
+ * @param {*} value
290
+ * @param {Option} option
291
+ * @returns {boolean}
292
+ */
293
+ valueFromOption(value, option) {
294
+ const optionKey = option.attributeName();
295
+ if (!this.dualOptions.has(optionKey))
296
+ return true;
297
+ // Use the value to deduce if (probably) came from the option.
298
+ const preset = this.negativeOptions.get(optionKey)?.presetArg;
299
+ const negativeValue = preset !== undefined ? preset : false;
300
+ return option.negate === (negativeValue === value);
301
+ }
302
+ }
303
+ /**
304
+ * Convert string from kebab-case to camelCase.
305
+ *
306
+ * @param {string} str
307
+ * @return {string}
308
+ * @private
309
+ */
310
+ function camelcase(str) {
311
+ return str.split('-').reduce((str, word) => {
312
+ return str + word[0].toUpperCase() + word.slice(1);
313
+ });
314
+ }
315
+ /**
316
+ * Split the short and long flag out of something like '-m,--mixed <value>'
317
+ *
318
+ * @private
319
+ */
320
+ function splitOptionFlags(flags) {
321
+ let shortFlag;
322
+ let longFlag;
323
+ // short flag, single dash and single character
324
+ const shortFlagExp = /^-[^-]$/;
325
+ // long flag, double dash and at least one character
326
+ const longFlagExp = /^--[^-]/;
327
+ const flagParts = flags.split(/[ |,]+/).concat('guard');
328
+ // Normal is short and/or long.
329
+ if (shortFlagExp.test(flagParts[0]))
330
+ shortFlag = flagParts.shift();
331
+ if (longFlagExp.test(flagParts[0]))
332
+ longFlag = flagParts.shift();
333
+ // Long then short. Rarely used but fine.
334
+ if (!shortFlag && shortFlagExp.test(flagParts[0]))
335
+ shortFlag = flagParts.shift();
336
+ // Allow two long flags, like '--ws, --workspace'
337
+ // This is the supported way to have a shortish option flag.
338
+ if (!shortFlag && longFlagExp.test(flagParts[0])) {
339
+ shortFlag = longFlag;
340
+ longFlag = flagParts.shift();
341
+ }
342
+ // Check for unprocessed flag. Fail noisily rather than silently ignore.
343
+ if (flagParts[0].startsWith('-')) {
344
+ const unsupportedFlag = flagParts[0];
345
+ const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
346
+ if (/^-[^-][^-]/.test(unsupportedFlag))
347
+ throw new Error(`${baseError}
348
+ - a short flag is a single dash and a single character
349
+ - either use a single dash and a single character (for a short flag)
350
+ - or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
351
+ if (shortFlagExp.test(unsupportedFlag))
352
+ throw new Error(`${baseError}
353
+ - too many short flags`);
354
+ if (longFlagExp.test(unsupportedFlag))
355
+ throw new Error(`${baseError}
356
+ - too many long flags`);
357
+ throw new Error(`${baseError}
358
+ - unrecognised flag format`);
359
+ }
360
+ if (shortFlag === undefined && longFlag === undefined)
361
+ throw new Error(`option creation failed due to no flags found in '${flags}'.`);
362
+ return { shortFlag, longFlag };
363
+ }
364
+ //# sourceMappingURL=option.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"option.js","sourceRoot":"","sources":["../../../../src/cli/src/lib/option.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,KAAK;AAGL,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAIlD,MAAM,OAAO,MAAM;IACjB,KAAK,CAAS;IACd,WAAW,CAAS;IAEpB,QAAQ,CAAU;IAClB,QAAQ,CAAU;IAClB,QAAQ,CAAU;IAClB,SAAS,CAAU;IAEnB,KAAK,CAAU;IACf,IAAI,CAAU;IAEd,MAAM,CAAU;IAEhB,YAAY,CAAO;IACnB,uBAAuB,CAAU;IACjC,SAAS,CAAW;IACpB,MAAM,CAAU;IAChB,QAAQ,CAAyB,CAAC,sBAAsB;IACxD,MAAM,CAA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Ae,CAAC;YACpC,MAAM,IAAI,KAAK,CACb,GAAG,SAAS;;;wFAGoE,CACjF,CAAC;QACJ,IAAI,YAAY,CAAC,IAAI,CAAC,eAAe,CAAC;YACpC,MAAM,IAAI,KAAK,CAAC,GAAG,SAAS;uBACX,CAAC,CAAC;QACrB,IAAI,WAAW,CAAC,IAAI,CAAC,eAAe,CAAC;YACnC,MAAM,IAAI,KAAK,CAAC,GAAG,SAAS;sBACZ,CAAC,CAAC;QAEpB,MAAM,IAAI,KAAK,CAAC,GAAG,SAAS;2BACL,CAAC,CAAC;IAC3B,CAAC;IACD,IAAI,SAAS,KAAK,SAAS,IAAI,QAAQ,KAAK,SAAS;QACnD,MAAM,IAAI,KAAK,CAAC,oDAAoD,KAAK,IAAI,CAAC,CAAC;IAEjF,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC;AACjC,CAAC"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Original library: commander.js (v15.0.0)
3
+ * - Copyright (c) 2011 TJ Holowaychuk
4
+ * - Copyright (c) John Gee and "commander.js" authors
5
+ *
6
+ * Modified and integrated in "Inray":
7
+ * - Evgeniy Bushin
8
+ */
9
+ /***/
10
+ /**
11
+ * Find close matches, restricted to same number of edits.
12
+ *
13
+ * @param {string} word
14
+ * @param {string[]} candidates
15
+ * @returns {string}
16
+ */
17
+ export declare function suggestSimilar(word: string, candidates: string[]): string;
18
+ //# sourceMappingURL=suggestSimilar.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"suggestSimilar.d.ts","sourceRoot":"","sources":["../../../../src/cli/src/lib/suggestSimilar.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,KAAK;AAgDL;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,MAAM,CA2CzE"}
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Original library: commander.js (v15.0.0)
3
+ * - Copyright (c) 2011 TJ Holowaychuk
4
+ * - Copyright (c) John Gee and "commander.js" authors
5
+ *
6
+ * Modified and integrated in "Inray":
7
+ * - Evgeniy Bushin
8
+ */
9
+ /***/
10
+ const maxDistance = 3;
11
+ function editDistance(a, b) {
12
+ // https://en.wikipedia.org/wiki/Damerau–Levenshtein_distance
13
+ // Calculating optimal string alignment distance, no substring is edited more than once.
14
+ // (Simple implementation.)
15
+ // Quick early exit, return worst case.
16
+ if (Math.abs(a.length - b.length) > maxDistance)
17
+ return Math.max(a.length, b.length);
18
+ // distance between prefix substrings of a and b
19
+ const d = [];
20
+ // pure deletions turn a into empty string
21
+ for (let i = 0; i <= a.length; i++) {
22
+ d[i] = [i];
23
+ }
24
+ // pure insertions turn empty string into b
25
+ for (let j = 0; j <= b.length; j++) {
26
+ d[0][j] = j;
27
+ }
28
+ // fill matrix
29
+ for (let j = 1; j <= b.length; j++) {
30
+ for (let i = 1; i <= a.length; i++) {
31
+ let cost;
32
+ if (a[i - 1] === b[j - 1]) {
33
+ cost = 0;
34
+ }
35
+ else {
36
+ cost = 1;
37
+ }
38
+ d[i][j] = Math.min(d[i - 1][j] + 1, // deletion
39
+ d[i][j - 1] + 1, // insertion
40
+ d[i - 1][j - 1] + cost // substitution
41
+ );
42
+ // transposition
43
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
44
+ d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
45
+ }
46
+ }
47
+ }
48
+ return d[a.length][b.length];
49
+ }
50
+ /**
51
+ * Find close matches, restricted to same number of edits.
52
+ *
53
+ * @param {string} word
54
+ * @param {string[]} candidates
55
+ * @returns {string}
56
+ */
57
+ export function suggestSimilar(word, candidates) {
58
+ if (!candidates || candidates.length === 0)
59
+ return '';
60
+ // remove possible duplicates
61
+ candidates = Array.from(new Set(candidates));
62
+ const searchingOptions = word.startsWith('--');
63
+ if (searchingOptions) {
64
+ word = word.slice(2);
65
+ candidates = candidates.map((candidate) => candidate.slice(2));
66
+ }
67
+ let similar = [];
68
+ let bestDistance = maxDistance;
69
+ const minSimilarity = 0.4;
70
+ candidates.forEach((candidate) => {
71
+ if (candidate.length <= 1)
72
+ return; // no one character guesses
73
+ const distance = editDistance(word, candidate);
74
+ const length = Math.max(word.length, candidate.length);
75
+ const similarity = (length - distance) / length;
76
+ if (similarity > minSimilarity) {
77
+ if (distance < bestDistance) {
78
+ // better edit distance, throw away previous worse matches
79
+ bestDistance = distance;
80
+ similar = [candidate];
81
+ }
82
+ else if (distance === bestDistance) {
83
+ similar.push(candidate);
84
+ }
85
+ }
86
+ });
87
+ similar.sort((a, b) => a.localeCompare(b));
88
+ if (searchingOptions) {
89
+ similar = similar.map((candidate) => `--${candidate}`);
90
+ }
91
+ if (similar.length > 1) {
92
+ return `\n(Did you mean one of ${similar.join(', ')}?)`;
93
+ }
94
+ if (similar.length === 1) {
95
+ return `\n(Did you mean ${similar[0]}?)`;
96
+ }
97
+ return '';
98
+ }
99
+ //# sourceMappingURL=suggestSimilar.js.map