lerna-projen 0.1.38 → 0.1.40

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.
@@ -0,0 +1,326 @@
1
+ const { InvalidArgumentError } = require('./error.js');
2
+
3
+ // @ts-check
4
+
5
+ class Option {
6
+ /**
7
+ * Initialize a new `Option` with the given `flags` and `description`.
8
+ *
9
+ * @param {string} flags
10
+ * @param {string} [description]
11
+ */
12
+
13
+ constructor(flags, description) {
14
+ this.flags = flags;
15
+ this.description = description || '';
16
+
17
+ this.required = flags.includes('<'); // A value must be supplied when the option is specified.
18
+ this.optional = flags.includes('['); // A value is optional when the option is specified.
19
+ // variadic test ignores <value,...> et al which might be used to describe custom splitting of single argument
20
+ this.variadic = /\w\.\.\.[>\]]$/.test(flags); // The option can take multiple values.
21
+ this.mandatory = false; // The option must have a value after parsing, which usually means it must be specified on command line.
22
+ const optionFlags = splitOptionFlags(flags);
23
+ this.short = optionFlags.shortFlag;
24
+ this.long = optionFlags.longFlag;
25
+ this.negate = false;
26
+ if (this.long) {
27
+ this.negate = this.long.startsWith('--no-');
28
+ }
29
+ this.defaultValue = undefined;
30
+ this.defaultValueDescription = undefined;
31
+ this.presetArg = undefined;
32
+ this.envVar = undefined;
33
+ this.parseArg = undefined;
34
+ this.hidden = false;
35
+ this.argChoices = undefined;
36
+ this.conflictsWith = [];
37
+ this.implied = undefined;
38
+ }
39
+
40
+ /**
41
+ * Set the default value, and optionally supply the description to be displayed in the help.
42
+ *
43
+ * @param {any} value
44
+ * @param {string} [description]
45
+ * @return {Option}
46
+ */
47
+
48
+ default(value, description) {
49
+ this.defaultValue = value;
50
+ this.defaultValueDescription = description;
51
+ return this;
52
+ }
53
+
54
+ /**
55
+ * Preset to use when option used without option-argument, especially optional but also boolean and negated.
56
+ * The custom processing (parseArg) is called.
57
+ *
58
+ * @example
59
+ * new Option('--color').default('GREYSCALE').preset('RGB');
60
+ * new Option('--donate [amount]').preset('20').argParser(parseFloat);
61
+ *
62
+ * @param {any} arg
63
+ * @return {Option}
64
+ */
65
+
66
+ preset(arg) {
67
+ this.presetArg = arg;
68
+ return this;
69
+ }
70
+
71
+ /**
72
+ * Add option name(s) that conflict with this option.
73
+ * An error will be displayed if conflicting options are found during parsing.
74
+ *
75
+ * @example
76
+ * new Option('--rgb').conflicts('cmyk');
77
+ * new Option('--js').conflicts(['ts', 'jsx']);
78
+ *
79
+ * @param {string | string[]} names
80
+ * @return {Option}
81
+ */
82
+
83
+ conflicts(names) {
84
+ this.conflictsWith = this.conflictsWith.concat(names);
85
+ return this;
86
+ }
87
+
88
+ /**
89
+ * Specify implied option values for when this option is set and the implied options are not.
90
+ *
91
+ * The custom processing (parseArg) is not called on the implied values.
92
+ *
93
+ * @example
94
+ * program
95
+ * .addOption(new Option('--log', 'write logging information to file'))
96
+ * .addOption(new Option('--trace', 'log extra details').implies({ log: 'trace.txt' }));
97
+ *
98
+ * @param {Object} impliedOptionValues
99
+ * @return {Option}
100
+ */
101
+ implies(impliedOptionValues) {
102
+ this.implied = Object.assign(this.implied || {}, impliedOptionValues);
103
+ return this;
104
+ }
105
+
106
+ /**
107
+ * Set environment variable to check for option value.
108
+ *
109
+ * An environment variable is only used if when processed the current option value is
110
+ * undefined, or the source of the current value is 'default' or 'config' or 'env'.
111
+ *
112
+ * @param {string} name
113
+ * @return {Option}
114
+ */
115
+
116
+ env(name) {
117
+ this.envVar = name;
118
+ return this;
119
+ }
120
+
121
+ /**
122
+ * Set the custom handler for processing CLI option arguments into option values.
123
+ *
124
+ * @param {Function} [fn]
125
+ * @return {Option}
126
+ */
127
+
128
+ argParser(fn) {
129
+ this.parseArg = fn;
130
+ return this;
131
+ }
132
+
133
+ /**
134
+ * Whether the option is mandatory and must have a value after parsing.
135
+ *
136
+ * @param {boolean} [mandatory=true]
137
+ * @return {Option}
138
+ */
139
+
140
+ makeOptionMandatory(mandatory = true) {
141
+ this.mandatory = !!mandatory;
142
+ return this;
143
+ }
144
+
145
+ /**
146
+ * Hide option in help.
147
+ *
148
+ * @param {boolean} [hide=true]
149
+ * @return {Option}
150
+ */
151
+
152
+ hideHelp(hide = true) {
153
+ this.hidden = !!hide;
154
+ return this;
155
+ }
156
+
157
+ /**
158
+ * @api private
159
+ */
160
+
161
+ _concatValue(value, previous) {
162
+ if (previous === this.defaultValue || !Array.isArray(previous)) {
163
+ return [value];
164
+ }
165
+
166
+ return previous.concat(value);
167
+ }
168
+
169
+ /**
170
+ * Only allow option value to be one of choices.
171
+ *
172
+ * @param {string[]} values
173
+ * @return {Option}
174
+ */
175
+
176
+ choices(values) {
177
+ this.argChoices = values.slice();
178
+ this.parseArg = (arg, previous) => {
179
+ if (!this.argChoices.includes(arg)) {
180
+ throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(', ')}.`);
181
+ }
182
+ if (this.variadic) {
183
+ return this._concatValue(arg, previous);
184
+ }
185
+ return arg;
186
+ };
187
+ return this;
188
+ }
189
+
190
+ /**
191
+ * Return option name.
192
+ *
193
+ * @return {string}
194
+ */
195
+
196
+ name() {
197
+ if (this.long) {
198
+ return this.long.replace(/^--/, '');
199
+ }
200
+ return this.short.replace(/^-/, '');
201
+ }
202
+
203
+ /**
204
+ * Return option name, in a camelcase format that can be used
205
+ * as a object attribute key.
206
+ *
207
+ * @return {string}
208
+ * @api private
209
+ */
210
+
211
+ attributeName() {
212
+ return camelcase(this.name().replace(/^no-/, ''));
213
+ }
214
+
215
+ /**
216
+ * Check if `arg` matches the short or long flag.
217
+ *
218
+ * @param {string} arg
219
+ * @return {boolean}
220
+ * @api private
221
+ */
222
+
223
+ is(arg) {
224
+ return this.short === arg || this.long === arg;
225
+ }
226
+
227
+ /**
228
+ * Return whether a boolean option.
229
+ *
230
+ * Options are one of boolean, negated, required argument, or optional argument.
231
+ *
232
+ * @return {boolean}
233
+ * @api private
234
+ */
235
+
236
+ isBoolean() {
237
+ return !this.required && !this.optional && !this.negate;
238
+ }
239
+ }
240
+
241
+ /**
242
+ * This class is to make it easier to work with dual options, without changing the existing
243
+ * implementation. We support separate dual options for separate positive and negative options,
244
+ * like `--build` and `--no-build`, which share a single option value. This works nicely for some
245
+ * use cases, but is tricky for others where we want separate behaviours despite
246
+ * the single shared option value.
247
+ */
248
+ class DualOptions {
249
+ /**
250
+ * @param {Option[]} options
251
+ */
252
+ constructor(options) {
253
+ this.positiveOptions = new Map();
254
+ this.negativeOptions = new Map();
255
+ this.dualOptions = new Set();
256
+ options.forEach(option => {
257
+ if (option.negate) {
258
+ this.negativeOptions.set(option.attributeName(), option);
259
+ } else {
260
+ this.positiveOptions.set(option.attributeName(), option);
261
+ }
262
+ });
263
+ this.negativeOptions.forEach((value, key) => {
264
+ if (this.positiveOptions.has(key)) {
265
+ this.dualOptions.add(key);
266
+ }
267
+ });
268
+ }
269
+
270
+ /**
271
+ * Did the value come from the option, and not from possible matching dual option?
272
+ *
273
+ * @param {any} value
274
+ * @param {Option} option
275
+ * @returns {boolean}
276
+ */
277
+ valueFromOption(value, option) {
278
+ const optionKey = option.attributeName();
279
+ if (!this.dualOptions.has(optionKey)) return true;
280
+
281
+ // Use the value to deduce if (probably) came from the option.
282
+ const preset = this.negativeOptions.get(optionKey).presetArg;
283
+ const negativeValue = (preset !== undefined) ? preset : false;
284
+ return option.negate === (negativeValue === value);
285
+ }
286
+ }
287
+
288
+ /**
289
+ * Convert string from kebab-case to camelCase.
290
+ *
291
+ * @param {string} str
292
+ * @return {string}
293
+ * @api private
294
+ */
295
+
296
+ function camelcase(str) {
297
+ return str.split('-').reduce((str, word) => {
298
+ return str + word[0].toUpperCase() + word.slice(1);
299
+ });
300
+ }
301
+
302
+ /**
303
+ * Split the short and long flag out of something like '-m,--mixed <value>'
304
+ *
305
+ * @api private
306
+ */
307
+
308
+ function splitOptionFlags(flags) {
309
+ let shortFlag;
310
+ let longFlag;
311
+ // Use original very loose parsing to maintain backwards compatibility for now,
312
+ // which allowed for example unintended `-sw, --short-word` [sic].
313
+ const flagParts = flags.split(/[ |,]+/);
314
+ if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1])) shortFlag = flagParts.shift();
315
+ longFlag = flagParts.shift();
316
+ // Add support for lone short flag without significantly changing parsing!
317
+ if (!shortFlag && /^-[^-]$/.test(longFlag)) {
318
+ shortFlag = longFlag;
319
+ longFlag = undefined;
320
+ }
321
+ return { shortFlag, longFlag };
322
+ }
323
+
324
+ exports.Option = Option;
325
+ exports.splitOptionFlags = splitOptionFlags;
326
+ exports.DualOptions = DualOptions;
@@ -0,0 +1,100 @@
1
+ const maxDistance = 3;
2
+
3
+ function editDistance(a, b) {
4
+ // https://en.wikipedia.org/wiki/Damerau–Levenshtein_distance
5
+ // Calculating optimal string alignment distance, no substring is edited more than once.
6
+ // (Simple implementation.)
7
+
8
+ // Quick early exit, return worst case.
9
+ if (Math.abs(a.length - b.length) > maxDistance) return Math.max(a.length, b.length);
10
+
11
+ // distance between prefix substrings of a and b
12
+ const d = [];
13
+
14
+ // pure deletions turn a into empty string
15
+ for (let i = 0; i <= a.length; i++) {
16
+ d[i] = [i];
17
+ }
18
+ // pure insertions turn empty string into b
19
+ for (let j = 0; j <= b.length; j++) {
20
+ d[0][j] = j;
21
+ }
22
+
23
+ // fill matrix
24
+ for (let j = 1; j <= b.length; j++) {
25
+ for (let i = 1; i <= a.length; i++) {
26
+ let cost = 1;
27
+ if (a[i - 1] === b[j - 1]) {
28
+ cost = 0;
29
+ } else {
30
+ cost = 1;
31
+ }
32
+ d[i][j] = Math.min(
33
+ d[i - 1][j] + 1, // deletion
34
+ d[i][j - 1] + 1, // insertion
35
+ d[i - 1][j - 1] + cost // substitution
36
+ );
37
+ // transposition
38
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
39
+ d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
40
+ }
41
+ }
42
+ }
43
+
44
+ return d[a.length][b.length];
45
+ }
46
+
47
+ /**
48
+ * Find close matches, restricted to same number of edits.
49
+ *
50
+ * @param {string} word
51
+ * @param {string[]} candidates
52
+ * @returns {string}
53
+ */
54
+
55
+ function suggestSimilar(word, candidates) {
56
+ if (!candidates || candidates.length === 0) return '';
57
+ // remove possible duplicates
58
+ candidates = Array.from(new Set(candidates));
59
+
60
+ const searchingOptions = word.startsWith('--');
61
+ if (searchingOptions) {
62
+ word = word.slice(2);
63
+ candidates = candidates.map(candidate => candidate.slice(2));
64
+ }
65
+
66
+ let similar = [];
67
+ let bestDistance = maxDistance;
68
+ const minSimilarity = 0.4;
69
+ candidates.forEach((candidate) => {
70
+ if (candidate.length <= 1) return; // no one character guesses
71
+
72
+ const distance = editDistance(word, candidate);
73
+ const length = Math.max(word.length, candidate.length);
74
+ const similarity = (length - distance) / length;
75
+ if (similarity > minSimilarity) {
76
+ if (distance < bestDistance) {
77
+ // better edit distance, throw away previous worse matches
78
+ bestDistance = distance;
79
+ similar = [candidate];
80
+ } else if (distance === bestDistance) {
81
+ similar.push(candidate);
82
+ }
83
+ }
84
+ });
85
+
86
+ similar.sort((a, b) => a.localeCompare(b));
87
+ if (searchingOptions) {
88
+ similar = similar.map(candidate => `--${candidate}`);
89
+ }
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
+
100
+ exports.suggestSimilar = suggestSimilar;
@@ -0,0 +1,16 @@
1
+ {
2
+ "versions": [
3
+ {
4
+ "version": "*",
5
+ "target": {
6
+ "node": "supported"
7
+ },
8
+ "response": {
9
+ "type": "time-permitting"
10
+ },
11
+ "backing": {
12
+ "npm-funding": true
13
+ }
14
+ }
15
+ ]
16
+ }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commander",
3
- "version": "6.2.1",
3
+ "version": "10.0.0",
4
4
  "description": "the complete solution for node.js command-line programs",
5
5
  "keywords": [
6
6
  "commander",
@@ -19,33 +19,62 @@
19
19
  "url": "https://github.com/tj/commander.js.git"
20
20
  },
21
21
  "scripts": {
22
- "lint": "eslint index.js \"tests/**/*.js\"",
23
- "typescript-lint": "eslint typings/*.ts",
22
+ "lint": "npm run lint:javascript && npm run lint:typescript",
23
+ "lint:javascript": "eslint index.js esm.mjs \"lib/*.js\" \"tests/**/*.js\"",
24
+ "lint:typescript": "eslint typings/*.ts tests/*.ts",
24
25
  "test": "jest && npm run test-typings",
25
- "test-typings": "tsc -p tsconfig.json"
26
+ "test-esm": "node ./tests/esm-imports-test.mjs",
27
+ "test-typings": "tsd",
28
+ "typescript-checkJS": "tsc --allowJS --checkJS index.js lib/*.js --noEmit",
29
+ "test-all": "npm run test && npm run lint && npm run typescript-checkJS && npm run test-esm"
26
30
  },
27
- "main": "index",
28
31
  "files": [
29
32
  "index.js",
30
- "typings/index.d.ts"
33
+ "lib/*.js",
34
+ "esm.mjs",
35
+ "typings/index.d.ts",
36
+ "package-support.json"
31
37
  ],
32
- "dependencies": {},
38
+ "type": "commonjs",
39
+ "main": "./index.js",
40
+ "exports": {
41
+ ".": {
42
+ "types": "./typings/index.d.ts",
43
+ "require": "./index.js",
44
+ "import": "./esm.mjs"
45
+ },
46
+ "./esm.mjs": "./esm.mjs"
47
+ },
33
48
  "devDependencies": {
34
- "@types/jest": "^26.0.15",
35
- "@types/node": "^14.14.2",
36
- "@typescript-eslint/eslint-plugin": "^4.5.0",
37
- "eslint": "^7.11.0",
38
- "eslint-config-standard-with-typescript": "^19.0.1",
39
- "eslint-plugin-jest": "^24.1.0",
40
- "jest": "^26.6.0",
41
- "standard": "^15.0.0",
42
- "typescript": "^4.0.3"
49
+ "@types/jest": "^29.2.4",
50
+ "@types/node": "^18.11.18",
51
+ "@typescript-eslint/eslint-plugin": "^5.47.1",
52
+ "@typescript-eslint/parser": "^5.47.1",
53
+ "eslint": "^8.30.0",
54
+ "eslint-config-standard": "^17.0.0",
55
+ "eslint-config-standard-with-typescript": "^24.0.0",
56
+ "eslint-plugin-import": "^2.26.0",
57
+ "eslint-plugin-jest": "^27.1.7",
58
+ "eslint-plugin-n": "^15.6.0",
59
+ "eslint-plugin-promise": "^6.1.1",
60
+ "jest": "^29.3.1",
61
+ "ts-jest": "^29.0.3",
62
+ "tsd": "^0.25.0",
63
+ "typescript": "^4.9.4"
43
64
  },
44
- "typings": "typings/index.d.ts",
65
+ "types": "typings/index.d.ts",
45
66
  "jest": {
46
- "collectCoverage": true
67
+ "testEnvironment": "node",
68
+ "collectCoverage": true,
69
+ "transform": {
70
+ "^.+\\.tsx?$": "ts-jest"
71
+ },
72
+ "testPathIgnorePatterns": [
73
+ "/node_modules/"
74
+ ]
47
75
  },
48
76
  "engines": {
49
- "node": ">= 6"
50
- }
77
+ "node": ">=14"
78
+ },
79
+ "support": true
51
80
  }