From 763e1b9004430a7c6a85c6bcd8cb7e5a241706e4 Mon Sep 17 00:00:00 2001 From: Alican Erdurmaz Date: Thu, 4 May 2023 15:45:41 +0300 Subject: [PATCH] feat: add vite (#342) * feat: add vite to prompt humanizer * feat: add sort project types * feat: update refine vite description --- src/Helper/humanize/index.ts | 14 +++++-- src/Helper/source/index.spec.ts | 67 ++++++++++++++++++++++++++++++++- src/Helper/source/index.ts | 26 ++++++++++++- 3 files changed, 102 insertions(+), 5 deletions(-) diff --git a/src/Helper/humanize/index.ts b/src/Helper/humanize/index.ts index fbe33d8d..6a07749b 100644 --- a/src/Helper/humanize/index.ts +++ b/src/Helper/humanize/index.ts @@ -15,11 +15,11 @@ export const HumanizeChoices = { description: "Creates a Next.js project", value: choice, }; - case "refine-react": + case "refine-vite": return { - title: "refine(CRA)", + title: "refine(Vite)", description: - "Creates a basic refine project (Recommended for CRUD applications)", + "Creates a refine React Vite project (Recommended for CRUD applications).", value: choice, }; case "refine-nextjs": @@ -36,6 +36,14 @@ export const HumanizeChoices = { "Creates a refine Remix project with SSR support (Recommended for CRUD applications)", value: choice, }; + case "refine-react": + return { + title: "refine(CRA) [Legacy]", + description: + "Creates a basic refine project (Recommended for CRUD applications)", + value: choice, + }; + default: return { title: choice, diff --git a/src/Helper/source/index.spec.ts b/src/Helper/source/index.spec.ts index d8b888ac..ed573fc8 100644 --- a/src/Helper/source/index.spec.ts +++ b/src/Helper/source/index.spec.ts @@ -1,5 +1,6 @@ import { promisify } from "util"; -import { get_source } from "./"; +import { get_source, sort_project_types } from "./"; + jest.mock("util", () => ({ promisify: jest.fn(), inherits: () => ({ @@ -7,6 +8,7 @@ jest.mock("util", () => ({ }), inspect: () => ({}), })); + describe("Source Helper", () => { it("incorrect source url/path", async () => { const source = await get_source("alibaba"); @@ -24,4 +26,67 @@ describe("Source Helper", () => { const source = await get_source("superplate-core-plugins"); expect(source.error).toBe(undefined); }); + + it("Sort project types", async () => { + const cases = [ + { + input: [ + { title: "Refine React", value: "refine-react" }, + { title: "Next.js", value: "nextjs" }, + { title: "React", value: "react" }, + { title: "Refine Vite", value: "refine-vite" }, + { title: "Refine Next.js", value: "refine-nextjs" }, + { title: "Refine Remix", value: "refine-remix" }, + ], + expectedOutput: [ + { title: "React", value: "react" }, + { title: "Next.js", value: "nextjs" }, + { title: "Refine Vite", value: "refine-vite" }, + { title: "Refine Next.js", value: "refine-nextjs" }, + { title: "Refine Remix", value: "refine-remix" }, + { title: "Refine React", value: "refine-react" }, + ], + }, + { + input: [ + { title: "Next.js", value: "nextjs" }, + { title: "Refine React", value: "refine-react" }, + { title: "Refine Next.js", value: "refine-nextjs" }, + { title: "Refine Remix", value: "refine-remix" }, + { title: "React", value: "react" }, + { title: "Refine Vite", value: "refine-vite" }, + ], + expectedOutput: [ + { title: "React", value: "react" }, + { title: "Next.js", value: "nextjs" }, + { title: "Refine Vite", value: "refine-vite" }, + { title: "Refine Next.js", value: "refine-nextjs" }, + { title: "Refine Remix", value: "refine-remix" }, + { title: "Refine React", value: "refine-react" }, + ], + }, + { + input: [ + { title: "Refine React", value: "refine-react" }, + { title: "Refine Next.js", value: "refine-nextjs" }, + { title: "Refine Remix", value: "refine-remix" }, + { title: "Refine Vite", value: "refine-vite" }, + ], + expectedOutput: [ + { title: "Refine Vite", value: "refine-vite" }, + { title: "Refine Next.js", value: "refine-nextjs" }, + { title: "Refine Remix", value: "refine-remix" }, + { title: "Refine React", value: "refine-react" }, + ], + }, + { + input: [], + expectedOutput: [], + }, + ]; + + cases.forEach((c) => { + expect(sort_project_types(c.input)).toEqual(c.expectedOutput); + }); + }); }); diff --git a/src/Helper/source/index.ts b/src/Helper/source/index.ts index f93e4880..5ff12004 100644 --- a/src/Helper/source/index.ts +++ b/src/Helper/source/index.ts @@ -104,6 +104,30 @@ export const is_multi_type = async ( return false; }; +export const sort_project_types = ( + projectTypes: { title: string; value: string }[], +): { title: string; value: string }[] => { + const order: Record = { + react: 1, + nextjs: 2, + "refine-vite": 3, + "refine-nextjs": 4, + "refine-remix": 5, + "refine-react": 6, + }; + + const newProjectTypes = [...projectTypes]; + + newProjectTypes.sort((a, b) => { + const firstValue = order[a?.value] ?? 1; + const secondValue = order[b?.value] ?? 1; + + return firstValue - secondValue; + }); + + return newProjectTypes; +}; + export const get_project_types = async (source: string): Promise => { const projectTypes: any[] = []; @@ -123,7 +147,7 @@ export const get_project_types = async (source: string): Promise => { } } - return projectTypes; + return sort_project_types(projectTypes); }; export const prompt_project_types = async (