|
| 1 | +import { ValidCreateNextStackFlags } from "../create-next-stack-types" |
| 2 | +import { ThenArg } from "../helpers/then-arg" |
| 3 | +import { withKeyConstraint } from "../helpers/with-key-constraint" |
| 4 | +import { CategoryValue, promptOptionalCategories } from "./questions/categories" |
| 5 | +import { promptAnimation } from "./questions/categories/animation" |
| 6 | +import { promptContinuousIntegration } from "./questions/categories/continuous-integration" |
| 7 | +import { promptFormStateManagement } from "./questions/categories/form-state-management" |
| 8 | +import { promptFormatting } from "./questions/categories/formatting" |
1 | 9 | import { |
2 | | - PackageManagerOption, |
3 | | - StylingOption, |
4 | | - ValidCreateNextStackFlags, |
5 | | -} from "../create-next-stack-types" |
6 | | -import { promptTechnologies } from "./questions/technologies" |
| 10 | + MiscellaneousValue, |
| 11 | + promptMiscellaneous, |
| 12 | +} from "./questions/categories/miscellaneous" |
| 13 | +import { promptPackageManager } from "./questions/categories/package-manager" |
| 14 | +import { promptStyling } from "./questions/categories/styling" |
| 15 | + |
| 16 | +const categoryToPromptFunction = withKeyConstraint<CategoryValue>()({ |
| 17 | + formatting: promptFormatting, |
| 18 | + formStateManagement: promptFormStateManagement, |
| 19 | + animation: promptAnimation, |
| 20 | + continuousIntegration: promptContinuousIntegration, |
| 21 | +} as const) |
| 22 | + |
| 23 | +type PromptReturnType = ThenArg< |
| 24 | + ReturnType<typeof categoryToPromptFunction[CategoryValue]> |
| 25 | +> |
| 26 | +export type OptionalTechnology = PromptReturnType extends Array<unknown> |
| 27 | + ? PromptReturnType[number] |
| 28 | + : PromptReturnType |
7 | 29 |
|
8 | 30 | export const performFlagsQuestionnaire = |
9 | 31 | async (): Promise<ValidCreateNextStackFlags> => { |
10 | | - const technologies = await promptTechnologies() |
| 32 | + // Mandatory prompts |
| 33 | + const packageManager = await promptPackageManager() |
| 34 | + const stylingMethod = await promptStyling() |
11 | 35 |
|
12 | | - return { |
13 | | - "package-manager": getPackageManager(technologies), |
14 | | - prettier: technologies.includes("prettier"), |
15 | | - styling: getStyling(technologies), |
16 | | - "react-hook-form": technologies.includes("reactHookForm"), |
17 | | - formik: technologies.includes("formik"), |
18 | | - "framer-motion": technologies.includes("framerMotion"), |
19 | | - "github-actions": technologies.includes("githubActions"), |
20 | | - "formatting-pre-commit-hook": technologies.includes("preCommitHook"), |
| 36 | + // Optional categories prompt |
| 37 | + const optionalCategories = await promptOptionalCategories() |
| 38 | + const optionalTechnologies = new Set<OptionalTechnology>() |
| 39 | + for (const category of optionalCategories) { |
| 40 | + const additionalTechnologies = await categoryToPromptFunction[category]() |
| 41 | + additionalTechnologies.forEach((tech) => optionalTechnologies.add(tech)) |
21 | 42 | } |
22 | | - } |
23 | | - |
24 | | -type ThenArg<T> = T extends PromiseLike<infer U> ? U : T |
25 | 43 |
|
26 | | -const getPackageManager = ( |
27 | | - technologies: ThenArg<ReturnType<typeof promptTechnologies>> |
28 | | -): PackageManagerOption => { |
29 | | - // TODO: Strengthen typing. TypeScript throw error here when new package manager is added in promptTechnologies. |
30 | | - if (technologies.includes("yarn")) { |
31 | | - return "yarn" |
32 | | - } else if (technologies.includes("npm")) { |
33 | | - return "npm" |
34 | | - } else { |
35 | | - throw new Error("Package manager not found or not supported.") |
36 | | - } |
37 | | -} |
| 44 | + // TODO: Remove prettier-check when promptMiscellaneous adds more options |
| 45 | + let miscellaneous: Set<MiscellaneousValue> = new Set() |
| 46 | + if (optionalTechnologies.has("prettier")) { |
| 47 | + miscellaneous = await promptMiscellaneous(optionalTechnologies) |
| 48 | + } |
38 | 49 |
|
39 | | -const getStyling = ( |
40 | | - technologies: ThenArg<ReturnType<typeof promptTechnologies>> |
41 | | -): StylingOption => { |
42 | | - // TODO: Strengthen typing. TypeScript throw error here when new styling method is added in promptTechnologies. |
43 | | - if (technologies.includes("emotion")) { |
44 | | - return "emotion" |
45 | | - } else if (technologies.includes("styledComponents")) { |
46 | | - return "styled-components" |
47 | | - } else if (technologies.includes("cssModules")) { |
48 | | - return "css-modules" |
49 | | - } else if (technologies.includes("cssModulesWithSass")) { |
50 | | - return "css-modules-with-sass" |
51 | | - } else { |
52 | | - throw new Error("Styling method not found or not supported.") |
| 50 | + return { |
| 51 | + "package-manager": packageManager, |
| 52 | + styling: stylingMethod, |
| 53 | + prettier: optionalTechnologies.has("prettier"), |
| 54 | + "react-hook-form": optionalTechnologies.has("reactHookForm"), |
| 55 | + formik: optionalTechnologies.has("formik"), |
| 56 | + "framer-motion": optionalTechnologies.has("framerMotion"), |
| 57 | + "github-actions": optionalTechnologies.has("githubActions"), |
| 58 | + "formatting-pre-commit-hook": miscellaneous.has( |
| 59 | + "formattingPreCommitHook" |
| 60 | + ), |
| 61 | + } |
53 | 62 | } |
54 | | -} |
|
0 commit comments