Debian Codegraph @

cli/publish-spec/src/validation/validators/name.ts

raw symbols SCIP
1 import path from "path";
2 import { ValidationError, GenericErrorEnum, ValidationErrorEnum } from "../../errors.js";
3 import { validateSpecName } from "../../utils.js";
4 import { value, questions } from "./helpers.js";
5 import type { RunOptions } from "../../types";
6 import type { Validator } from "../types";
7
8 interface NameValidation {
9 name: string;
10 }
11
12 export const validateName: Validator<NameValidation> = async (options: RunOptions) => {
13 const { name: optionalName, specPath, interactive } = options;
14 let name = optionalName;
15 try {
16 if (!name) {
17 if (specPath) {
18 // if name is missing we extract it from the optional spec-name
19 // NOTE: this won't work correctly for spec names that require being nested under a subfolder
20 // e.g. `@withfig/autocomplete-tools.ts`
21 name = path.basename(specPath, ".ts");
22 } else {
23 throw new ValidationError(GenericErrorEnum.missingName);
24 }
25 }
26 validateSpecName(name);
27 return value({ name });
28 } catch (error) {
29 if (!interactive) throw error;
30 return questions({
31 type: "text",
32 message: "Choose a name for the spec:",
33 name: "name",
34 validate: (v: string) => !v.includes(" ") ?? ValidationErrorEnum.nameWithSpace,
35 });
36 }
37 };