| 1 | import type { Validator } from "../types"; |
| 2 | import type { RunOptions } from "../../types"; |
| 3 | import { value, questions } from "./helpers.js"; |
| 4 | import { ValidationError, ValidationErrorEnum } from "../../errors.js"; |
| 5 | import { DEFAULT_OPTIONS } from "../../constants.js"; |
| 6 | |
| 7 | interface SpecDataValidation { |
| 8 | mode?: "command" | "filepath"; |
| 9 | specPath?: string; |
| 10 | command?: string; |
| 11 | } |
| 12 | |
| 13 | export const validateSpecData: Validator<SpecDataValidation> = async (options: RunOptions) => { |
| 14 | const { binaryPath, specPath, interactive, framework, subcommandName } = options; |
| 15 | if (!binaryPath && !specPath) { |
| 16 | if (interactive) { |
| 17 | return questions([ |
| 18 | { |
| 19 | name: "mode", |
| 20 | message: |
| 21 | "Do you want to generate the spec now from a command or add a path to a local spec TypeScript file?", |
| 22 | type: "select", |
| 23 | choices: [ |
| 24 | { |
| 25 | title: "I want to generate the spec", |
| 26 | value: "command", |
| 27 | }, |
| 28 | { |
| 29 | title: "I want to specify a path to a file", |
| 30 | value: "filepath", |
| 31 | }, |
| 32 | ], |
| 33 | }, |
| 34 | { |
| 35 | type: (_, prevValues) => (prevValues.mode === "filepath" ? "text" : null), |
| 36 | name: "specPath", |
| 37 | // TODO(fedeci): add TIP about passing this option via the flag and also using interactive mode |
| 38 | message: "Add the path to a local spec:", |
| 39 | }, |
| 40 | { |
| 41 | type: (_, prevValues) => (prevValues.mode === "command" ? "text" : null), |
| 42 | name: "command", |
| 43 | message: "Add the path to a local spec:", |
| 44 | }, |
| 45 | ]); |
| 46 | } |
| 47 | throw new ValidationError(ValidationErrorEnum.noSpecPassed); |
| 48 | } |
| 49 | if (binaryPath) { |
| 50 | return value({ |
| 51 | command: |
| 52 | framework || subcommandName |
| 53 | ? `${binaryPath} ${subcommandName ?? DEFAULT_OPTIONS.subcommandName}` |
| 54 | : binaryPath, |
| 55 | specPath, |
| 56 | }); |
| 57 | } |
| 58 | return value({ specPath }); |
| 59 | }; |