@villedemontreal/caporal 3.1.7
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.
- package/LICENSE +21 -0
- package/README.md +29 -0
- package/dist/src/__tests__/issue-163.spec.d.ts +2 -0
- package/dist/src/__tests__/issue-163.spec.d.ts.map +1 -0
- package/dist/src/__tests__/issue-163.spec.js +17 -0
- package/dist/src/__tests__/issue-163.spec.js.map +1 -0
- package/dist/src/argument/__tests__/argument.spec.d.ts +2 -0
- package/dist/src/argument/__tests__/argument.spec.d.ts.map +1 -0
- package/dist/src/argument/__tests__/argument.spec.js +85 -0
- package/dist/src/argument/__tests__/argument.spec.js.map +1 -0
- package/dist/src/argument/find.d.ts +8 -0
- package/dist/src/argument/find.d.ts.map +1 -0
- package/dist/src/argument/find.js +11 -0
- package/dist/src/argument/find.js.map +1 -0
- package/dist/src/argument/index.d.ts +13 -0
- package/dist/src/argument/index.d.ts.map +1 -0
- package/dist/src/argument/index.js +27 -0
- package/dist/src/argument/index.js.map +1 -0
- package/dist/src/argument/synopsis.d.ts +19 -0
- package/dist/src/argument/synopsis.d.ts.map +1 -0
- package/dist/src/argument/synopsis.js +39 -0
- package/dist/src/argument/synopsis.js.map +1 -0
- package/dist/src/argument/validate.d.ts +43 -0
- package/dist/src/argument/validate.d.ts.map +1 -0
- package/dist/src/argument/validate.js +127 -0
- package/dist/src/argument/validate.js.map +1 -0
- package/dist/src/autocomplete/__fixtures__/prog-autocomplete.d.ts +3 -0
- package/dist/src/autocomplete/__fixtures__/prog-autocomplete.d.ts.map +1 -0
- package/dist/src/autocomplete/__fixtures__/prog-autocomplete.js +24 -0
- package/dist/src/autocomplete/__fixtures__/prog-autocomplete.js.map +1 -0
- package/dist/src/autocomplete/__tests__/autocomplete.spec.d.ts +2 -0
- package/dist/src/autocomplete/__tests__/autocomplete.spec.d.ts.map +1 -0
- package/dist/src/autocomplete/__tests__/autocomplete.spec.js +119 -0
- package/dist/src/autocomplete/__tests__/autocomplete.spec.js.map +1 -0
- package/dist/src/autocomplete/index.d.ts +20 -0
- package/dist/src/autocomplete/index.d.ts.map +1 -0
- package/dist/src/autocomplete/index.js +172 -0
- package/dist/src/autocomplete/index.js.map +1 -0
- package/dist/src/autocomplete/types.d.ts +26 -0
- package/dist/src/autocomplete/types.d.ts.map +1 -0
- package/dist/src/autocomplete/types.js +3 -0
- package/dist/src/autocomplete/types.js.map +1 -0
- package/dist/src/command/__fixtures__/example-cmd.d.ts +8 -0
- package/dist/src/command/__fixtures__/example-cmd.d.ts.map +1 -0
- package/dist/src/command/__fixtures__/example-cmd.js +10 -0
- package/dist/src/command/__fixtures__/example-cmd.js.map +1 -0
- package/dist/src/command/__tests__/command.spec.d.ts +2 -0
- package/dist/src/command/__tests__/command.spec.d.ts.map +1 -0
- package/dist/src/command/__tests__/command.spec.js +510 -0
- package/dist/src/command/__tests__/command.spec.js.map +1 -0
- package/dist/src/command/__tests__/find.spec.d.ts +2 -0
- package/dist/src/command/__tests__/find.spec.d.ts.map +1 -0
- package/dist/src/command/__tests__/find.spec.js +31 -0
- package/dist/src/command/__tests__/find.spec.js.map +1 -0
- package/dist/src/command/__tests__/import.spec.d.ts +2 -0
- package/dist/src/command/__tests__/import.spec.d.ts.map +1 -0
- package/dist/src/command/__tests__/import.spec.js +17 -0
- package/dist/src/command/__tests__/import.spec.js.map +1 -0
- package/dist/src/command/__tests__/scan.spec.d.ts +2 -0
- package/dist/src/command/__tests__/scan.spec.d.ts.map +1 -0
- package/dist/src/command/__tests__/scan.spec.js +20 -0
- package/dist/src/command/__tests__/scan.spec.js.map +1 -0
- package/dist/src/command/find.d.ts +8 -0
- package/dist/src/command/find.d.ts.map +1 -0
- package/dist/src/command/find.js +47 -0
- package/dist/src/command/find.js.map +1 -0
- package/dist/src/command/import.d.ts +7 -0
- package/dist/src/command/import.d.ts.map +1 -0
- package/dist/src/command/import.js +47 -0
- package/dist/src/command/import.js.map +1 -0
- package/dist/src/command/index.d.ts +211 -0
- package/dist/src/command/index.d.ts.map +1 -0
- package/dist/src/command/index.js +374 -0
- package/dist/src/command/index.js.map +1 -0
- package/dist/src/command/scan.d.ts +4 -0
- package/dist/src/command/scan.d.ts.map +1 -0
- package/dist/src/command/scan.js +31 -0
- package/dist/src/command/scan.js.map +1 -0
- package/dist/src/command/validate-call.d.ts +8 -0
- package/dist/src/command/validate-call.d.ts.map +1 -0
- package/dist/src/command/validate-call.js +13 -0
- package/dist/src/command/validate-call.js.map +1 -0
- package/dist/src/config/index.d.ts +7 -0
- package/dist/src/config/index.d.ts.map +1 -0
- package/dist/src/config/index.js +24 -0
- package/dist/src/config/index.js.map +1 -0
- package/dist/src/error/__tests__/fatal.spec.d.ts +2 -0
- package/dist/src/error/__tests__/fatal.spec.d.ts.map +1 -0
- package/dist/src/error/__tests__/fatal.spec.js +34 -0
- package/dist/src/error/__tests__/fatal.spec.js.map +1 -0
- package/dist/src/error/action.d.ts +9 -0
- package/dist/src/error/action.d.ts.map +1 -0
- package/dist/src/error/action.js +16 -0
- package/dist/src/error/action.js.map +1 -0
- package/dist/src/error/base.d.ts +11 -0
- package/dist/src/error/base.d.ts.map +1 -0
- package/dist/src/error/base.js +18 -0
- package/dist/src/error/base.js.map +1 -0
- package/dist/src/error/fatal.d.ts +10 -0
- package/dist/src/error/fatal.d.ts.map +1 -0
- package/dist/src/error/fatal.js +27 -0
- package/dist/src/error/fatal.js.map +1 -0
- package/dist/src/error/index.d.ts +18 -0
- package/dist/src/error/index.d.ts.map +1 -0
- package/dist/src/error/index.js +34 -0
- package/dist/src/error/index.js.map +1 -0
- package/dist/src/error/invalid-validator.d.ts +10 -0
- package/dist/src/error/invalid-validator.d.ts.map +1 -0
- package/dist/src/error/invalid-validator.js +15 -0
- package/dist/src/error/invalid-validator.js.map +1 -0
- package/dist/src/error/missing-argument.d.ts +11 -0
- package/dist/src/error/missing-argument.d.ts.map +1 -0
- package/dist/src/error/missing-argument.js +20 -0
- package/dist/src/error/missing-argument.js.map +1 -0
- package/dist/src/error/missing-flag.d.ts +11 -0
- package/dist/src/error/missing-flag.d.ts.map +1 -0
- package/dist/src/error/missing-flag.js +20 -0
- package/dist/src/error/missing-flag.js.map +1 -0
- package/dist/src/error/multi-validation.d.ts +10 -0
- package/dist/src/error/multi-validation.d.ts.map +1 -0
- package/dist/src/error/multi-validation.js +26 -0
- package/dist/src/error/multi-validation.js.map +1 -0
- package/dist/src/error/no-action.d.ts +10 -0
- package/dist/src/error/no-action.d.ts.map +1 -0
- package/dist/src/error/no-action.js +22 -0
- package/dist/src/error/no-action.js.map +1 -0
- package/dist/src/error/option-synopsis-syntax.d.ts +9 -0
- package/dist/src/error/option-synopsis-syntax.d.ts.map +1 -0
- package/dist/src/error/option-synopsis-syntax.js +15 -0
- package/dist/src/error/option-synopsis-syntax.js.map +1 -0
- package/dist/src/error/too-many-arguments.d.ts +11 -0
- package/dist/src/error/too-many-arguments.d.ts.map +1 -0
- package/dist/src/error/too-many-arguments.js +25 -0
- package/dist/src/error/too-many-arguments.js.map +1 -0
- package/dist/src/error/unknown-command.d.ts +13 -0
- package/dist/src/error/unknown-command.d.ts.map +1 -0
- package/dist/src/error/unknown-command.js +44 -0
- package/dist/src/error/unknown-command.js.map +1 -0
- package/dist/src/error/unknown-option.d.ts +13 -0
- package/dist/src/error/unknown-option.d.ts.map +1 -0
- package/dist/src/error/unknown-option.js +40 -0
- package/dist/src/error/unknown-option.js.map +1 -0
- package/dist/src/error/validation.d.ts +17 -0
- package/dist/src/error/validation.d.ts.map +1 -0
- package/dist/src/error/validation.js +49 -0
- package/dist/src/error/validation.js.map +1 -0
- package/dist/src/help/__tests__/help.spec.d.ts +2 -0
- package/dist/src/help/__tests__/help.spec.d.ts.map +1 -0
- package/dist/src/help/__tests__/help.spec.js +130 -0
- package/dist/src/help/__tests__/help.spec.js.map +1 -0
- package/dist/src/help/__tests__/utils.spec.d.ts +2 -0
- package/dist/src/help/__tests__/utils.spec.d.ts.map +1 -0
- package/dist/src/help/__tests__/utils.spec.js +16 -0
- package/dist/src/help/__tests__/utils.spec.js.map +1 -0
- package/dist/src/help/index.d.ts +47 -0
- package/dist/src/help/index.d.ts.map +1 -0
- package/dist/src/help/index.js +131 -0
- package/dist/src/help/index.js.map +1 -0
- package/dist/src/help/templates/command.d.ts +7 -0
- package/dist/src/help/templates/command.d.ts.map +1 -0
- package/dist/src/help/templates/command.js +23 -0
- package/dist/src/help/templates/command.js.map +1 -0
- package/dist/src/help/templates/custom.d.ts +7 -0
- package/dist/src/help/templates/custom.d.ts.map +1 -0
- package/dist/src/help/templates/custom.js +24 -0
- package/dist/src/help/templates/custom.js.map +1 -0
- package/dist/src/help/templates/header.d.ts +7 -0
- package/dist/src/help/templates/header.d.ts.map +1 -0
- package/dist/src/help/templates/header.js +16 -0
- package/dist/src/help/templates/header.js.map +1 -0
- package/dist/src/help/templates/index.d.ts +10 -0
- package/dist/src/help/templates/index.d.ts.map +1 -0
- package/dist/src/help/templates/index.js +26 -0
- package/dist/src/help/templates/index.js.map +1 -0
- package/dist/src/help/templates/program.d.ts +7 -0
- package/dist/src/help/templates/program.d.ts.map +1 -0
- package/dist/src/help/templates/program.js +18 -0
- package/dist/src/help/templates/program.js.map +1 -0
- package/dist/src/help/templates/usage.d.ts +7 -0
- package/dist/src/help/templates/usage.d.ts.map +1 -0
- package/dist/src/help/templates/usage.js +19 -0
- package/dist/src/help/templates/usage.js.map +1 -0
- package/dist/src/help/types.d.ts +53 -0
- package/dist/src/help/types.d.ts.map +1 -0
- package/dist/src/help/types.js +3 -0
- package/dist/src/help/types.js.map +1 -0
- package/dist/src/help/utils.d.ts +9 -0
- package/dist/src/help/utils.d.ts.map +1 -0
- package/dist/src/help/utils.js +92 -0
- package/dist/src/help/utils.js.map +1 -0
- package/dist/src/index.d.ts +90 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +111 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/logger/__tests__/logger.spec.d.ts +2 -0
- package/dist/src/logger/__tests__/logger.spec.d.ts.map +1 -0
- package/dist/src/logger/__tests__/logger.spec.js +56 -0
- package/dist/src/logger/__tests__/logger.spec.js.map +1 -0
- package/dist/src/logger/index.d.ts +6 -0
- package/dist/src/logger/index.d.ts.map +1 -0
- package/dist/src/logger/index.js +128 -0
- package/dist/src/logger/index.js.map +1 -0
- package/dist/src/option/__tests__/global.spec.d.ts +2 -0
- package/dist/src/option/__tests__/global.spec.d.ts.map +1 -0
- package/dist/src/option/__tests__/global.spec.js +121 -0
- package/dist/src/option/__tests__/global.spec.js.map +1 -0
- package/dist/src/option/__tests__/option.spec.d.ts +2 -0
- package/dist/src/option/__tests__/option.spec.d.ts.map +1 -0
- package/dist/src/option/__tests__/option.spec.js +111 -0
- package/dist/src/option/__tests__/option.spec.js.map +1 -0
- package/dist/src/option/find.d.ts +14 -0
- package/dist/src/option/find.d.ts.map +1 -0
- package/dist/src/option/find.js +17 -0
- package/dist/src/option/find.js.map +1 -0
- package/dist/src/option/index.d.ts +65 -0
- package/dist/src/option/index.d.ts.map +1 -0
- package/dist/src/option/index.js +230 -0
- package/dist/src/option/index.js.map +1 -0
- package/dist/src/option/mapping.d.ts +7 -0
- package/dist/src/option/mapping.d.ts.map +1 -0
- package/dist/src/option/mapping.js +21 -0
- package/dist/src/option/mapping.js.map +1 -0
- package/dist/src/option/utils.d.ts +35 -0
- package/dist/src/option/utils.d.ts.map +1 -0
- package/dist/src/option/utils.js +139 -0
- package/dist/src/option/utils.js.map +1 -0
- package/dist/src/option/validate.d.ts +15 -0
- package/dist/src/option/validate.d.ts.map +1 -0
- package/dist/src/option/validate.js +63 -0
- package/dist/src/option/validate.js.map +1 -0
- package/dist/src/parser/__tests__/parser.spec.d.ts +2 -0
- package/dist/src/parser/__tests__/parser.spec.d.ts.map +1 -0
- package/dist/src/parser/__tests__/parser.spec.js +343 -0
- package/dist/src/parser/__tests__/parser.spec.js.map +1 -0
- package/dist/src/parser/index.d.ts +17 -0
- package/dist/src/parser/index.d.ts.map +1 -0
- package/dist/src/parser/index.js +299 -0
- package/dist/src/parser/index.js.map +1 -0
- package/dist/src/program/__tests__/program.spec.d.ts +2 -0
- package/dist/src/program/__tests__/program.spec.d.ts.map +1 -0
- package/dist/src/program/__tests__/program.spec.js +261 -0
- package/dist/src/program/__tests__/program.spec.js.map +1 -0
- package/dist/src/program/index.d.ts +349 -0
- package/dist/src/program/index.d.ts.map +1 -0
- package/dist/src/program/index.js +584 -0
- package/dist/src/program/index.js.map +1 -0
- package/dist/src/types.d.ts +396 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +51 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils/__tests__/fs.spec.d.ts +2 -0
- package/dist/src/utils/__tests__/fs.spec.d.ts.map +1 -0
- package/dist/src/utils/__tests__/fs.spec.js +14 -0
- package/dist/src/utils/__tests__/fs.spec.js.map +1 -0
- package/dist/src/utils/__tests__/levenshtein.spec.d.ts +2 -0
- package/dist/src/utils/__tests__/levenshtein.spec.d.ts.map +1 -0
- package/dist/src/utils/__tests__/levenshtein.spec.js +18 -0
- package/dist/src/utils/__tests__/levenshtein.spec.js.map +1 -0
- package/dist/src/utils/__tests__/suggest.spec.d.ts +2 -0
- package/dist/src/utils/__tests__/suggest.spec.d.ts.map +1 -0
- package/dist/src/utils/__tests__/suggest.spec.js +31 -0
- package/dist/src/utils/__tests__/suggest.spec.js.map +1 -0
- package/dist/src/utils/colorize.d.ts +2 -0
- package/dist/src/utils/colorize.d.ts.map +1 -0
- package/dist/src/utils/colorize.js +27 -0
- package/dist/src/utils/colorize.js.map +1 -0
- package/dist/src/utils/fs.d.ts +2 -0
- package/dist/src/utils/fs.d.ts.map +1 -0
- package/dist/src/utils/fs.js +19 -0
- package/dist/src/utils/fs.js.map +1 -0
- package/dist/src/utils/levenshtein.d.ts +6 -0
- package/dist/src/utils/levenshtein.d.ts.map +1 -0
- package/dist/src/utils/levenshtein.js +32 -0
- package/dist/src/utils/levenshtein.js.map +1 -0
- package/dist/src/utils/suggest.d.ts +15 -0
- package/dist/src/utils/suggest.d.ts.map +1 -0
- package/dist/src/utils/suggest.js +49 -0
- package/dist/src/utils/suggest.js.map +1 -0
- package/dist/src/utils/version.d.ts +2 -0
- package/dist/src/utils/version.d.ts.map +1 -0
- package/dist/src/utils/version.js +21 -0
- package/dist/src/utils/version.js.map +1 -0
- package/dist/src/utils/web/autocomplete.d.ts +12 -0
- package/dist/src/utils/web/autocomplete.d.ts.map +1 -0
- package/dist/src/utils/web/autocomplete.js +12 -0
- package/dist/src/utils/web/autocomplete.js.map +1 -0
- package/dist/src/utils/web/process.d.ts +15 -0
- package/dist/src/utils/web/process.d.ts.map +1 -0
- package/dist/src/utils/web/process.js +29 -0
- package/dist/src/utils/web/process.js.map +1 -0
- package/dist/src/validator/__tests__/array.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/array.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/array.spec.js +34 -0
- package/dist/src/validator/__tests__/array.spec.js.map +1 -0
- package/dist/src/validator/__tests__/caporal.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/caporal.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/caporal.spec.js +86 -0
- package/dist/src/validator/__tests__/caporal.spec.js.map +1 -0
- package/dist/src/validator/__tests__/function.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/function.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/function.spec.js +42 -0
- package/dist/src/validator/__tests__/function.spec.js.map +1 -0
- package/dist/src/validator/__tests__/regexp.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/regexp.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/regexp.spec.js +34 -0
- package/dist/src/validator/__tests__/regexp.spec.js.map +1 -0
- package/dist/src/validator/__tests__/utils.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/utils.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/utils.spec.js +66 -0
- package/dist/src/validator/__tests__/utils.spec.js.map +1 -0
- package/dist/src/validator/__tests__/validate.spec.d.ts +2 -0
- package/dist/src/validator/__tests__/validate.spec.d.ts.map +1 -0
- package/dist/src/validator/__tests__/validate.spec.js +25 -0
- package/dist/src/validator/__tests__/validate.spec.js.map +1 -0
- package/dist/src/validator/array.d.ts +14 -0
- package/dist/src/validator/array.d.ts.map +1 -0
- package/dist/src/validator/array.js +29 -0
- package/dist/src/validator/array.js.map +1 -0
- package/dist/src/validator/caporal.d.ts +25 -0
- package/dist/src/validator/caporal.d.ts.map +1 -0
- package/dist/src/validator/caporal.js +89 -0
- package/dist/src/validator/caporal.js.map +1 -0
- package/dist/src/validator/function.d.ts +7 -0
- package/dist/src/validator/function.d.ts.map +1 -0
- package/dist/src/validator/function.js +27 -0
- package/dist/src/validator/function.js.map +1 -0
- package/dist/src/validator/regexp.d.ts +14 -0
- package/dist/src/validator/regexp.d.ts.map +1 -0
- package/dist/src/validator/regexp.js +31 -0
- package/dist/src/validator/regexp.js.map +1 -0
- package/dist/src/validator/utils.d.ts +14 -0
- package/dist/src/validator/utils.d.ts.map +1 -0
- package/dist/src/validator/utils.js +81 -0
- package/dist/src/validator/utils.js.map +1 -0
- package/dist/src/validator/validate.d.ts +7 -0
- package/dist/src/validator/validate.d.ts.map +1 -0
- package/dist/src/validator/validate.js +29 -0
- package/dist/src/validator/validate.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +75 -0
- package/src/__tests__/issue-163.spec.ts +18 -0
- package/src/argument/__tests__/argument.spec.ts +84 -0
- package/src/argument/find.ts +11 -0
- package/src/argument/index.ts +35 -0
- package/src/argument/synopsis.ts +41 -0
- package/src/argument/validate.ts +176 -0
- package/src/autocomplete/__fixtures__/prog-autocomplete.ts +24 -0
- package/src/autocomplete/__tests__/autocomplete.spec.ts +129 -0
- package/src/autocomplete/index.ts +225 -0
- package/src/autocomplete/types.ts +29 -0
- package/src/command/__fixtures__/example-cmd.ts +14 -0
- package/src/command/__tests__/command.spec.ts +621 -0
- package/src/command/__tests__/find.spec.ts +32 -0
- package/src/command/__tests__/import.spec.ts +14 -0
- package/src/command/__tests__/scan.spec.ts +16 -0
- package/src/command/find.ts +57 -0
- package/src/command/import.ts +13 -0
- package/src/command/index.ts +458 -0
- package/src/command/scan.ts +30 -0
- package/src/command/validate-call.ts +19 -0
- package/src/config/index.ts +26 -0
- package/src/error/__tests__/fatal.spec.ts +38 -0
- package/src/error/action.ts +13 -0
- package/src/error/base.ts +20 -0
- package/src/error/fatal.ts +25 -0
- package/src/error/index.ts +17 -0
- package/src/error/invalid-validator.ts +12 -0
- package/src/error/missing-argument.ts +16 -0
- package/src/error/missing-flag.ts +16 -0
- package/src/error/multi-validation.ts +22 -0
- package/src/error/no-action.ts +19 -0
- package/src/error/option-synopsis-syntax.ts +12 -0
- package/src/error/too-many-arguments.ts +28 -0
- package/src/error/unknown-command.ts +41 -0
- package/src/error/unknown-option.ts +40 -0
- package/src/error/validation.ts +80 -0
- package/src/help/__tests__/__snapshots__/help.spec.ts.snap +334 -0
- package/src/help/__tests__/help.spec.ts +144 -0
- package/src/help/__tests__/utils.spec.ts +14 -0
- package/src/help/index.ts +107 -0
- package/src/help/templates/command.ts +27 -0
- package/src/help/templates/custom.ts +25 -0
- package/src/help/templates/header.ts +19 -0
- package/src/help/templates/index.ts +9 -0
- package/src/help/templates/program.ts +21 -0
- package/src/help/templates/usage.ts +24 -0
- package/src/help/types.ts +57 -0
- package/src/help/utils.ts +109 -0
- package/src/index.ts +93 -0
- package/src/logger/__tests__/logger.spec.ts +65 -0
- package/src/logger/index.ts +97 -0
- package/src/option/__tests__/global.spec.ts +141 -0
- package/src/option/__tests__/option.spec.ts +115 -0
- package/src/option/find.ts +17 -0
- package/src/option/index.ts +280 -0
- package/src/option/mapping.ts +17 -0
- package/src/option/utils.ts +141 -0
- package/src/option/validate.ts +80 -0
- package/src/parser/__tests__/parser.spec.ts +371 -0
- package/src/parser/index.ts +383 -0
- package/src/program/__tests__/program.spec.ts +316 -0
- package/src/program/index.ts +676 -0
- package/src/types.ts +432 -0
- package/src/utils/__tests__/fs.spec.ts +14 -0
- package/src/utils/__tests__/levenshtein.spec.ts +16 -0
- package/src/utils/__tests__/suggest.spec.ts +30 -0
- package/src/utils/colorize.ts +21 -0
- package/src/utils/fs.ts +13 -0
- package/src/utils/levenshtein.ts +28 -0
- package/src/utils/suggest.ts +52 -0
- package/src/utils/version.ts +14 -0
- package/src/utils/web/autocomplete.ts +19 -0
- package/src/utils/web/process.ts +24 -0
- package/src/validator/__tests__/array.spec.ts +41 -0
- package/src/validator/__tests__/caporal.spec.ts +132 -0
- package/src/validator/__tests__/function.spec.ts +52 -0
- package/src/validator/__tests__/regexp.spec.ts +38 -0
- package/src/validator/__tests__/utils.spec.ts +67 -0
- package/src/validator/__tests__/validate.spec.ts +24 -0
- package/src/validator/array.ts +31 -0
- package/src/validator/caporal.ts +104 -0
- package/src/validator/function.ts +31 -0
- package/src/validator/regexp.ts +34 -0
- package/src/validator/utils.ts +84 -0
- package/src/validator/validate.ts +38 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { validateWithArray } from "../array"
|
|
2
|
+
import { createArgument } from "../../argument"
|
|
3
|
+
import { createOption } from "../../option"
|
|
4
|
+
import { ValidationError } from "../../error"
|
|
5
|
+
import { expect, it, describe } from "vitest"
|
|
6
|
+
|
|
7
|
+
describe("validateWithArray()", () => {
|
|
8
|
+
const arg = createArgument("<fake>", "Fake arg")
|
|
9
|
+
const opt = createOption("--file <file>", "File")
|
|
10
|
+
|
|
11
|
+
it("should validate a valid string", () => {
|
|
12
|
+
expect(validateWithArray(["foo", "bar"], "bar", arg)).toEqual("bar")
|
|
13
|
+
expect(validateWithArray(["foo", "bar"], "bar", opt)).toEqual("bar")
|
|
14
|
+
})
|
|
15
|
+
it("should throw for an invalid string", () => {
|
|
16
|
+
expect(() => validateWithArray(["foo", "bar"], "invalid", arg)).toThrowError(
|
|
17
|
+
ValidationError,
|
|
18
|
+
)
|
|
19
|
+
expect(() => validateWithArray(["foo", "bar"], "invalid", opt)).toThrowError(
|
|
20
|
+
ValidationError,
|
|
21
|
+
)
|
|
22
|
+
})
|
|
23
|
+
it("should validate a valid string[]", () => {
|
|
24
|
+
expect(validateWithArray(["foo", "bar", "hey"], ["bar", "hey"], arg)).toEqual([
|
|
25
|
+
"bar",
|
|
26
|
+
"hey",
|
|
27
|
+
])
|
|
28
|
+
expect(validateWithArray(["foo", "bar", "hey"], ["bar", "hey"], opt)).toEqual([
|
|
29
|
+
"bar",
|
|
30
|
+
"hey",
|
|
31
|
+
])
|
|
32
|
+
})
|
|
33
|
+
it("should throw for an invalid string[]", () => {
|
|
34
|
+
expect(() =>
|
|
35
|
+
validateWithArray(["foo", "bar", "hey"], ["bar", "bad"], arg),
|
|
36
|
+
).toThrowError(ValidationError)
|
|
37
|
+
expect(() =>
|
|
38
|
+
validateWithArray(["foo", "bar", "hey"], ["bar", "bad"], opt),
|
|
39
|
+
).toThrowError(ValidationError)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { createArgument } from "../../argument"
|
|
2
|
+
import { createOption } from "../../option"
|
|
3
|
+
import { ValidationError } from "../../error"
|
|
4
|
+
import { validateWithCaporal, CaporalValidator } from "../caporal"
|
|
5
|
+
import { expect, it, describe } from "vitest"
|
|
6
|
+
|
|
7
|
+
describe("validateWithCaporal()", () => {
|
|
8
|
+
const arg = createArgument("<fake>", "Fake arg")
|
|
9
|
+
const opt = createOption("--file <file>", "File")
|
|
10
|
+
|
|
11
|
+
it("should work with an array validator", () => {
|
|
12
|
+
expect(validateWithCaporal(CaporalValidator.ARRAY, ["val2"], arg)).toEqual(["val2"])
|
|
13
|
+
expect(validateWithCaporal(CaporalValidator.ARRAY, ["val2"], opt)).toEqual(["val2"])
|
|
14
|
+
expect(validateWithCaporal(CaporalValidator.ARRAY, "hey", arg)).toEqual(["hey"])
|
|
15
|
+
expect(validateWithCaporal(CaporalValidator.ARRAY, "hey", opt)).toEqual(["hey"])
|
|
16
|
+
expect(validateWithCaporal(CaporalValidator.ARRAY, "hey,ho", arg)).toEqual([
|
|
17
|
+
"hey",
|
|
18
|
+
"ho",
|
|
19
|
+
])
|
|
20
|
+
|
|
21
|
+
expect(validateWithCaporal(CaporalValidator.ARRAY, "hey,ho", opt)).toEqual([
|
|
22
|
+
"hey",
|
|
23
|
+
"ho",
|
|
24
|
+
])
|
|
25
|
+
|
|
26
|
+
expect(validateWithCaporal(CaporalValidator.ARRAY, ["hey", "ho"], arg)).toEqual([
|
|
27
|
+
"hey",
|
|
28
|
+
"ho",
|
|
29
|
+
])
|
|
30
|
+
|
|
31
|
+
expect(validateWithCaporal(CaporalValidator.ARRAY, ["hey", "ho"], opt)).toEqual([
|
|
32
|
+
"hey",
|
|
33
|
+
"ho",
|
|
34
|
+
])
|
|
35
|
+
|
|
36
|
+
expect(validateWithCaporal(CaporalValidator.ARRAY, 123, arg)).toEqual([123])
|
|
37
|
+
expect(validateWithCaporal(CaporalValidator.ARRAY, 123, opt)).toEqual([123])
|
|
38
|
+
})
|
|
39
|
+
it("should work with an numeric validator", () => {
|
|
40
|
+
expect(validateWithCaporal(CaporalValidator.NUMBER, 123, arg)).toEqual(123)
|
|
41
|
+
expect(validateWithCaporal(CaporalValidator.NUMBER, 123, opt)).toEqual(123)
|
|
42
|
+
expect(validateWithCaporal(CaporalValidator.NUMBER, "123", arg)).toEqual(123)
|
|
43
|
+
expect(validateWithCaporal(CaporalValidator.NUMBER, "123", opt)).toEqual(123)
|
|
44
|
+
expect(validateWithCaporal(CaporalValidator.NUMBER, "3.14", arg)).toEqual(3.14)
|
|
45
|
+
expect(validateWithCaporal(CaporalValidator.NUMBER, "3.14", opt)).toEqual(3.14)
|
|
46
|
+
expect(() => validateWithCaporal(CaporalValidator.NUMBER, "hello", arg)).toThrowError(
|
|
47
|
+
ValidationError,
|
|
48
|
+
)
|
|
49
|
+
expect(() => validateWithCaporal(CaporalValidator.NUMBER, "hello", opt)).toThrowError(
|
|
50
|
+
ValidationError,
|
|
51
|
+
)
|
|
52
|
+
expect(() =>
|
|
53
|
+
validateWithCaporal(CaporalValidator.NUMBER, ["array"], arg),
|
|
54
|
+
).toThrowError(ValidationError)
|
|
55
|
+
|
|
56
|
+
expect(() =>
|
|
57
|
+
validateWithCaporal(CaporalValidator.NUMBER, ["array"], opt),
|
|
58
|
+
).toThrowError(ValidationError)
|
|
59
|
+
|
|
60
|
+
expect(() => validateWithCaporal(CaporalValidator.NUMBER, true, arg)).toThrowError(
|
|
61
|
+
ValidationError,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
expect(() => validateWithCaporal(CaporalValidator.NUMBER, true, opt)).toThrowError(
|
|
65
|
+
ValidationError,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
expect(() => validateWithCaporal(CaporalValidator.NUMBER, false, arg)).toThrowError(
|
|
69
|
+
ValidationError,
|
|
70
|
+
)
|
|
71
|
+
expect(() => validateWithCaporal(CaporalValidator.NUMBER, false, opt)).toThrowError(
|
|
72
|
+
ValidationError,
|
|
73
|
+
)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it("should work with an string validator", () => {
|
|
77
|
+
expect(validateWithCaporal(CaporalValidator.STRING, "hey", arg)).toEqual("hey")
|
|
78
|
+
expect(validateWithCaporal(CaporalValidator.STRING, "hey", opt)).toEqual("hey")
|
|
79
|
+
expect(validateWithCaporal(CaporalValidator.STRING, 1, opt)).toEqual("1")
|
|
80
|
+
expect(validateWithCaporal(CaporalValidator.STRING, 3.14, opt)).toEqual("3.14")
|
|
81
|
+
expect(validateWithCaporal(CaporalValidator.STRING, true, opt)).toEqual("true")
|
|
82
|
+
expect(validateWithCaporal(CaporalValidator.STRING, false, opt)).toEqual("false")
|
|
83
|
+
expect(() =>
|
|
84
|
+
validateWithCaporal(CaporalValidator.STRING, ["hello"], arg),
|
|
85
|
+
).toThrowError(ValidationError)
|
|
86
|
+
expect(() =>
|
|
87
|
+
validateWithCaporal(CaporalValidator.STRING, ["hello"], opt),
|
|
88
|
+
).toThrowError(ValidationError)
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it("should work with an boolean validator", () => {
|
|
92
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, true, arg)).toEqual(true)
|
|
93
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, true, opt)).toEqual(true)
|
|
94
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, false, arg)).toEqual(false)
|
|
95
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, false, opt)).toEqual(false)
|
|
96
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, 1, arg)).toEqual(true)
|
|
97
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, 1, opt)).toEqual(true)
|
|
98
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, 0, arg)).toEqual(false)
|
|
99
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, 0, opt)).toEqual(false)
|
|
100
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, "true", arg)).toEqual(true)
|
|
101
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, "true", opt)).toEqual(true)
|
|
102
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, "false", arg)).toEqual(false)
|
|
103
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, "false", opt)).toEqual(false)
|
|
104
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, "yes", arg)).toEqual(true)
|
|
105
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, "yes", opt)).toEqual(true)
|
|
106
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, "no", arg)).toEqual(false)
|
|
107
|
+
expect(validateWithCaporal(CaporalValidator.BOOLEAN, "no", opt)).toEqual(false)
|
|
108
|
+
expect(() =>
|
|
109
|
+
validateWithCaporal(CaporalValidator.BOOLEAN, ["hello"], arg),
|
|
110
|
+
).toThrowError(ValidationError)
|
|
111
|
+
|
|
112
|
+
expect(() =>
|
|
113
|
+
validateWithCaporal(CaporalValidator.BOOLEAN, ["hello"], opt),
|
|
114
|
+
).toThrowError(ValidationError)
|
|
115
|
+
|
|
116
|
+
expect(() =>
|
|
117
|
+
validateWithCaporal(CaporalValidator.BOOLEAN, "unknown", arg),
|
|
118
|
+
).toThrowError(ValidationError)
|
|
119
|
+
|
|
120
|
+
expect(() =>
|
|
121
|
+
validateWithCaporal(CaporalValidator.BOOLEAN, "unknown", opt),
|
|
122
|
+
).toThrowError(ValidationError)
|
|
123
|
+
|
|
124
|
+
expect(() => validateWithCaporal(CaporalValidator.BOOLEAN, 2, arg)).toThrowError(
|
|
125
|
+
ValidationError,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
expect(() => validateWithCaporal(CaporalValidator.BOOLEAN, 2, opt)).toThrowError(
|
|
129
|
+
ValidationError,
|
|
130
|
+
)
|
|
131
|
+
})
|
|
132
|
+
})
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { validateWithFunction } from "../function"
|
|
2
|
+
import { createArgument } from "../../argument"
|
|
3
|
+
import { createOption } from "../../option"
|
|
4
|
+
import { ValidationError } from "../../error"
|
|
5
|
+
import { FunctionValidator } from "../../types"
|
|
6
|
+
import { expect, it, describe } from "vitest"
|
|
7
|
+
|
|
8
|
+
describe("validateWithFunction()", () => {
|
|
9
|
+
const validator: FunctionValidator = function (value) {
|
|
10
|
+
if (value !== "hey" && value !== "ho") {
|
|
11
|
+
throw Error("my error")
|
|
12
|
+
}
|
|
13
|
+
return value
|
|
14
|
+
}
|
|
15
|
+
const arg = createArgument("<fake>", "Fake arg", { validator })
|
|
16
|
+
const opt = createOption("--file <file>", "File")
|
|
17
|
+
|
|
18
|
+
it("should validate a valid value", async () => {
|
|
19
|
+
await expect(validateWithFunction(validator, "hey", arg)).resolves.toEqual("hey")
|
|
20
|
+
await expect(validateWithFunction(validator, "hey", opt)).resolves.toEqual("hey")
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it("should throw for an invalid value", async () => {
|
|
24
|
+
await expect(validateWithFunction(validator, "invalid", arg)).rejects.toBeInstanceOf(
|
|
25
|
+
ValidationError,
|
|
26
|
+
)
|
|
27
|
+
await expect(validateWithFunction(validator, "invalid", opt)).rejects.toBeInstanceOf(
|
|
28
|
+
ValidationError,
|
|
29
|
+
)
|
|
30
|
+
})
|
|
31
|
+
it("should validate a valid array", async () => {
|
|
32
|
+
await expect(validateWithFunction(validator, ["ho"], arg)).resolves.toEqual(["ho"])
|
|
33
|
+
await expect(validateWithFunction(validator, ["ho"], opt)).resolves.toEqual(["ho"])
|
|
34
|
+
await expect(validateWithFunction(validator, ["ho", "hey"], arg)).resolves.toEqual([
|
|
35
|
+
"ho",
|
|
36
|
+
"hey",
|
|
37
|
+
])
|
|
38
|
+
await expect(validateWithFunction(validator, ["ho", "hey"], opt)).resolves.toEqual([
|
|
39
|
+
"ho",
|
|
40
|
+
"hey",
|
|
41
|
+
])
|
|
42
|
+
})
|
|
43
|
+
it("should throw for an invalid array", async () => {
|
|
44
|
+
await expect(
|
|
45
|
+
validateWithFunction(validator, ["invalid", "hey"], arg),
|
|
46
|
+
).rejects.toBeInstanceOf(ValidationError)
|
|
47
|
+
|
|
48
|
+
await expect(
|
|
49
|
+
validateWithFunction(validator, ["invalid", "hey"], opt),
|
|
50
|
+
).rejects.toBeInstanceOf(ValidationError)
|
|
51
|
+
})
|
|
52
|
+
})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { expect, it, describe } from "vitest"
|
|
2
|
+
|
|
3
|
+
import { validateWithRegExp } from "../regexp"
|
|
4
|
+
import { createArgument } from "../../argument"
|
|
5
|
+
import { createOption } from "../../option"
|
|
6
|
+
import { ValidationError } from "../../error"
|
|
7
|
+
|
|
8
|
+
describe("validateWithRegExp()", () => {
|
|
9
|
+
const arg = createArgument("<fake>", "Fake arg")
|
|
10
|
+
const opt = createOption("--file <file>", "File")
|
|
11
|
+
|
|
12
|
+
it("should validate a valid string", () => {
|
|
13
|
+
expect(validateWithRegExp(/^ex/, "example", arg)).toEqual("example")
|
|
14
|
+
expect(validateWithRegExp(/^ex/, "example", opt)).toEqual("example")
|
|
15
|
+
})
|
|
16
|
+
it("should throw for an invalid string", () => {
|
|
17
|
+
expect(() => validateWithRegExp(/^ex/, "invalid", arg)).toThrowError(ValidationError)
|
|
18
|
+
expect(() => validateWithRegExp(/^ex/, "invalid", opt)).toThrowError(ValidationError)
|
|
19
|
+
})
|
|
20
|
+
it("should validate a valid string[]", () => {
|
|
21
|
+
expect(validateWithRegExp(/^ex/, ["example", "executor"], arg)).toEqual([
|
|
22
|
+
"example",
|
|
23
|
+
"executor",
|
|
24
|
+
])
|
|
25
|
+
expect(validateWithRegExp(/^ex/, ["example", "executor"], opt)).toEqual([
|
|
26
|
+
"example",
|
|
27
|
+
"executor",
|
|
28
|
+
])
|
|
29
|
+
})
|
|
30
|
+
it("should throw for an invalid string[]", () => {
|
|
31
|
+
expect(() => validateWithRegExp(/^ex/, ["example", "invalid"], arg)).toThrowError(
|
|
32
|
+
ValidationError,
|
|
33
|
+
)
|
|
34
|
+
expect(() => validateWithRegExp(/^ex/, ["example", "invalid"], opt)).toThrowError(
|
|
35
|
+
ValidationError,
|
|
36
|
+
)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { checkValidator, getTypeHint } from "../utils"
|
|
2
|
+
import { createOption } from "../../option"
|
|
3
|
+
import { InvalidValidatorError } from "../../error"
|
|
4
|
+
import { CaporalValidator } from "../../types"
|
|
5
|
+
import isNumber from "lodash/isNumber"
|
|
6
|
+
import { expect, it, describe } from "vitest"
|
|
7
|
+
|
|
8
|
+
const validators = Object.values(CaporalValidator).filter(isNumber)
|
|
9
|
+
|
|
10
|
+
describe("validator / utils", () => {
|
|
11
|
+
describe("checkValidator()", () => {
|
|
12
|
+
it("should throw InvalidValidatorError for an invalid Caporal Validator", () => {
|
|
13
|
+
// @ts-expect-error for tests
|
|
14
|
+
expect(() => checkValidator(1001)).toThrowError(InvalidValidatorError)
|
|
15
|
+
})
|
|
16
|
+
it("should throw InvalidValidatorError for all valid Caporal Validators", () => {
|
|
17
|
+
// @ts-expect-error for tests
|
|
18
|
+
expect(() => checkValidator(1001)).toThrowError(InvalidValidatorError)
|
|
19
|
+
})
|
|
20
|
+
it.each(validators)("should not throw for %d", (v) => {
|
|
21
|
+
expect(() => checkValidator(v)).not.toThrowError(InvalidValidatorError)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it("should throw for an invalid caporal validator", () => {
|
|
25
|
+
// @ts-expect-error for tests
|
|
26
|
+
expect(() => checkValidator(1000)).toThrowError(InvalidValidatorError)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it("should throw for an invalid user defined validator", () => {
|
|
30
|
+
// @ts-expect-error for tests
|
|
31
|
+
expect(() => checkValidator("wrong")).toThrowError(InvalidValidatorError)
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
describe("getTypeHint()", () => {
|
|
36
|
+
it("should return 'boolean' for a boolean option whose default is true", () => {
|
|
37
|
+
const opt = createOption("--use-this", "Use this", { default: true })
|
|
38
|
+
expect(getTypeHint(opt)).toBe("boolean")
|
|
39
|
+
})
|
|
40
|
+
it("should return a JSON for an array-validator", () => {
|
|
41
|
+
const opt = createOption("--name <name>", "Your name", {
|
|
42
|
+
validator: ["john", "jesse"],
|
|
43
|
+
})
|
|
44
|
+
expect(getTypeHint(opt)).toBe('one of "john","jesse"')
|
|
45
|
+
})
|
|
46
|
+
it("should return undefined for an array-validator if the JSON representation is too long", () => {
|
|
47
|
+
const arr = new Array(120).fill("str".repeat(15))
|
|
48
|
+
const opt = createOption("--name <name>", "Your name", {
|
|
49
|
+
validator: arr,
|
|
50
|
+
})
|
|
51
|
+
expect(getTypeHint(opt)).toBeUndefined()
|
|
52
|
+
})
|
|
53
|
+
it("should throw InvalidValidatorError for all valid Caporal Validators", () => {
|
|
54
|
+
// @ts-expect-error for tests
|
|
55
|
+
expect(() => checkValidator(1001)).toThrowError(InvalidValidatorError)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it.each(validators)("should not throw for %d", (v) => {
|
|
59
|
+
expect(() => checkValidator(v)).not.toThrowError(InvalidValidatorError)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it("should throw for an invalid user defined validator", () => {
|
|
63
|
+
// @ts-expect-error for tests
|
|
64
|
+
expect(() => checkValidator("wrong")).toThrowError(InvalidValidatorError)
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { validate } from "../validate"
|
|
2
|
+
import { createArgument } from "../../argument"
|
|
3
|
+
import { FunctionValidator } from "../../types"
|
|
4
|
+
import { expect, it, describe } from "vitest"
|
|
5
|
+
|
|
6
|
+
describe("validate()", () => {
|
|
7
|
+
const arg = createArgument("<fake>", "Fake arg")
|
|
8
|
+
it("should handle function validators", () => {
|
|
9
|
+
const validator: FunctionValidator = function (value) {
|
|
10
|
+
if (value !== "hey" && value !== "ho") {
|
|
11
|
+
throw Error("my error")
|
|
12
|
+
}
|
|
13
|
+
return value
|
|
14
|
+
}
|
|
15
|
+
return expect(validate("hey", validator, arg)).resolves.toEqual("hey")
|
|
16
|
+
})
|
|
17
|
+
it("should handle regexp validators", () => {
|
|
18
|
+
return expect(validate("TEST", /[A-Z]+/, arg)).toEqual("TEST")
|
|
19
|
+
})
|
|
20
|
+
it("should handle unknown validators", () => {
|
|
21
|
+
// @ts-expect-error for tests
|
|
22
|
+
return expect(validate("TEST", 1000, arg)).toEqual("TEST")
|
|
23
|
+
})
|
|
24
|
+
})
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { ParserTypes, Argument, Option } from "../types"
|
|
7
|
+
import { ValidationError } from "../error"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Validate using an array of valid values.
|
|
11
|
+
*
|
|
12
|
+
* @param validator
|
|
13
|
+
* @param value
|
|
14
|
+
* @ignore
|
|
15
|
+
*/
|
|
16
|
+
export function validateWithArray(
|
|
17
|
+
validator: ParserTypes[],
|
|
18
|
+
value: ParserTypes | ParserTypes[],
|
|
19
|
+
context: Argument | Option,
|
|
20
|
+
): ParserTypes | ParserTypes[] {
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
value.forEach((v) => validateWithArray(validator, v, context))
|
|
23
|
+
} else if (validator.includes(value) === false) {
|
|
24
|
+
throw new ValidationError({
|
|
25
|
+
validator,
|
|
26
|
+
value,
|
|
27
|
+
context,
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
return value
|
|
31
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { ParserTypes, Argument, Option } from "../types"
|
|
7
|
+
import { CaporalValidator } from "../types"
|
|
8
|
+
import { ValidationError } from "../error"
|
|
9
|
+
import { isNumeric } from "../option/utils"
|
|
10
|
+
import flatMap from "lodash/flatMap"
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
isNumericValidator,
|
|
14
|
+
isStringValidator,
|
|
15
|
+
isBoolValidator,
|
|
16
|
+
isArrayValidator,
|
|
17
|
+
} from "./utils"
|
|
18
|
+
|
|
19
|
+
// Re-export for convenience
|
|
20
|
+
export { CaporalValidator }
|
|
21
|
+
|
|
22
|
+
export function validateWithCaporal(
|
|
23
|
+
validator: CaporalValidator,
|
|
24
|
+
value: ParserTypes | ParserTypes[],
|
|
25
|
+
context: Argument | Option,
|
|
26
|
+
skipArrayValidation = false,
|
|
27
|
+
): ParserTypes | ParserTypes[] {
|
|
28
|
+
if (!skipArrayValidation && isArrayValidator(validator)) {
|
|
29
|
+
return validateArrayFlag(validator, value, context)
|
|
30
|
+
} else if (Array.isArray(value)) {
|
|
31
|
+
// should not happen!
|
|
32
|
+
throw new ValidationError({
|
|
33
|
+
error: "Expected a scalar value, got an array",
|
|
34
|
+
value,
|
|
35
|
+
validator,
|
|
36
|
+
context,
|
|
37
|
+
})
|
|
38
|
+
} else if (isNumericValidator(validator)) {
|
|
39
|
+
return validateNumericFlag(validator, value, context)
|
|
40
|
+
} else if (isStringValidator(validator)) {
|
|
41
|
+
return validateStringFlag(value)
|
|
42
|
+
} else if (isBoolValidator(validator)) {
|
|
43
|
+
return validateBoolFlag(value, context)
|
|
44
|
+
}
|
|
45
|
+
return value
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The string validator actually just cast the value to string
|
|
50
|
+
*
|
|
51
|
+
* @param value
|
|
52
|
+
* @ignore
|
|
53
|
+
*/
|
|
54
|
+
export function validateBoolFlag(
|
|
55
|
+
value: ParserTypes,
|
|
56
|
+
context: Argument | Option,
|
|
57
|
+
): boolean {
|
|
58
|
+
if (typeof value === "boolean") {
|
|
59
|
+
return value
|
|
60
|
+
} else if (/^(true|false|yes|no|0|1)$/i.test(String(value)) === false) {
|
|
61
|
+
throw new ValidationError({
|
|
62
|
+
value,
|
|
63
|
+
validator: CaporalValidator.BOOLEAN,
|
|
64
|
+
context,
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
return /^0|no|false$/.test(String(value)) === false
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function validateNumericFlag(
|
|
71
|
+
validator: number,
|
|
72
|
+
value: ParserTypes,
|
|
73
|
+
context: Argument | Option,
|
|
74
|
+
): number {
|
|
75
|
+
const str = value + ""
|
|
76
|
+
if (Array.isArray(value) || !isNumeric(str)) {
|
|
77
|
+
throw new ValidationError({
|
|
78
|
+
value,
|
|
79
|
+
validator,
|
|
80
|
+
context,
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
return parseFloat(str)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function validateArrayFlag(
|
|
87
|
+
validator: number,
|
|
88
|
+
value: ParserTypes | ParserTypes[],
|
|
89
|
+
context: Argument | Option,
|
|
90
|
+
): ParserTypes | ParserTypes[] {
|
|
91
|
+
const values: ParserTypes[] =
|
|
92
|
+
typeof value === "string" ? value.split(",") : !Array.isArray(value) ? [value] : value
|
|
93
|
+
return flatMap(values, (el) => validateWithCaporal(validator, el, context, true))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The string validator actually just cast the value to string
|
|
98
|
+
*
|
|
99
|
+
* @param value
|
|
100
|
+
* @ignore
|
|
101
|
+
*/
|
|
102
|
+
export function validateStringFlag(value: ParserTypes | ParserTypes[]): string {
|
|
103
|
+
return value + ""
|
|
104
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { ParserTypes, FunctionValidator, Argument, Option } from "../types"
|
|
7
|
+
import { ValidationError } from "../error"
|
|
8
|
+
|
|
9
|
+
export async function validateWithFunction(
|
|
10
|
+
validator: FunctionValidator,
|
|
11
|
+
value: ParserTypes | ParserTypes[],
|
|
12
|
+
context: Argument | Option,
|
|
13
|
+
): Promise<ParserTypes | ParserTypes[]> {
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
return Promise.all(
|
|
16
|
+
value.map((v) => {
|
|
17
|
+
return validateWithFunction(validator, v, context) as Promise<ParserTypes>
|
|
18
|
+
}),
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
return await validator(value)
|
|
23
|
+
} catch (error) {
|
|
24
|
+
throw new ValidationError({
|
|
25
|
+
validator,
|
|
26
|
+
value,
|
|
27
|
+
error,
|
|
28
|
+
context,
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { ParserTypes, Argument, Option } from "../types"
|
|
7
|
+
import { ValidationError } from "../error"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Validate using a RegExp
|
|
11
|
+
*
|
|
12
|
+
* @param validator
|
|
13
|
+
* @param value
|
|
14
|
+
* @ignore
|
|
15
|
+
*/
|
|
16
|
+
export function validateWithRegExp(
|
|
17
|
+
validator: RegExp,
|
|
18
|
+
value: ParserTypes | ParserTypes[],
|
|
19
|
+
context: Argument | Option,
|
|
20
|
+
): ParserTypes | ParserTypes[] {
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
return value.map((v) => {
|
|
23
|
+
return validateWithRegExp(validator, v, context) as ParserTypes
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
if (!validator.test(value + "")) {
|
|
27
|
+
throw new ValidationError({
|
|
28
|
+
validator: validator,
|
|
29
|
+
value,
|
|
30
|
+
context,
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
return value
|
|
34
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Validator } from "../types"
|
|
7
|
+
import { CaporalValidator, Argument, Option } from "../types"
|
|
8
|
+
import isNumber from "lodash/isNumber"
|
|
9
|
+
import { InvalidValidatorError } from "../error"
|
|
10
|
+
|
|
11
|
+
export function isCaporalValidator(
|
|
12
|
+
validator: Validator | undefined,
|
|
13
|
+
): validator is number {
|
|
14
|
+
if (typeof validator !== "number") {
|
|
15
|
+
return false
|
|
16
|
+
}
|
|
17
|
+
const mask = getCaporalValidatorsMask()
|
|
18
|
+
const exist = (mask & validator) === validator
|
|
19
|
+
return exist
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function isNumericValidator(validator: Validator | undefined): boolean {
|
|
23
|
+
return isCaporalValidator(validator) && Boolean(validator & CaporalValidator.NUMBER)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function isStringValidator(validator: Validator | undefined): boolean {
|
|
27
|
+
return isCaporalValidator(validator) && Boolean(validator & CaporalValidator.STRING)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function isBoolValidator(validator: Validator | undefined): boolean {
|
|
31
|
+
return isCaporalValidator(validator) && Boolean(validator & CaporalValidator.BOOLEAN)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function isArrayValidator(validator: Validator | undefined): boolean {
|
|
35
|
+
return isCaporalValidator(validator) && Boolean(validator & CaporalValidator.ARRAY)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function getCaporalValidatorsMask(): number {
|
|
39
|
+
return Object.values(CaporalValidator)
|
|
40
|
+
.filter(isNumber)
|
|
41
|
+
.reduce((a, b) => a | b, 0)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function checkCaporalValidator(validator: CaporalValidator): void {
|
|
45
|
+
if (!isCaporalValidator(validator)) {
|
|
46
|
+
throw new InvalidValidatorError(validator)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function checkUserDefinedValidator(validator: Validator): void {
|
|
51
|
+
if (
|
|
52
|
+
typeof validator !== "function" &&
|
|
53
|
+
!(validator instanceof RegExp) &&
|
|
54
|
+
!Array.isArray(validator)
|
|
55
|
+
) {
|
|
56
|
+
throw new InvalidValidatorError(validator)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function checkValidator(validator: Validator | undefined): void {
|
|
61
|
+
if (validator !== undefined) {
|
|
62
|
+
typeof validator === "number"
|
|
63
|
+
? checkCaporalValidator(validator)
|
|
64
|
+
: checkUserDefinedValidator(validator)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function getTypeHint(obj: Argument | Option): string | undefined {
|
|
69
|
+
let hint
|
|
70
|
+
if (
|
|
71
|
+
isBoolValidator(obj.validator) ||
|
|
72
|
+
("boolean" in obj && obj.boolean && obj.default !== false)
|
|
73
|
+
) {
|
|
74
|
+
hint = "boolean"
|
|
75
|
+
} else if (isNumericValidator(obj.validator)) {
|
|
76
|
+
hint = "number"
|
|
77
|
+
} else if (Array.isArray(obj.validator)) {
|
|
78
|
+
const stringified = JSON.stringify(obj.validator)
|
|
79
|
+
if (stringified.length < 300) {
|
|
80
|
+
hint = "one of " + stringified.substr(1, stringified.length - 2)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return hint
|
|
84
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
Validator,
|
|
8
|
+
Promisable,
|
|
9
|
+
ParsedOption,
|
|
10
|
+
ParsedArgument,
|
|
11
|
+
Argument,
|
|
12
|
+
Option,
|
|
13
|
+
} from "../types"
|
|
14
|
+
|
|
15
|
+
import { validateWithRegExp } from "./regexp"
|
|
16
|
+
import { validateWithArray } from "./array"
|
|
17
|
+
import { validateWithFunction } from "./function"
|
|
18
|
+
import { validateWithCaporal } from "./caporal"
|
|
19
|
+
import { isCaporalValidator } from "./utils"
|
|
20
|
+
|
|
21
|
+
export function validate(
|
|
22
|
+
value: ParsedOption | ParsedArgument,
|
|
23
|
+
validator: Validator,
|
|
24
|
+
context: Argument | Option,
|
|
25
|
+
): Promisable<ParsedOption | ParsedArgument> {
|
|
26
|
+
if (typeof validator === "function") {
|
|
27
|
+
return validateWithFunction(validator, value, context)
|
|
28
|
+
} else if (validator instanceof RegExp) {
|
|
29
|
+
return validateWithRegExp(validator, value, context)
|
|
30
|
+
} else if (Array.isArray(validator)) {
|
|
31
|
+
return validateWithArray(validator, value, context)
|
|
32
|
+
}
|
|
33
|
+
// Caporal flag validator
|
|
34
|
+
else if (isCaporalValidator(validator)) {
|
|
35
|
+
return validateWithCaporal(validator, value, context)
|
|
36
|
+
}
|
|
37
|
+
return value
|
|
38
|
+
}
|