@yamato-daiwa/es-extensions-nodejs 0.0.2 → 0.10.0-alpha.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 (44) hide show
  1. package/Distributable/ConsoleApplicationLogger.d.ts +68 -0
  2. package/Distributable/ConsoleApplicationLogger.js +435 -0
  3. package/Distributable/ConsoleCommandsParser/ConsoleCommandsParser.d.ts +289 -0
  4. package/Distributable/ConsoleCommandsParser/ConsoleCommandsParser.js +702 -0
  5. package/Distributable/ConsoleCommandsParser/ConsoleCommandsParserLocalization.english.d.ts +4 -0
  6. package/Distributable/ConsoleCommandsParser/ConsoleCommandsParserLocalization.english.js +118 -0
  7. package/Distributable/DateTime/NodeJS_Timer.d.ts +21 -7
  8. package/Distributable/DateTime/NodeJS_Timer.js +61 -13
  9. package/Distributable/DotEnvFilesGenerator.d.ts +13 -0
  10. package/Distributable/DotEnvFilesGenerator.js +21 -0
  11. package/Distributable/Errors/FileNotFoundError/FileNotFoundError.d.ts +25 -0
  12. package/Distributable/Errors/FileNotFoundError/FileNotFoundError.js +21 -0
  13. package/Distributable/Errors/FileNotFoundError/FileNotFoundErrorLocalization.english.d.ts +3 -0
  14. package/Distributable/Errors/FileNotFoundError/FileNotFoundErrorLocalization.english.js +7 -0
  15. package/Distributable/Errors/InvalidConsoleCommand/InvalidConsoleCommandError.d.ts +25 -0
  16. package/Distributable/Errors/InvalidConsoleCommand/InvalidConsoleCommandError.js +21 -0
  17. package/Distributable/Errors/InvalidConsoleCommand/InvalidConsoleCommandErrorLocalization.english.d.ts +3 -0
  18. package/Distributable/Errors/InvalidConsoleCommand/InvalidConsoleCommandErrorLocalization.english.js +7 -0
  19. package/Distributable/Errors/PathRefersToDirectoryNotFileError/PathRefersToDirectoryNotFileError.d.ts +25 -0
  20. package/Distributable/Errors/PathRefersToDirectoryNotFileError/PathRefersToDirectoryNotFileError.js +21 -0
  21. package/Distributable/Errors/PathRefersToDirectoryNotFileError/PathRefersToDirectoryNotFileErrorLocalization.english.d.ts +3 -0
  22. package/Distributable/Errors/PathRefersToDirectoryNotFileError/PathRefersToDirectoryNotFileErrorLocalization.english.js +7 -0
  23. package/Distributable/ImprovedFileSystem.d.ts +43 -0
  24. package/Distributable/ImprovedFileSystem.js +142 -0
  25. package/Distributable/ImprovedGlob.d.ts +57 -0
  26. package/Distributable/ImprovedGlob.js +275 -0
  27. package/Distributable/ImprovedPath/ImprovedPath.d.ts +59 -0
  28. package/Distributable/ImprovedPath/ImprovedPath.js +224 -0
  29. package/Distributable/ObjectDataFilesProcessor/ObjectDataFilesProcessor.d.ts +55 -0
  30. package/Distributable/ObjectDataFilesProcessor/ObjectDataFilesProcessor.js +228 -0
  31. package/Distributable/ObjectDataFilesProcessor/ObjectDataFilesProcessorLocalization.english.d.ts +4 -0
  32. package/Distributable/ObjectDataFilesProcessor/ObjectDataFilesProcessorLocalization.english.js +11 -0
  33. package/Distributable/Temporary/appendCharacterIfItDoesNotPresentInLastPosition.d.ts +4 -0
  34. package/Distributable/Temporary/appendCharacterIfItDoesNotPresentInLastPosition.js +7 -0
  35. package/Distributable/Temporary/extractLastExtensionOfFileName.d.ts +9 -0
  36. package/Distributable/Temporary/extractLastExtensionOfFileName.js +21 -0
  37. package/Distributable/index.d.ts +22 -7
  38. package/Distributable/index.js +45 -7
  39. package/Distributable/isErrnoException.d.ts +2 -0
  40. package/Distributable/isErrnoException.js +12 -0
  41. package/LICENSE +1 -1
  42. package/README.md +51 -3
  43. package/package.json +45 -15
  44. package/tsconfig.test.json +0 -4
