diff --git a/packages/init/src/SliceMachineInitProcess.ts b/packages/init/src/SliceMachineInitProcess.ts index dbbee5e38d..ac2022e15e 100644 --- a/packages/init/src/SliceMachineInitProcess.ts +++ b/packages/init/src/SliceMachineInitProcess.ts @@ -17,6 +17,7 @@ import { SliceMachineManager, PackageManager, StarterId, + REPOSITORY_NAME_VALIDATION, } from "@slicemachine/manager"; import pkg from "../package.json"; @@ -683,7 +684,7 @@ export class SliceMachineInitProcess { this.manager.prismicRepository.checkExists({ domain }), }); - if (validation.LessThan4 || validation.MoreThan30) { + if (validation.Min || validation.Max) { const errorMessage = getErrorMessageForRepositoryDomainValidation({ validation: { ...validation, @@ -846,17 +847,25 @@ export class SliceMachineInitProcess { const domain = formatRepositoryDomain(rawDomain); const validation = validateRepositoryDomain({ domain }); - const minRule = validation.LessThan4 + const minRule = validation.Min ? chalk.red( - `1. Name must be ${chalk.bold("4 characters long or more")}`, + `1. Name must be ${chalk.bold( + REPOSITORY_NAME_VALIDATION.min + " characters long or more", + )}`, ) - : `1. Name must be ${chalk.cyan("4 characters long or more")}`; + : `1. Name must be ${chalk.cyan( + REPOSITORY_NAME_VALIDATION.min + " characters long or more", + )}`; - const maxRule = validation.MoreThan30 + const maxRule = validation.Max ? chalk.red( - `1. Name must be ${chalk.bold("30 characters long or less")}`, + `1. Name must be ${chalk.bold( + REPOSITORY_NAME_VALIDATION.max + " characters long or less", + )}`, ) - : `1. Name must be ${chalk.cyan("30 characters long or less")}`; + : `1. Name must be ${chalk.cyan( + REPOSITORY_NAME_VALIDATION.max + " characters long or less", + )}`; this.msg = chalk.reset( ` diff --git a/packages/init/src/lib/repositoryDomain.ts b/packages/init/src/lib/repositoryDomain.ts index f1d9e70e4c..dcd4b1b9d7 100644 --- a/packages/init/src/lib/repositoryDomain.ts +++ b/packages/init/src/lib/repositoryDomain.ts @@ -1,3 +1,5 @@ +import { REPOSITORY_NAME_VALIDATION } from "@slicemachine/manager"; + const abc123 = `abcdefghijklmnopqrstuvwxyz0123456789`; // 11 characters long or less adjectives @@ -114,8 +116,8 @@ const assertFormattedRepositoryDomain = ( }; export const ValidationErrors = { - LessThan4: "LessThan4", - MoreThan30: "MoreThan30", + Min: "Min", + Max: "Max", AlreadyExists: "AlreadyExists", } as const; type ValidationErrors = @@ -137,8 +139,8 @@ export const validateRepositoryDomain = ( * * @see https://regex101.com/r/OBZ6UH/1 */ - [ValidationErrors.LessThan4]: args.domain.length < 4, - [ValidationErrors.MoreThan30]: args.domain.length > 30, + [ValidationErrors.Min]: args.domain.length < REPOSITORY_NAME_VALIDATION.Min, + [ValidationErrors.Max]: args.domain.length > REPOSITORY_NAME_VALIDATION.Max, }; return { @@ -191,11 +193,15 @@ export const getErrorMessageForRepositoryDomainValidation = ( if (args.validation.hasErrors) { const formattedErrors: string[] = []; - if (args.validation.LessThan4) { - formattedErrors.push("must be 4 characters long or more"); + if (args.validation.Min) { + formattedErrors.push( + `must be ${REPOSITORY_NAME_VALIDATION.Min} characters long or more`, + ); } - if (args.validation.MoreThan30) { - formattedErrors.push("must be 30 characters long or less"); + if (args.validation.Max) { + formattedErrors.push( + `must be ${REPOSITORY_NAME_VALIDATION.Max} characters long or less`, + ); } if ( ValidationErrors.AlreadyExists in args.validation && diff --git a/packages/init/test/SliceMachineInitProcess-useRepositoryFlag.test.ts b/packages/init/test/SliceMachineInitProcess-useRepositoryFlag.test.ts index 70ca48733c..f59cefa1f6 100644 --- a/packages/init/test/SliceMachineInitProcess-useRepositoryFlag.test.ts +++ b/packages/init/test/SliceMachineInitProcess-useRepositoryFlag.test.ts @@ -164,7 +164,7 @@ it("throws if repository name is too long", async () => { return initProcess.useRepositoryFlag(); }), ).rejects.toThrowErrorMatchingInlineSnapshot( - '"Repository name lorem-ipsum-dolor-sit-amet-consectetur-adipisicing-elit-officiis-incidunt-ex-harum must be 30 characters long or less"', + '"Repository name lorem-ipsum-dolor-sit-amet-consectetur-adipisicing-elit-officiis-incidunt-ex-harum must be 63 characters long or less"', ); }); diff --git a/packages/init/test/lib-repositoryDomain-getErrorMessageForRepositoryDomainValidation.test.ts b/packages/init/test/lib-repositoryDomain-getErrorMessageForRepositoryDomainValidation.test.ts index e1cecc72f4..de47935851 100644 --- a/packages/init/test/lib-repositoryDomain-getErrorMessageForRepositoryDomainValidation.test.ts +++ b/packages/init/test/lib-repositoryDomain-getErrorMessageForRepositoryDomainValidation.test.ts @@ -31,7 +31,7 @@ it("gets an error message when there is validation errors for the repository dom "lorem-ipsum-dolor-sit-amet-consectetur-adipisicing-elit-officiis-incidunt-ex-harum", }), ).toMatchInlineSnapshot( - '"Repository name lorem-ipsum-dolor-sit-amet-consectetur-adipisicing-elit-officiis-incidunt-ex-harum must be 30 characters long or less"', + '"Repository name lorem-ipsum-dolor-sit-amet-consectetur-adipisicing-elit-officiis-incidunt-ex-harum must be 63 characters long or less"', ); const existsValidation = await validateRepositoryDomainAndAvailability({ diff --git a/packages/manager/src/constants/REPOSITORY_NAME_VALIDATION.ts b/packages/manager/src/constants/REPOSITORY_NAME_VALIDATION.ts new file mode 100644 index 0000000000..4a15162164 --- /dev/null +++ b/packages/manager/src/constants/REPOSITORY_NAME_VALIDATION.ts @@ -0,0 +1,4 @@ +export const REPOSITORY_NAME_VALIDATION = { + Min: 4, + Max: 63, +}; diff --git a/packages/manager/src/index.ts b/packages/manager/src/index.ts index fbaa6e37cb..942999c268 100644 --- a/packages/manager/src/index.ts +++ b/packages/manager/src/index.ts @@ -29,3 +29,5 @@ export { DecodeError } from "./lib/DecodeError"; export type { SliceMachineConfig, PackageManager } from "./types"; export type { APIEndpoints } from "./constants/API_ENDPOINTS"; + +export { REPOSITORY_NAME_VALIDATION } from "./constants/REPOSITORY_NAME_VALIDATION"; diff --git a/packages/manager/src/managers/prismicRepository/PrismicRepositoryManager.ts b/packages/manager/src/managers/prismicRepository/PrismicRepositoryManager.ts index a5ab911759..d876482828 100644 --- a/packages/manager/src/managers/prismicRepository/PrismicRepositoryManager.ts +++ b/packages/manager/src/managers/prismicRepository/PrismicRepositoryManager.ts @@ -7,6 +7,7 @@ import { serializeCookies } from "../../lib/serializeCookies"; import { SLICE_MACHINE_USER_AGENT } from "../../constants/SLICE_MACHINE_USER_AGENT"; import { API_ENDPOINTS } from "../../constants/API_ENDPOINTS"; +import { REPOSITORY_NAME_VALIDATION } from "../../constants/REPOSITORY_NAME_VALIDATION"; import { BaseManager } from "../BaseManager"; @@ -173,9 +174,13 @@ export class PrismicRepositoryManager extends BaseManager { }); const text = await res.text(); - // Endpoint returns repository name on success, which must be more than 4 characters and less than 30 + // Endpoint returns repository name on success, that should be within the validation range // Even when there is an error, we get a 200 success and so we have to check the name thanks to that - if (!res.ok || text.length < 4 || text.length > 63) { + if ( + !res.ok || + text.length < REPOSITORY_NAME_VALIDATION.Min || + text.length > REPOSITORY_NAME_VALIDATION.Max + ) { throw new Error(`Failed to create repository \`${args.domain}\``, { cause: text, });