@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,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
import type { Program } from "../program"
|
|
6
|
+
import type { Command } from "."
|
|
7
|
+
import { importCommand } from "./import"
|
|
8
|
+
import { createCommand } from "."
|
|
9
|
+
import path from "path"
|
|
10
|
+
|
|
11
|
+
export async function findCommand(
|
|
12
|
+
program: Program,
|
|
13
|
+
argv: string[],
|
|
14
|
+
): Promise<Command | undefined> {
|
|
15
|
+
const commands = program.getCommands()
|
|
16
|
+
const findRegisteredCommand = (search: string): Command | undefined =>
|
|
17
|
+
commands.find((c) => c.name === search || c.hasAlias(search))
|
|
18
|
+
|
|
19
|
+
let foundCommand
|
|
20
|
+
let i
|
|
21
|
+
for (i = 0; i < argv.length; i++) {
|
|
22
|
+
const cmd = argv.slice(0, i + 1).join(" ")
|
|
23
|
+
// break as soon as possible
|
|
24
|
+
if (argv[i].startsWith("-")) {
|
|
25
|
+
break
|
|
26
|
+
}
|
|
27
|
+
const potentialCmd =
|
|
28
|
+
findRegisteredCommand(cmd) || (await discoverCommand(program, cmd))
|
|
29
|
+
foundCommand = potentialCmd || foundCommand
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return foundCommand
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Search for a command in discovery path
|
|
37
|
+
*/
|
|
38
|
+
async function discoverCommand(
|
|
39
|
+
program: Program,
|
|
40
|
+
cmd: string,
|
|
41
|
+
): Promise<Command | undefined> {
|
|
42
|
+
if (program.discoveryPath === undefined) {
|
|
43
|
+
return undefined
|
|
44
|
+
}
|
|
45
|
+
const filename = cmd.split(" ").join("/")
|
|
46
|
+
try {
|
|
47
|
+
const fullPath = path.join(program.discoveryPath, filename)
|
|
48
|
+
const cmdBuilder = await importCommand(fullPath)
|
|
49
|
+
const options = {
|
|
50
|
+
createCommand: createCommand.bind(null, program, cmd),
|
|
51
|
+
program,
|
|
52
|
+
}
|
|
53
|
+
return cmdBuilder(options)
|
|
54
|
+
// eslint-disable-next-line no-empty
|
|
55
|
+
} catch (e) {}
|
|
56
|
+
return undefined
|
|
57
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
import { CommandCreator } from "../types"
|
|
6
|
+
import path from "path"
|
|
7
|
+
|
|
8
|
+
export async function importCommand(file: string): Promise<CommandCreator> {
|
|
9
|
+
const { dir, name } = path.parse(file)
|
|
10
|
+
const filename = path.join(dir, name)
|
|
11
|
+
const mod = await import(filename)
|
|
12
|
+
return mod.default ?? mod
|
|
13
|
+
}
|
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @module caporal/command
|
|
4
|
+
*/
|
|
5
|
+
import { validateCall } from "./validate-call"
|
|
6
|
+
import { logger } from "../logger"
|
|
7
|
+
import { createArgument } from "../argument"
|
|
8
|
+
import { createOption, processGlobalOptions } from "../option"
|
|
9
|
+
import { registerCompletion } from "../autocomplete"
|
|
10
|
+
import { Completer } from "../autocomplete/types"
|
|
11
|
+
import { getOptsMapping } from "../option/mapping"
|
|
12
|
+
import { isStringValidator, isBoolValidator } from "../validator/utils"
|
|
13
|
+
import type { Program } from "../program"
|
|
14
|
+
import { ActionError, NoActionError, BaseError, ValidationSummaryError } from "../error"
|
|
15
|
+
import {
|
|
16
|
+
Action,
|
|
17
|
+
ParserOptions,
|
|
18
|
+
ParserResult,
|
|
19
|
+
Option,
|
|
20
|
+
Argument,
|
|
21
|
+
CreateArgumentOpts,
|
|
22
|
+
Configurator,
|
|
23
|
+
CommandConfig,
|
|
24
|
+
CreateOptionCommandOpts,
|
|
25
|
+
} from "../types"
|
|
26
|
+
import { CustomizedHelpOpts } from "../help/types"
|
|
27
|
+
import { customizeHelp } from "../help"
|
|
28
|
+
import { createConfigurator } from "../config"
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @ignore
|
|
32
|
+
*/
|
|
33
|
+
export const PROG_CMD = "__self_cmd"
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @ignore
|
|
37
|
+
*/
|
|
38
|
+
export const HELP_CMD = "help"
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Command class
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
export class Command {
|
|
45
|
+
private program: Program
|
|
46
|
+
private _action?: Action
|
|
47
|
+
private _lastAddedArgOrOpt?: Argument | Option
|
|
48
|
+
private _aliases: string[] = []
|
|
49
|
+
private _name: string
|
|
50
|
+
private _config: Configurator<CommandConfig>
|
|
51
|
+
/**
|
|
52
|
+
* Command description
|
|
53
|
+
*
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
readonly description: string
|
|
57
|
+
/**
|
|
58
|
+
* Command options array
|
|
59
|
+
*
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
readonly options: Option[] = []
|
|
63
|
+
/**
|
|
64
|
+
* Command arguments array
|
|
65
|
+
*
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
readonly args: Argument[] = []
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
*
|
|
72
|
+
* @param program
|
|
73
|
+
* @param name
|
|
74
|
+
* @param description
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
constructor(
|
|
78
|
+
program: Program,
|
|
79
|
+
name: string,
|
|
80
|
+
description: string,
|
|
81
|
+
config: Partial<CommandConfig> = {},
|
|
82
|
+
) {
|
|
83
|
+
this.program = program
|
|
84
|
+
this._name = name
|
|
85
|
+
this.description = description
|
|
86
|
+
this._config = createConfigurator({ visible: true, ...config })
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Add one or more aliases so the command can be called by different names.
|
|
91
|
+
*
|
|
92
|
+
* @param aliases Command aliases
|
|
93
|
+
*/
|
|
94
|
+
alias(...aliases: string[]): Command {
|
|
95
|
+
this._aliases.push(...aliases)
|
|
96
|
+
return this
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Name getter. Will return an empty string in the program-command context
|
|
101
|
+
*
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
get name(): string {
|
|
105
|
+
return this.isProgramCommand() ? "" : this._name
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Add an argument to the command.
|
|
110
|
+
* Synopsis is a string like `<my-argument>` or `[my-argument]`.
|
|
111
|
+
* Angled brackets (e.g. `<item>`) indicate required input. Square brackets (e.g. `[env]`) indicate optional input.
|
|
112
|
+
*
|
|
113
|
+
* Returns the {@link Command} object to facilitate chaining of methods.
|
|
114
|
+
*
|
|
115
|
+
* @param synopsis Argument synopsis.
|
|
116
|
+
* @param description - Argument description.
|
|
117
|
+
* @param [options] - Optional parameters including validator and default value.
|
|
118
|
+
*/
|
|
119
|
+
argument(
|
|
120
|
+
synopsis: string,
|
|
121
|
+
description: string,
|
|
122
|
+
options: CreateArgumentOpts = {},
|
|
123
|
+
): Command {
|
|
124
|
+
this._lastAddedArgOrOpt = createArgument(synopsis, description, options)
|
|
125
|
+
this.args.push(this._lastAddedArgOrOpt)
|
|
126
|
+
return this
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Set the corresponding action to execute for this command
|
|
131
|
+
*
|
|
132
|
+
* @param action Action to execute
|
|
133
|
+
*/
|
|
134
|
+
action(action: Action): Command {
|
|
135
|
+
this._action = action
|
|
136
|
+
return this
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Allow chaining command() calls. See {@link Program.command}.
|
|
141
|
+
*
|
|
142
|
+
*/
|
|
143
|
+
command(
|
|
144
|
+
name: string,
|
|
145
|
+
description: string,
|
|
146
|
+
config: Partial<CommandConfig> = {},
|
|
147
|
+
): Command {
|
|
148
|
+
return this.program.command(name, description, config)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Makes the command the default one for the program.
|
|
153
|
+
*/
|
|
154
|
+
default(): Command {
|
|
155
|
+
this.program.defaultCommand = this
|
|
156
|
+
return this
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Checks if the command has the given alias registered.
|
|
161
|
+
*
|
|
162
|
+
* @param alias
|
|
163
|
+
* @internal
|
|
164
|
+
*/
|
|
165
|
+
hasAlias(alias: string): boolean {
|
|
166
|
+
return this._aliases.includes(alias)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Get command aliases.
|
|
171
|
+
* @internal
|
|
172
|
+
*/
|
|
173
|
+
getAliases(): string[] {
|
|
174
|
+
return this._aliases
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* @internal
|
|
179
|
+
*/
|
|
180
|
+
isProgramCommand(): boolean {
|
|
181
|
+
return this._name === PROG_CMD
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @internal
|
|
186
|
+
*/
|
|
187
|
+
isHelpCommand(): boolean {
|
|
188
|
+
return this._name === HELP_CMD
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Hide the command from help.
|
|
193
|
+
* Shortcut to calling `.configure({ visible: false })`.
|
|
194
|
+
*/
|
|
195
|
+
hide(): Command {
|
|
196
|
+
return this.configure({ visible: false })
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Add an option to the current command.
|
|
201
|
+
*
|
|
202
|
+
* @param synopsis Option synopsis like '-f, --force', or '-f, --file \<file\>', or '--with-openssl [path]'
|
|
203
|
+
* @param description Option description
|
|
204
|
+
* @param options Additional parameters
|
|
205
|
+
*/
|
|
206
|
+
option(
|
|
207
|
+
synopsis: string,
|
|
208
|
+
description: string,
|
|
209
|
+
options: CreateOptionCommandOpts = {},
|
|
210
|
+
): Command {
|
|
211
|
+
const opt = (this._lastAddedArgOrOpt = createOption(synopsis, description, options))
|
|
212
|
+
this.options.push(opt)
|
|
213
|
+
return this
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @internal
|
|
218
|
+
*/
|
|
219
|
+
getParserConfig(): Partial<ParserOptions> {
|
|
220
|
+
const defaults: ParserOptions = {
|
|
221
|
+
boolean: [],
|
|
222
|
+
string: [],
|
|
223
|
+
alias: getOptsMapping(this),
|
|
224
|
+
autoCast: this.autoCast,
|
|
225
|
+
variadic: [],
|
|
226
|
+
ddash: false,
|
|
227
|
+
}
|
|
228
|
+
let parserOpts = this.options.reduce((parserOpts, opt) => {
|
|
229
|
+
if (opt.boolean) {
|
|
230
|
+
parserOpts.boolean.push(opt.name)
|
|
231
|
+
}
|
|
232
|
+
if (isStringValidator(opt.validator)) {
|
|
233
|
+
parserOpts.string.push(opt.name)
|
|
234
|
+
}
|
|
235
|
+
if (opt.variadic) {
|
|
236
|
+
parserOpts.variadic.push(opt.name)
|
|
237
|
+
}
|
|
238
|
+
return parserOpts
|
|
239
|
+
}, defaults)
|
|
240
|
+
|
|
241
|
+
parserOpts = this.args.reduce((parserOpts, arg, index) => {
|
|
242
|
+
if (!this.isProgramCommand()) {
|
|
243
|
+
index++
|
|
244
|
+
}
|
|
245
|
+
if (isBoolValidator(arg.validator)) {
|
|
246
|
+
parserOpts.boolean.push(index)
|
|
247
|
+
}
|
|
248
|
+
if (isStringValidator(arg.validator)) {
|
|
249
|
+
parserOpts.string.push(index)
|
|
250
|
+
}
|
|
251
|
+
if (arg.variadic) {
|
|
252
|
+
parserOpts.variadic.push(index)
|
|
253
|
+
}
|
|
254
|
+
return parserOpts
|
|
255
|
+
}, parserOpts)
|
|
256
|
+
|
|
257
|
+
return parserOpts
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Return a reformated synopsis string
|
|
262
|
+
* @internal
|
|
263
|
+
*/
|
|
264
|
+
get synopsis(): string {
|
|
265
|
+
const opts = this.options.length
|
|
266
|
+
? this.options.some((f) => f.required)
|
|
267
|
+
? "<OPTIONS...>"
|
|
268
|
+
: "[OPTIONS...]"
|
|
269
|
+
: ""
|
|
270
|
+
const name = this._name !== PROG_CMD ? " " + this._name : ""
|
|
271
|
+
|
|
272
|
+
return (
|
|
273
|
+
this.program.getBin() +
|
|
274
|
+
name +
|
|
275
|
+
" " +
|
|
276
|
+
this.args.map((a) => a.synopsis).join(" ") +
|
|
277
|
+
" " +
|
|
278
|
+
opts
|
|
279
|
+
).trim()
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Customize command help. Can be called multiple times to add more paragraphs and/or sections.
|
|
284
|
+
*
|
|
285
|
+
* @param text Help contents
|
|
286
|
+
* @param options Display options
|
|
287
|
+
*/
|
|
288
|
+
help(text: string, options: Partial<CustomizedHelpOpts> = {}): Command {
|
|
289
|
+
customizeHelp(this, text, options)
|
|
290
|
+
return this
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Configure some behavioral properties.
|
|
295
|
+
*
|
|
296
|
+
* @param props properties to set/update
|
|
297
|
+
*/
|
|
298
|
+
configure(props: Partial<CommandConfig>): Command {
|
|
299
|
+
this._config.set(props)
|
|
300
|
+
return this
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Get a configuration property value.
|
|
305
|
+
*
|
|
306
|
+
* @internal
|
|
307
|
+
* @param key Property key to get value for. See {@link CommandConfig}.
|
|
308
|
+
*/
|
|
309
|
+
getConfigProperty<K extends keyof CommandConfig>(key: K): CommandConfig[K] {
|
|
310
|
+
return this._config.get(key)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Get the auto-casting flag.
|
|
315
|
+
*
|
|
316
|
+
* @internal
|
|
317
|
+
*/
|
|
318
|
+
get autoCast(): boolean {
|
|
319
|
+
return (
|
|
320
|
+
this.getConfigProperty("autoCast") ?? this.program.getConfigProperty("autoCast")
|
|
321
|
+
)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Auto-complete
|
|
326
|
+
*/
|
|
327
|
+
complete(completer: Completer): Command {
|
|
328
|
+
if (!this._lastAddedArgOrOpt) {
|
|
329
|
+
throw new Error(
|
|
330
|
+
"Caporal setup error: you should only call `.complete()` after .argument() or .option().",
|
|
331
|
+
)
|
|
332
|
+
}
|
|
333
|
+
registerCompletion(this._lastAddedArgOrOpt, completer)
|
|
334
|
+
return this
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Toggle strict mode.
|
|
339
|
+
* Shortcut to calling: `.configure({ strictArgsCount: strict, strictOptions: strict }).
|
|
340
|
+
* By default, strict settings are not defined for commands, and inherit from the
|
|
341
|
+
* program settings. Calling `.strict(value)` on a command will override the program
|
|
342
|
+
* settings.
|
|
343
|
+
*
|
|
344
|
+
* @param strict boolean enabled flag
|
|
345
|
+
*/
|
|
346
|
+
strict(strict = true): Command {
|
|
347
|
+
return this.configure({
|
|
348
|
+
strictArgsCount: strict,
|
|
349
|
+
strictOptions: strict,
|
|
350
|
+
})
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Computed strictOptions flag.
|
|
355
|
+
*
|
|
356
|
+
* @internal
|
|
357
|
+
*/
|
|
358
|
+
get strictOptions(): boolean {
|
|
359
|
+
return (
|
|
360
|
+
this.getConfigProperty("strictOptions") ??
|
|
361
|
+
this.program.getConfigProperty("strictOptions")
|
|
362
|
+
)
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Computed strictArgsCount flag.
|
|
366
|
+
*
|
|
367
|
+
* @internal
|
|
368
|
+
*/
|
|
369
|
+
get strictArgsCount(): boolean {
|
|
370
|
+
return (
|
|
371
|
+
this.getConfigProperty("strictArgsCount") ??
|
|
372
|
+
this.program.getConfigProperty("strictArgsCount")
|
|
373
|
+
)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Enable or disable auto casting of arguments & options for the command.
|
|
378
|
+
* This is basically a shortcut to calling `command.configure({ autoCast: enabled })`.
|
|
379
|
+
* By default, auto-casting is inherited from the program configuration.
|
|
380
|
+
* This method allows overriding what's been set on the program level.
|
|
381
|
+
*
|
|
382
|
+
* @param enabled
|
|
383
|
+
*/
|
|
384
|
+
cast(enabled: boolean): Command {
|
|
385
|
+
return this.configure({ autoCast: enabled })
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Visible flag
|
|
390
|
+
*
|
|
391
|
+
* @internal
|
|
392
|
+
*/
|
|
393
|
+
get visible(): boolean {
|
|
394
|
+
return this.getConfigProperty("visible")
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Run the action associated with the command
|
|
399
|
+
*
|
|
400
|
+
* @internal
|
|
401
|
+
*/
|
|
402
|
+
async run(parsed: Partial<ParserResult>): Promise<unknown> {
|
|
403
|
+
const data: ParserResult = {
|
|
404
|
+
args: [],
|
|
405
|
+
options: {},
|
|
406
|
+
line: "",
|
|
407
|
+
rawOptions: {},
|
|
408
|
+
rawArgv: [],
|
|
409
|
+
ddash: [],
|
|
410
|
+
...parsed,
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
try {
|
|
414
|
+
// Validate args and options, including global options
|
|
415
|
+
const result = await validateCall(this, data)
|
|
416
|
+
const { args, options, ddash, errors } = result
|
|
417
|
+
|
|
418
|
+
// Process any global options
|
|
419
|
+
const shouldStop = await processGlobalOptions(result, this.program, this)
|
|
420
|
+
if (shouldStop) {
|
|
421
|
+
return -1
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (errors.length) {
|
|
425
|
+
throw new ValidationSummaryError(this, errors)
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (!this._action) {
|
|
429
|
+
throw new NoActionError(this)
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return await this._action({
|
|
433
|
+
args,
|
|
434
|
+
options,
|
|
435
|
+
ddash,
|
|
436
|
+
logger,
|
|
437
|
+
program: this.program,
|
|
438
|
+
command: this,
|
|
439
|
+
})
|
|
440
|
+
} catch (err) {
|
|
441
|
+
const ctor = Object.getPrototypeOf(err).constructor.name
|
|
442
|
+
throw err instanceof BaseError && ctor !== "Error"
|
|
443
|
+
? err
|
|
444
|
+
: new ActionError(err as Error)
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Create a new command
|
|
451
|
+
*
|
|
452
|
+
* @internal
|
|
453
|
+
*/
|
|
454
|
+
export function createCommand(
|
|
455
|
+
...args: ConstructorParameters<typeof Command>
|
|
456
|
+
): InstanceType<typeof Command> {
|
|
457
|
+
return new Command(...args)
|
|
458
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
import path from "path"
|
|
6
|
+
import zipObject from "lodash/zipObject"
|
|
7
|
+
import map from "lodash/map"
|
|
8
|
+
import { readdir } from "../utils/fs"
|
|
9
|
+
import { importCommand } from "./import"
|
|
10
|
+
import { createCommand } from "."
|
|
11
|
+
import type { Command } from "."
|
|
12
|
+
import type { Program } from "../program"
|
|
13
|
+
|
|
14
|
+
export async function scanCommands(
|
|
15
|
+
program: Program,
|
|
16
|
+
dirPath: string,
|
|
17
|
+
): Promise<Command[]> {
|
|
18
|
+
const files = await readdir(dirPath)
|
|
19
|
+
const imp = await Promise.all(files.map((f) => importCommand(path.join(dirPath, f))))
|
|
20
|
+
const data = zipObject(files, imp)
|
|
21
|
+
return map(data, (cmdBuilder, filename) => {
|
|
22
|
+
const { dir, name } = path.parse(filename)
|
|
23
|
+
const cmd = dir ? [...dir.split("/"), name].join(" ") : name
|
|
24
|
+
const options = {
|
|
25
|
+
createCommand: createCommand.bind(null, program, cmd),
|
|
26
|
+
program,
|
|
27
|
+
}
|
|
28
|
+
return cmdBuilder(options)
|
|
29
|
+
})
|
|
30
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
import { ParserResult, ParserProcessedResult } from "../types"
|
|
6
|
+
import { validateArgs } from "../argument/validate"
|
|
7
|
+
import { validateOptions } from "../option/validate"
|
|
8
|
+
import type { Command } from "."
|
|
9
|
+
|
|
10
|
+
export async function validateCall(
|
|
11
|
+
cmd: Command,
|
|
12
|
+
result: ParserResult,
|
|
13
|
+
): Promise<ParserProcessedResult> {
|
|
14
|
+
const { args: parsedArgs, options: parsedFlags } = result
|
|
15
|
+
// check if there are some global flags to handle
|
|
16
|
+
const { options: flags, errors: flagsErrors } = await validateOptions(cmd, parsedFlags)
|
|
17
|
+
const { args, errors: argsErrors } = await validateArgs(cmd, parsedArgs)
|
|
18
|
+
return { ...result, args, options: flags, errors: [...argsErrors, ...flagsErrors] }
|
|
19
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
import type { BaseConfig, Configurator } from "../types"
|
|
6
|
+
|
|
7
|
+
export function createConfigurator<T extends BaseConfig>(defaults: T): Configurator<T> {
|
|
8
|
+
const _defaults = defaults
|
|
9
|
+
let config = defaults
|
|
10
|
+
return {
|
|
11
|
+
reset(): T {
|
|
12
|
+
config = _defaults
|
|
13
|
+
return config
|
|
14
|
+
},
|
|
15
|
+
get<K extends keyof T>(key: K): T[K] {
|
|
16
|
+
return config[key]
|
|
17
|
+
},
|
|
18
|
+
getAll(): T {
|
|
19
|
+
return config
|
|
20
|
+
},
|
|
21
|
+
set(props: Partial<T>): T {
|
|
22
|
+
config = { ...config, ...props }
|
|
23
|
+
return config
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { fatalError } from "../fatal"
|
|
2
|
+
import { BaseError } from "../base"
|
|
3
|
+
import { logger } from "../../logger"
|
|
4
|
+
import { expect, it, describe, afterEach, vi } from "vitest"
|
|
5
|
+
|
|
6
|
+
describe("fatalError()", () => {
|
|
7
|
+
const loggerLogSpy = vi.spyOn(logger, "log")
|
|
8
|
+
const loggerErrSpy = vi.spyOn(logger, "error")
|
|
9
|
+
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
logger.level = "info"
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it("should always call process.exit(1)", () => {
|
|
15
|
+
const err = new BaseError("my error")
|
|
16
|
+
fatalError(err)
|
|
17
|
+
expect(process.exitCode).toEqual(1)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it("should always logger.error in normal situation", () => {
|
|
21
|
+
const err = new BaseError("my error")
|
|
22
|
+
fatalError(err)
|
|
23
|
+
expect(loggerErrSpy).toHaveBeenCalledWith(err.message)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it("should always logger.log in debug mode, with more info", () => {
|
|
27
|
+
const err = new BaseError("my error")
|
|
28
|
+
logger.level = "debug"
|
|
29
|
+
fatalError(err)
|
|
30
|
+
expect(loggerLogSpy).toHaveBeenCalledWith(
|
|
31
|
+
expect.objectContaining({
|
|
32
|
+
message: expect.stringContaining("my error"),
|
|
33
|
+
stack: err.stack,
|
|
34
|
+
name: "BaseError",
|
|
35
|
+
}),
|
|
36
|
+
)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { BaseError } from "./base"
|
|
7
|
+
|
|
8
|
+
export class ActionError extends BaseError {
|
|
9
|
+
constructor(error: string | Error) {
|
|
10
|
+
const message = typeof error === "string" ? error : error.message
|
|
11
|
+
super(message, { error })
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { ErrorMetadata } from "../types"
|
|
7
|
+
|
|
8
|
+
export type CommonError = Error | BaseError
|
|
9
|
+
|
|
10
|
+
export class BaseError extends Error {
|
|
11
|
+
public meta: ErrorMetadata
|
|
12
|
+
|
|
13
|
+
constructor(message: string, meta: ErrorMetadata = {}) {
|
|
14
|
+
super(message)
|
|
15
|
+
Object.setPrototypeOf(this, new.target.prototype)
|
|
16
|
+
this.name = this.constructor.name
|
|
17
|
+
this.meta = meta
|
|
18
|
+
Error.captureStackTrace(this, this.constructor)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { logger } from "../logger"
|
|
7
|
+
import type { BaseError } from "./base"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param err - Error object
|
|
11
|
+
*/
|
|
12
|
+
export function fatalError(error: BaseError): void {
|
|
13
|
+
if (logger.level == "debug") {
|
|
14
|
+
logger.log({
|
|
15
|
+
level: "error",
|
|
16
|
+
...error,
|
|
17
|
+
message: error.message + "\n\n" + error.stack,
|
|
18
|
+
stack: error.stack,
|
|
19
|
+
name: error.name,
|
|
20
|
+
})
|
|
21
|
+
} else {
|
|
22
|
+
logger.error(error.message)
|
|
23
|
+
}
|
|
24
|
+
process.exitCode = 1
|
|
25
|
+
}
|