@@ -0,0 +1,702 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const json5_1 = __importDefault(require("json5"));
7
+ const es_extensions_1 = require("@yamato-daiwa/es-extensions");
8
+ const InvalidConsoleCommandError_1 = __importDefault(require("../Errors/InvalidConsoleCommand/InvalidConsoleCommandError"));
9
+ const ConsoleCommandsParserLocalization_english_1 = __importDefault(require("./ConsoleCommandsParserLocalization.english"));
10
+ class ConsoleCommandsParser {
11
+ static localization = ConsoleCommandsParserLocalization_english_1.default;
12
+ static MINIMAL_ARGUMENTS_COUNT_IN_VALID_CONSOLE_COMMAND = 2;
13
+ static PARSING_METHOD_INVOCATION_EXPRESSION = "ConsoleCommandsParser.parse(commandLineInterfaceSpecification, argumentsVector)";
14
+ applicationName;
15
+ targetCommandPhrase;
16
+ targetCommandPhraseDescription;
17
+ isTargetCommandPhraseDefault;
18
+ targetCommandOptions;
19
+ targetCommandOptionsWhichHasNotBeenProcessedYet = [];
20
+ targetCommandOptionsSpecification;
21
+ static parse(commandLineInterfaceSpecification, argumentsVector = process.argv) {
22
+ ConsoleCommandsParser.validateCommandLineInterfaceSpecification(commandLineInterfaceSpecification);
23
+ if (!Array.isArray(argumentsVector)) {
24
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
25
+ errorInstance: new InvalidConsoleCommandError_1.default({
26
+ applicationName: commandLineInterfaceSpecification.applicationName,
27
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.argumentsVectorIsNotArray.
28
+ generate({ stringifiedActualValueOfArgumentsVector: (0, es_extensions_1.stringifyAndFormatArbitraryValue)(argumentsVector) })
29
+ }),
30
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
31
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
32
+ });
33
+ }
34
+ if (argumentsVector.length < ConsoleCommandsParser.MINIMAL_ARGUMENTS_COUNT_IN_VALID_CONSOLE_COMMAND) {
35
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
36
+ errorInstance: new InvalidConsoleCommandError_1.default({
37
+ applicationName: commandLineInterfaceSpecification.applicationName,
38
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.
39
+ argumentsVectorHasNotEnoughElements.generate({
40
+ minimalElementsCountInArgumentsVector: ConsoleCommandsParser.MINIMAL_ARGUMENTS_COUNT_IN_VALID_CONSOLE_COMMAND,
41
+ actualElementsCountInArgumentsVector: argumentsVector.length,
42
+ stringifiedArgumentsVector: (0, es_extensions_1.stringifyAndFormatArbitraryValue)(argumentsVector)
43
+ })
44
+ }),
45
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
46
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
47
+ });
48
+ }
49
+ const consciouslyInputtedArguments = [];
50
+ const nonStringArguments = [];
51
+ let NodeJS_InterpreterAbsolutePath = "";
52
+ let executableFileAbsolutePath = "";
53
+ for (const [index, argument] of argumentsVector.entries()) {
54
+ if (!(0, es_extensions_1.isString)(argument)) {
55
+ nonStringArguments.push(argument);
56
+ continue;
57
+ }
58
+ switch (index) {
59
+ case 0: {
60
+ NodeJS_InterpreterAbsolutePath = argument;
61
+ break;
62
+ }
63
+ case 1: {
64
+ executableFileAbsolutePath = argument;
65
+ break;
66
+ }
67
+ default: {
68
+ consciouslyInputtedArguments.push(argument);
69
+ }
70
+ }
71
+ }
72
+ if (nonStringArguments.length > 0) {
73
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
74
+ errorInstance: new InvalidConsoleCommandError_1.default({
75
+ applicationName: commandLineInterfaceSpecification.applicationName,
76
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.
77
+ argumentsVectorHasNonStringElements.generate({
78
+ stringifiedFormattedNonStringArguments: (0, es_extensions_1.stringifyAndFormatArbitraryValue)(nonStringArguments)
79
+ })
80
+ }),
81
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
82
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
83
+ });
84
+ }
85
+ const dataHoldingSelfInstance = new ConsoleCommandsParser({
86
+ commandLineInterfaceSpecification, consciouslyInputtedArguments
87
+ });
88
+ return {
89
+ NodeJS_InterpreterAbsolutePath,
90
+ executableFileAbsolutePath,
91
+ phrase: dataHoldingSelfInstance.targetCommandPhrase,
92
+ ...dataHoldingSelfInstance.parseOptionsAndParameters()
93
+ };
94
+ }
95
+ static generateFullHelpReference(commandLineInterfaceSpecification) {
96
+ ConsoleCommandsParser.validateCommandLineInterfaceSpecification(commandLineInterfaceSpecification);
97
+ const textSegments = [];
98
+ if ((0, es_extensions_1.isNonEmptyString)(commandLineInterfaceSpecification.applicationDescription)) {
99
+ textSegments.push(commandLineInterfaceSpecification.applicationDescription);
100
+ }
101
+ const commandPhrasesReferences = [];
102
+ for (const [commandPhrase, commandPhraseSpecification] of Object.entries(commandLineInterfaceSpecification.commandPhrases)) {
103
+ commandPhrasesReferences.push(this.generateSingleCommandPhraseHelpReference({
104
+ commandPhrase,
105
+ commandPhraseSpecification: { ...commandPhraseSpecification, isDefault: commandPhraseSpecification.isDefault === true }
106
+ }));
107
+ }
108
+ textSegments.push("\n\n", commandPhrasesReferences.join("\n\n"));
109
+ return textSegments.join("");
110
+ }
111
+ constructor({ commandLineInterfaceSpecification, consciouslyInputtedArguments }) {
112
+ const helpReference = ConsoleCommandsParser.generateFullHelpReference(commandLineInterfaceSpecification);
113
+ const firstConsciouslyInputtedArgument = consciouslyInputtedArguments[0];
114
+ let defaultCommandPhrase;
115
+ for (const [commandPhrase, commandPhraseSpecification] of Object.entries(commandLineInterfaceSpecification.commandPhrases)) {
116
+ if (commandPhraseSpecification.isDefault === true) {
117
+ defaultCommandPhrase = { name: commandPhrase, specification: commandPhraseSpecification };
118
+ break;
119
+ }
120
+ }
121
+ let targetCommandPhrase;
122
+ let targetCommandPhraseDescription;
123
+ let targetCommandOptions;
124
+ let optionsSpecificationForRecognizedCommandPhrase;
125
+ if ((0, es_extensions_1.isUndefined)(firstConsciouslyInputtedArgument)) {
126
+ if ((0, es_extensions_1.isUndefined)(defaultCommandPhrase)) {
127
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
128
+ errorInstance: new InvalidConsoleCommandError_1.default({
129
+ applicationName: commandLineInterfaceSpecification.applicationName,
130
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.
131
+ noDefaultCommandPhraseAvailable.generate({ helpReference })
132
+ }),
133
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
134
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
135
+ });
136
+ }
137
+ targetCommandPhrase = defaultCommandPhrase.name;
138
+ targetCommandPhraseDescription = defaultCommandPhrase.specification.description;
139
+ optionsSpecificationForRecognizedCommandPhrase = defaultCommandPhrase.specification.options ?? {};
140
+ }
141
+ else if (ConsoleCommandsParser.isCommandArgumentTheOption(firstConsciouslyInputtedArgument)) {
142
+ if ((0, es_extensions_1.isUndefined)(defaultCommandPhrase)) {
143
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
144
+ errorInstance: new InvalidConsoleCommandError_1.default({
145
+ applicationName: commandLineInterfaceSpecification.applicationName,
146
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.
147
+ noDefaultCommandPhraseAvailable.generate({ helpReference })
148
+ }),
149
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
150
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
151
+ });
152
+ }
153
+ targetCommandPhrase = defaultCommandPhrase.name;
154
+ targetCommandPhraseDescription = defaultCommandPhrase.specification.description;
155
+ optionsSpecificationForRecognizedCommandPhrase = defaultCommandPhrase.specification.options ?? {};
156
+ targetCommandOptions = [...consciouslyInputtedArguments];
157
+ }
158
+ else {
159
+ if ((0, es_extensions_1.isUndefined)(commandLineInterfaceSpecification.commandPhrases)) {
160
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
161
+ errorInstance: new InvalidConsoleCommandError_1.default({
162
+ applicationName: commandLineInterfaceSpecification.applicationName,
163
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.
164
+ firstParameterLooksLikeCommandPhraseWhileNoCommandPhrasesAvailable.generate({
165
+ commandPhraseLikeArgument: firstConsciouslyInputtedArgument, helpReference
166
+ })
167
+ }),
168
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
169
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
170
+ });
171
+ }
172
+ targetCommandPhrase = firstConsciouslyInputtedArgument;
173
+ targetCommandOptions = consciouslyInputtedArguments.slice(1);
174
+ for (const [commandPhrase, commandPhraseSpecification] of Object.entries(commandLineInterfaceSpecification.commandPhrases)) {
175
+ if (commandPhrase === targetCommandPhrase) {
176
+ targetCommandPhraseDescription = commandPhraseSpecification.description;
177
+ optionsSpecificationForRecognizedCommandPhrase = commandPhraseSpecification.options ?? {};
178
+ break;
179
+ }
180
+ }
181
+ if ((0, es_extensions_1.isUndefined)(optionsSpecificationForRecognizedCommandPhrase)) {
182
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
183
+ errorInstance: new InvalidConsoleCommandError_1.default({
184
+ applicationName: commandLineInterfaceSpecification.applicationName,
185
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.unknownCommandPhrase.generate({
186
+ inputtedCommandPhrase: targetCommandPhrase, helpReference
187
+ })
188
+ }),
189
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
190
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
191
+ });
192
+ }
193
+ }
194
+ this.applicationName = commandLineInterfaceSpecification.applicationName;
195
+ this.targetCommandPhrase = targetCommandPhrase;
196
+ this.targetCommandPhraseDescription = targetCommandPhraseDescription;
197
+ this.isTargetCommandPhraseDefault = this.targetCommandPhrase === defaultCommandPhrase?.name;
198
+ this.targetCommandOptions = targetCommandOptions;
199
+ this.targetCommandOptionsSpecification = optionsSpecificationForRecognizedCommandPhrase;
200
+ }
201
+ parseOptionsAndParameters() {
202
+ if ((0, es_extensions_1.isUndefined)(this.targetCommandOptionsSpecification)) {
203
+ return {};
204
+ }
205
+ const parsedOptions = {};
206
+ this.targetCommandOptionsWhichHasNotBeenProcessedYet.push(...this.targetCommandOptions ?? []);
207
+ for (const [optionKey, optionSpecification] of Object.entries(this.targetCommandOptionsSpecification)) {
208
+ const optionKeyWithoutLeading2NDashes = optionKey.startsWith("--") ? optionKey.slice(2) : optionKey;
209
+ const optionKeyWithLeading2NDashes = `--${optionKeyWithoutLeading2NDashes}`;
210
+ const optionName = optionSpecification.newName ?? optionKeyWithoutLeading2NDashes;
211
+ let shortcutWithLeadingNDash;
212
+ if ((0, es_extensions_1.isNotUndefined)(optionSpecification.shortcut)) {
213
+ shortcutWithLeadingNDash = optionSpecification.shortcut.startsWith("-") ?
214
+ optionSpecification.shortcut : `-${optionSpecification.shortcut}`;
215
+ }
216
+ const arrayIndexOfCurrentOptionKey = this.targetCommandOptionsWhichHasNotBeenProcessedYet.findIndex((commandOption) => commandOption === optionKeyWithLeading2NDashes || commandOption === shortcutWithLeadingNDash);
217
+ const hasCurrentOptionBeenSpecified = arrayIndexOfCurrentOptionKey !== -1;
218
+ if (optionSpecification.type === ConsoleCommandsParser.ParametersTypes.boolean) {
219
+ if (hasCurrentOptionBeenSpecified) {
220
+ this.targetCommandOptionsWhichHasNotBeenProcessedYet.splice(arrayIndexOfCurrentOptionKey, 1);
221
+ }
222
+ parsedOptions[optionName] = hasCurrentOptionBeenSpecified;
223
+ continue;
224
+ }
225
+ if (!hasCurrentOptionBeenSpecified) {
226
+ if ("defaultValue" in optionSpecification) {
227
+ parsedOptions[optionName] = optionSpecification.defaultValue;
228
+ continue;
229
+ }
230
+ if ("required" in optionSpecification && optionSpecification.required) {
231
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
232
+ errorInstance: new InvalidConsoleCommandError_1.default({
233
+ applicationName: this.applicationName,
234
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.requiredOptionKeyIsMissing.generate({
235
+ commandPhrase: this.targetCommandPhrase,
236
+ isDefaultCommandPhrase: this.isTargetCommandPhraseDefault,
237
+ missingOptionKey: optionKeyWithLeading2NDashes,
238
+ commandHelpReference: ConsoleCommandsParser.generateSingleCommandPhraseHelpReference({
239
+ commandPhrase: this.targetCommandPhrase,
240
+ commandPhraseSpecification: {
241
+ isDefault: this.isTargetCommandPhraseDefault,
242
+ description: this.targetCommandPhraseDescription,
243
+ options: this.targetCommandOptionsSpecification
244
+ }
245
+ })
246
+ })
247
+ }),
248
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
249
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
250
+ });
251
+ }
252
+ continue;
253
+ }
254
+ const potentialValueOfCurrentOption = this.targetCommandOptionsWhichHasNotBeenProcessedYet[arrayIndexOfCurrentOptionKey + 1];
255
+ if ((0, es_extensions_1.isUndefined)(potentialValueOfCurrentOption)) {
256
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
257
+ errorInstance: new InvalidConsoleCommandError_1.default({
258
+ applicationName: this.applicationName,
259
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.
260
+ noValueFollowingTheKeyOfNonBooleanOption.generate({
261
+ targetOptionKey: optionKeyWithLeading2NDashes,
262
+ commandHelpReference: ConsoleCommandsParser.generateSingleCommandPhraseHelpReference({
263
+ commandPhrase: this.targetCommandPhrase,
264
+ commandPhraseSpecification: {
265
+ isDefault: this.isTargetCommandPhraseDefault,
266
+ description: this.targetCommandPhraseDescription,
267
+ options: this.targetCommandOptionsSpecification
268
+ }
269
+ })
270
+ })
271
+ }),
272
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
273
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
274
+ });
275
+ }
276
+ switch (optionSpecification.type) {
277
+ case ConsoleCommandsParser.ParametersTypes.string: {
278
+ parsedOptions[optionName] = this.processStringTypeOptionValue({
279
+ targetOptionRawValue: potentialValueOfCurrentOption,
280
+ optionKeyWithLeading2NDashes,
281
+ optionSpecification
282
+ });
283
+ break;
284
+ }
285
+ case ConsoleCommandsParser.ParametersTypes.number: {
286
+ parsedOptions[optionName] = this.processNumberTypeOptionValue({
287
+ targetOptionRawValue: potentialValueOfCurrentOption,
288
+ optionKeyWithLeading2NDashes,
289
+ optionSpecification
290
+ });
291
+ break;
292
+ }
293
+ case ConsoleCommandsParser.ParametersTypes.JSON5: {
294
+ const targetParsedJSON5_ParameterValue = this.
295
+ extractAndValidateParsedJSON5_ParameterValue({
296
+ targetOptionRawValue: potentialValueOfCurrentOption,
297
+ optionKeyWithLeading2NDashes,
298
+ optionSpecification
299
+ });
300
+ if ((0, es_extensions_1.isNotUndefined)(targetParsedJSON5_ParameterValue)) {
301
+ parsedOptions[optionName] = targetParsedJSON5_ParameterValue;
302
+ }
303
+ break;
304
+ }
305
+ default: {
306
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
307
+ errorType: "InvalidConsoleCommandOptionSpecification",
308
+ title: "Invalid console command option specification",
309
+ description: ConsoleCommandsParser.localization.errorsMessages.invalidCommandOptionTypeAtSpecification.generate({
310
+ optionName, typeName: String(optionSpecification.type)
311
+ }),
312
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
313
+ });
314
+ }
315
+ }
316
+ this.targetCommandOptionsWhichHasNotBeenProcessedYet.splice(arrayIndexOfCurrentOptionKey, 2);
317
+ }
318
+ if (this.targetCommandOptionsWhichHasNotBeenProcessedYet.length > 0) {
319
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
320
+ errorInstance: new InvalidConsoleCommandError_1.default({
321
+ applicationName: this.applicationName,
322
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.unknownOptionsFoundForSpecificCommand.generate({
323
+ commandPhrase: this.targetCommandPhrase,
324
+ formattedUnknownOptions: (0, es_extensions_1.stringifyAndFormatArbitraryValue)(this.targetCommandOptionsWhichHasNotBeenProcessedYet),
325
+ commandHelpReference: ConsoleCommandsParser.generateSingleCommandPhraseHelpReference({
326
+ commandPhrase: this.targetCommandPhrase,
327
+ commandPhraseSpecification: {
328
+ isDefault: this.isTargetCommandPhraseDefault,
329
+ description: this.targetCommandPhraseDescription,
330
+ options: this.targetCommandOptionsSpecification
331
+ }
332
+ })
333
+ })
334
+ }),
335
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
336
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
337
+ });
338
+ }
339
+ return parsedOptions;
340
+ }
341
+ processStringTypeOptionValue({ targetOptionRawValue, optionKeyWithLeading2NDashes, optionSpecification }) {
342
+ const { allowedAlternatives } = optionSpecification;
343
+ if ((0, es_extensions_1.isNonEmptyArray)(allowedAlternatives) && !allowedAlternatives.includes(targetOptionRawValue)) {
344
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
345
+ errorInstance: new InvalidConsoleCommandError_1.default({
346
+ applicationName: this.applicationName,
347
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.
348
+ optionValueIsNotAmongAllowedAlternatives.generate({
349
+ targetOptionKey: optionKeyWithLeading2NDashes,
350
+ actualOptionValue: targetOptionRawValue,
351
+ commandHelpReference: ConsoleCommandsParser.generateSingleCommandPhraseHelpReference({
352
+ commandPhrase: this.targetCommandPhrase,
353
+ commandPhraseSpecification: {
354
+ isDefault: this.isTargetCommandPhraseDefault,
355
+ description: this.targetCommandPhraseDescription,
356
+ options: this.targetCommandOptionsSpecification
357
+ }
358
+ })
359
+ })
360
+ }),
361
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
362
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
363
+ });
364
+ }
365
+ return targetOptionRawValue;
366
+ }
367
+ processNumberTypeOptionValue({ targetOptionRawValue, optionKeyWithLeading2NDashes, optionSpecification }) {
368
+ const { numbersSet, minimalValue, maximalValue } = optionSpecification;
369
+ const targetOptionParsedValue = Number(targetOptionRawValue);
370
+ if (Number.isNaN(targetOptionParsedValue)) {
371
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
372
+ errorInstance: new InvalidConsoleCommandError_1.default({
373
+ applicationName: this.applicationName,
374
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.unparsableNumericOptionValue.generate({
375
+ targetOptionKey: optionKeyWithLeading2NDashes,
376
+ actualOptionValue: targetOptionRawValue,
377
+ commandHelpReference: ConsoleCommandsParser.generateSingleCommandPhraseHelpReference({
378
+ commandPhrase: this.targetCommandPhrase,
379
+ commandPhraseSpecification: {
380
+ isDefault: this.isTargetCommandPhraseDefault,
381
+ description: this.targetCommandPhraseDescription,
382
+ options: this.targetCommandOptionsSpecification
383
+ }
384
+ })
385
+ })
386
+ }),
387
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
388
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
389
+ });
390
+ }
391
+ let isOptionValueMatchingWithExpectedNumberSet;
392
+ switch (numbersSet) {
393
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.naturalNumber: {
394
+ isOptionValueMatchingWithExpectedNumberSet = (0, es_extensions_1.isNaturalNumber)(targetOptionParsedValue);
395
+ break;
396
+ }
397
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.naturalNumberOrZero:
398
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.positiveIntegerOrZero: {
399
+ isOptionValueMatchingWithExpectedNumberSet = (0, es_extensions_1.isNaturalNumberOrZero)(targetOptionParsedValue);
400
+ break;
401
+ }
402
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.negativeInteger: {
403
+ isOptionValueMatchingWithExpectedNumberSet = (0, es_extensions_1.isNegativeInteger)(targetOptionParsedValue);
404
+ break;
405
+ }
406
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.negativeIntegerOrZero: {
407
+ isOptionValueMatchingWithExpectedNumberSet = (0, es_extensions_1.isNegativeIntegerOrZero)(targetOptionParsedValue);
408
+ break;
409
+ }
410
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.anyInteger: {
411
+ isOptionValueMatchingWithExpectedNumberSet = Number.isInteger(targetOptionParsedValue);
412
+ break;
413
+ }
414
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.positiveDecimalFraction: {
415
+ isOptionValueMatchingWithExpectedNumberSet = (0, es_extensions_1.isPositiveDecimalFraction)(targetOptionParsedValue);
416
+ break;
417
+ }
418
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.positiveDecimalFractionOrZero: {
419
+ isOptionValueMatchingWithExpectedNumberSet =
420
+ (0, es_extensions_1.isPositiveDecimalFraction)(targetOptionParsedValue) || targetOptionParsedValue === 0;
421
+ break;
422
+ }
423
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.negativeDecimalFraction: {
424
+ isOptionValueMatchingWithExpectedNumberSet = (0, es_extensions_1.isNegativeDecimalFraction)(targetOptionParsedValue);
425
+ break;
426
+ }
427
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.negativeDecimalFractionOrZero: {
428
+ isOptionValueMatchingWithExpectedNumberSet =
429
+ (0, es_extensions_1.isNegativeDecimalFraction)(targetOptionParsedValue) || targetOptionParsedValue === 0;
430
+ break;
431
+ }
432
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.anyDecimalFraction: {
433
+ isOptionValueMatchingWithExpectedNumberSet = (0, es_extensions_1.isDecimalFractionOfAnySign)(targetOptionParsedValue);
434
+ break;
435
+ }
436
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.anyDecimalFractionOrZero: {
437
+ isOptionValueMatchingWithExpectedNumberSet =
438
+ (0, es_extensions_1.isDecimalFractionOfAnySign)(targetOptionParsedValue) || targetOptionParsedValue === 0;
439
+ break;
440
+ }
441
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.anyRealNumber: {
442
+ isOptionValueMatchingWithExpectedNumberSet = true;
443
+ break;
444
+ }
445
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.positiveRealNumber: {
446
+ isOptionValueMatchingWithExpectedNumberSet = targetOptionParsedValue > 0;
447
+ break;
448
+ }
449
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.negativeRealNumber: {
450
+ isOptionValueMatchingWithExpectedNumberSet = targetOptionParsedValue < 0;
451
+ break;
452
+ }
453
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.positiveRealNumberOrZero: {
454
+ isOptionValueMatchingWithExpectedNumberSet = targetOptionParsedValue >= 0;
455
+ break;
456
+ }
457
+ case es_extensions_1.RawObjectDataProcessor.NumbersSets.negativeRealNumberOrZero: {
458
+ isOptionValueMatchingWithExpectedNumberSet = targetOptionParsedValue <= 0;
459
+ }
460
+ }
461
+ if (!isOptionValueMatchingWithExpectedNumberSet) {
462
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
463
+ errorInstance: new InvalidConsoleCommandError_1.default({
464
+ applicationName: this.applicationName,
465
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.
466
+ numericOptionValueIsNotBelongToExpectedNumbersSet.generate({
467
+ targetOptionKey: optionKeyWithLeading2NDashes,
468
+ expectedNumbersSet: numbersSet,
469
+ actualOptionValue: targetOptionRawValue,
470
+ commandHelpReference: ConsoleCommandsParser.generateSingleCommandPhraseHelpReference({
471
+ commandPhrase: this.targetCommandPhrase,
472
+ commandPhraseSpecification: {
473
+ isDefault: this.isTargetCommandPhraseDefault,
474
+ description: this.targetCommandPhraseDescription,
475
+ options: this.targetCommandOptionsSpecification
476
+ }
477
+ })
478
+ })
479
+ }),
480
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
481
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
482
+ });
483
+ }
484
+ if ((0, es_extensions_1.isNotUndefined)(minimalValue) && targetOptionParsedValue <= minimalValue) {
485
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
486
+ errorInstance: new InvalidConsoleCommandError_1.default({
487
+ applicationName: this.applicationName,
488
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.
489
+ numericValueIsSmallerThanRequiredMinimum.generate({
490
+ targetOptionKey: optionKeyWithLeading2NDashes,
491
+ requiredMinimum: minimalValue,
492
+ actualOptionValue: targetOptionRawValue,
493
+ commandHelpReference: ConsoleCommandsParser.generateSingleCommandPhraseHelpReference({
494
+ commandPhrase: this.targetCommandPhrase,
495
+ commandPhraseSpecification: {
496
+ isDefault: this.isTargetCommandPhraseDefault,
497
+ description: this.targetCommandPhraseDescription,
498
+ options: this.targetCommandOptionsSpecification
499
+ }
500
+ })
501
+ })
502
+ }),
503
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
504
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
505
+ });
506
+ }
507
+ if ((0, es_extensions_1.isNotUndefined)(maximalValue) && targetOptionParsedValue >= maximalValue) {
508
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
509
+ errorInstance: new InvalidConsoleCommandError_1.default({
510
+ applicationName: this.applicationName,
511
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.
512
+ numericValueIsGreaterThanAllowedMaximum.generate({
513
+ targetOptionKey: optionKeyWithLeading2NDashes,
514
+ allowedMaximum: maximalValue,
515
+ actualOptionValue: targetOptionRawValue,
516
+ commandHelpReference: ConsoleCommandsParser.generateSingleCommandPhraseHelpReference({
517
+ commandPhrase: this.targetCommandPhrase,
518
+ commandPhraseSpecification: {
519
+ isDefault: this.isTargetCommandPhraseDefault,
520
+ description: this.targetCommandPhraseDescription,
521
+ options: this.targetCommandOptionsSpecification
522
+ }
523
+ })
524
+ })
525
+ }),
526
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
527
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
528
+ });
529
+ }
530
+ return targetOptionParsedValue;
531
+ }
532
+ extractAndValidateParsedJSON5_ParameterValue({ targetOptionRawValue, optionKeyWithLeading2NDashes, optionSpecification }) {
533
+ let targetParameterParsedValue;
534
+ try {
535
+ targetParameterParsedValue = json5_1.default.parse(targetOptionRawValue);
536
+ }
537
+ catch (error) {
538
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
539
+ errorInstance: new InvalidConsoleCommandError_1.default({
540
+ applicationName: this.applicationName,
541
+ messageSpecificPart: ConsoleCommandsParser.localization.errorsMessages.malformedJSON5_Option.generate({
542
+ targetOptionKey: optionKeyWithLeading2NDashes,
543
+ commandHelpReference: ConsoleCommandsParser.generateSingleCommandPhraseHelpReference({
544
+ commandPhrase: this.targetCommandPhrase,
545
+ commandPhraseSpecification: {
546
+ isDefault: this.isTargetCommandPhraseDefault,
547
+ description: this.targetCommandPhraseDescription,
548
+ options: this.targetCommandOptionsSpecification
549
+ }
550
+ })
551
+ })
552
+ }),
553
+ title: InvalidConsoleCommandError_1.default.localization.defaultTitle,
554
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION,
555
+ innerError: error
556
+ });
557
+ }
558
+ const validationResult = es_extensions_1.RawObjectDataProcessor.
559
+ process(targetParameterParsedValue, {
560
+ nameForLogging: optionKeyWithLeading2NDashes,
561
+ subtype: es_extensions_1.RawObjectDataProcessor.ObjectSubtypes.fixedSchema,
562
+ properties: optionSpecification.validValueSpecification
563
+ });
564
+ if (validationResult.isRawDataInvalid) {
565
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
566
+ errorInstance: new es_extensions_1.InvalidExternalDataError({
567
+ customMessage: ConsoleCommandsParser.localization.errorsMessages.JSON5_OptionDoesNotMatchWithValidDataSchema.generate({
568
+ targetOptionKey: optionKeyWithLeading2NDashes,
569
+ formattedValidationErrorsMessages: es_extensions_1.RawObjectDataProcessor.
570
+ formatValidationErrorsList(validationResult.validationErrorsMessages)
571
+ })
572
+ }),
573
+ title: es_extensions_1.InvalidExternalDataError.localization.defaultTitle,
574
+ occurrenceLocation: ConsoleCommandsParser.PARSING_METHOD_INVOCATION_EXPRESSION
575
+ });
576
+ }
577
+ return validationResult.processedData;
578
+ }
579
+ static isCommandArgumentTheOption(consoleCommandArgument) {
580
+ return consoleCommandArgument.startsWith("-");
581
+ }
582
+ static generateSingleCommandPhraseHelpReference({ commandPhrase, commandPhraseSpecification }) {
583
+ const indentationCoordinator = new es_extensions_1.IndentationCoordinator();
584
+ const textSegments = [
585
+ `● ${commandPhrase}${(0, es_extensions_1.insertSubstring)(ConsoleCommandsParser.localization.helpReference.defaultCommandPhrase__parenthetical, {
586
+ condition: commandPhraseSpecification.isDefault,
587
+ modifier: (defaultCommandPhrase__parenthetical) => ` ${defaultCommandPhrase__parenthetical}`
588
+ })}\n`
589
+ ];
590
+ if ((0, es_extensions_1.isNonEmptyString)(commandPhraseSpecification.description)) {
591
+ textSegments.push(indentationCoordinator.insertIncrementedIndentWithoutUpdatingOfIndentationMultiplier() +
592
+ commandPhraseSpecification.description);
593
+ }
594
+ const commandOptions = commandPhraseSpecification.options ?? {};
595
+ if (Object.entries(commandOptions).length > 0) {
596
+ indentationCoordinator.incrementIndent();
597
+ textSegments.push(`\n\n${indentationCoordinator.insertIndent()}` +
598
+ `■ ${ConsoleCommandsParser.localization.helpReference.options}`);
599
+ indentationCoordinator.incrementIndent();
600
+ for (const [commandOptionKey, commandOptionSpecification] of Object.entries(commandOptions)) {
601
+ textSegments.push(ConsoleCommandsParser.generateSingleCommandOptionHelpReference({
602
+ commandOptionKey,
603
+ commandOptionSpecification,
604
+ indentationCoordinator
605
+ }));
606
+ }
607
+ indentationCoordinator.decrementIndent();
608
+ }
609
+ return textSegments.join("");
610
+ }
611
+ static generateSingleCommandOptionHelpReference({ commandOptionKey, commandOptionSpecification, indentationCoordinator }) {
612
+ const textSegments = [
613
+ `\n\n${indentationCoordinator.insertIndent()}--${commandOptionKey}` +
614
+ `\n${indentationCoordinator.incrementIndentAndInsert()}`
615
+ ];
616
+ const helpReferenceLocalization = ConsoleCommandsParser.localization.helpReference;
617
+ if ((0, es_extensions_1.isNonEmptyString)(commandOptionSpecification.description)) {
618
+ textSegments.push(`${commandOptionSpecification.description}\n`);
619
+ }
620
+ if ((0, es_extensions_1.isNotUndefined)(commandOptionSpecification.shortcut)) {
621
+ textSegments.push(`\n${indentationCoordinator.insertIndent()}` +
622
+ `◯ ${helpReferenceLocalization.shortcut}: -${commandOptionSpecification.shortcut}`);
623
+ }
624
+ textSegments.push(`\n${indentationCoordinator.insertIndent()}◯ ${helpReferenceLocalization.type}: ${commandOptionSpecification.type}`);
625
+ if ("required" in commandOptionSpecification) {
626
+ textSegments.push(`\n${indentationCoordinator.insertIndent()}◯ ${helpReferenceLocalization.isRequired}: ` +
627
+ (commandOptionSpecification.required ? helpReferenceLocalization.yes : helpReferenceLocalization.no));
628
+ }
629
+ else if ("defaultValue" in commandOptionSpecification) {
630
+ textSegments.push(`\n${indentationCoordinator.insertIndent()}◯ ${helpReferenceLocalization.defaultValue}: ` +
631
+ (0, es_extensions_1.stringifyAndFormatArbitraryValue)(commandOptionSpecification.defaultValue));
632
+ }
633
+ switch (commandOptionSpecification.type) {
634
+ case ConsoleCommandsParser.ParametersTypes.string: {
635
+ if ((0, es_extensions_1.isNonEmptyArray)(commandOptionSpecification.allowedAlternatives)) {
636
+ textSegments.push(`\n${indentationCoordinator.insertIndent()}◯ ${helpReferenceLocalization.allowedAlternatives}:`);
637
+ indentationCoordinator.incrementIndent();
638
+ textSegments.push(commandOptionSpecification.allowedAlternatives.
639
+ map((allowedAlternative) => `\n${indentationCoordinator.insertIndent()}◆ ${allowedAlternative}`).
640
+ join(""));
641
+ indentationCoordinator.decrementIndent();
642
+ }
643
+ break;
644
+ }
645
+ case ConsoleCommandsParser.ParametersTypes.number: {
646
+ textSegments.push(`\n${indentationCoordinator.insertIndent()}◯ ` +
647
+ `${helpReferenceLocalization.numbersSet.key}: ` +
648
+ `${helpReferenceLocalization.numbersSet.generateValue(commandOptionSpecification.numbersSet)} `);
649
+ if ((0, es_extensions_1.isNotUndefined)(commandOptionSpecification.minimalValue)) {
650
+ textSegments.push(`\n${indentationCoordinator.insertIndent()}◯ ` +
651
+ `${helpReferenceLocalization.minimalValue}: ${commandOptionSpecification.minimalValue}`);
652
+ }
653
+ if ((0, es_extensions_1.isNotUndefined)(commandOptionSpecification.maximalValue)) {
654
+ textSegments.push(`\n${indentationCoordinator.insertIndent()}◯ ` +
655
+ `${helpReferenceLocalization.maximalValue}: ${commandOptionSpecification.maximalValue}`);
656
+ }
657
+ break;
658
+ }
659
+ case ConsoleCommandsParser.ParametersTypes.JSON5: {
660
+ textSegments.push(`\n${indentationCoordinator.insertIndent()}◯ ` +
661
+ `${helpReferenceLocalization.objectTypeValuePropertiesSpecification}:\n`);
662
+ indentationCoordinator.incrementIndent();
663
+ textSegments.push(indentationCoordinator.addCurrentIndentationToEachLineOf((0, es_extensions_1.stringifyAndFormatArbitraryValue)(commandOptionSpecification.validValueSpecification.properties)));
664
+ indentationCoordinator.decrementIndent();
665
+ }
666
+ }
667
+ indentationCoordinator.decrementIndent();
668
+ return textSegments.join("");
669
+ }
670
+ static validateCommandLineInterfaceSpecification(commandLineInterfaceSpecification) {
671
+ const specificationsOfCommandsMarkedAsDefault = Object.entries(commandLineInterfaceSpecification.commandPhrases).
672
+ reduce((_specificationsOfCommandsMarkedAsDefault, [commandPhrase, commandPhraseSpecification]) => {
673
+ if (commandPhraseSpecification.isDefault === true) {
674
+ _specificationsOfCommandsMarkedAsDefault[commandPhrase] = commandPhraseSpecification;
675
+ }
676
+ return _specificationsOfCommandsMarkedAsDefault;
677
+ }, {});
678
+ if (Object.entries(specificationsOfCommandsMarkedAsDefault).length > 1) {
679
+ es_extensions_1.Logger.throwErrorWithFormattedMessage({
680
+ errorType: "InvalidConsoleCommandSpecification",
681
+ title: "Invalid console command specification",
682
+ description: ConsoleCommandsParser.localization.errorsMessages.moreThanOneCommandPhrasesMarkedAsDefault.generate({
683
+ formattedListOfCommandsPhrasesMarkedAsDefault: Object.
684
+ keys(specificationsOfCommandsMarkedAsDefault).
685
+ map((commandPhrase) => `● ${commandPhrase}`).
686
+ join("\n")
687
+ }),
688
+ occurrenceLocation: "ConsoleCommandsParser.validateCommandLineInterfaceSpecification(commandLineInterfaceSpecification)"
689
+ });
690
+ }
691
+ }
692
+ }
693
+ (function (ConsoleCommandsParser) {
694
+ let ParametersTypes;
695
+ (function (ParametersTypes) {
696
+ ParametersTypes["string"] = "string";
697
+ ParametersTypes["number"] = "number";
698
+ ParametersTypes["boolean"] = "boolean";
699
+ ParametersTypes["JSON5"] = "JSON5";
700
+ })(ParametersTypes = ConsoleCommandsParser.ParametersTypes || (ConsoleCommandsParser.ParametersTypes = {}));
701
+ })(ConsoleCommandsParser || (ConsoleCommandsParser = {}));
702
+ exports.default = ConsoleCommandsParser;