From b6f7c936216e080b13904cc0590ad8da4490a647 Mon Sep 17 00:00:00 2001 From: rharkor Date: Mon, 16 Sep 2024 10:27:28 +0200 Subject: [PATCH] refactor: config schemas --- packages/cli-app/src/api/_app.ts | 2 + packages/cli-app/src/api/stores/_router.ts | 23 +++++ packages/cli-app/src/api/stores/mutations.ts | 47 ++++++++++ packages/cli-app/src/api/stores/queries.ts | 18 ++++ packages/cli-app/src/api/stores/schemas.ts | 20 ++++ packages/cli-app/src/app/plugins/content.tsx | 2 +- packages/cli-app/src/app/stores/content.dr.ts | 1 + packages/cli-app/src/app/stores/content.tsx | 76 ++++++++++++--- packages/cli-app/src/app/stores/page.tsx | 3 +- packages/cli-app/src/langs/en.json | 3 +- packages/cli-app/src/langs/fr.json | 3 +- .../cli-app/src/lib/configuration/index.ts | 11 +-- packages/cli-app/src/lib/stores/index.ts | 93 ++++++++++-------- packages/cli-app/src/lib/templates/index.ts | 6 +- packages/cli-app/tsconfig.tsbuildinfo | 2 +- packages/cli/schemas/template.d.json | 18 ++-- packages/scripts/package.json | 2 - .../scripts/src/ci-check/cli-components.ts | 44 --------- .../scripts/src/ci-check/templates-config.ts | 94 ------------------- .../src/utils/template-config/index.ts | 12 ++- 20 files changed, 265 insertions(+), 215 deletions(-) create mode 100644 packages/cli-app/src/api/stores/_router.ts create mode 100644 packages/cli-app/src/api/stores/mutations.ts create mode 100644 packages/cli-app/src/api/stores/queries.ts create mode 100644 packages/cli-app/src/api/stores/schemas.ts delete mode 100644 packages/scripts/src/ci-check/cli-components.ts delete mode 100644 packages/scripts/src/ci-check/templates-config.ts diff --git a/packages/cli-app/src/api/_app.ts b/packages/cli-app/src/api/_app.ts index aae27e3f..4e10d78f 100644 --- a/packages/cli-app/src/api/_app.ts +++ b/packages/cli-app/src/api/_app.ts @@ -2,12 +2,14 @@ import { router } from "@/lib/trpc/init" import { configurationRouter } from "./configuration/_router" import { pluginsRouter } from "./plugins/_router" +import { storesRouter } from "./stores/_router" import { templatesRouter } from "./templates/_router" export const appRouter = router({ configuration: configurationRouter, plugins: pluginsRouter, templates: templatesRouter, + stores: storesRouter, }) export type AppRouter = typeof appRouter diff --git a/packages/cli-app/src/api/stores/_router.ts b/packages/cli-app/src/api/stores/_router.ts new file mode 100644 index 00000000..55f65218 --- /dev/null +++ b/packages/cli-app/src/api/stores/_router.ts @@ -0,0 +1,23 @@ +import { publicProcedure, router } from "@/lib/trpc/init" + +import { deleteStore, installOrUpdateStore } from "./mutations" +import { getStoresQuery } from "./queries" +import { + deleteStoreRequestSchema, + deleteStoreResponseSchema, + getStoresResponseSchema, + installOrUpdateStoreRequestSchema, + installOrUpdateStoreResponseSchema, +} from "./schemas" + +export const storesRouter = router({ + getStores: publicProcedure.output(getStoresResponseSchema()).query(getStoresQuery), + installOrUpdateStore: publicProcedure + .input(installOrUpdateStoreRequestSchema()) + .output(installOrUpdateStoreResponseSchema()) + .mutation(installOrUpdateStore), + deleteStore: publicProcedure + .input(deleteStoreRequestSchema()) + .output(deleteStoreResponseSchema()) + .mutation(deleteStore), +}) diff --git a/packages/cli-app/src/api/stores/mutations.ts b/packages/cli-app/src/api/stores/mutations.ts new file mode 100644 index 00000000..42aa7945 --- /dev/null +++ b/packages/cli-app/src/api/stores/mutations.ts @@ -0,0 +1,47 @@ +import { z } from "zod" + +import { getConfiguration, setConfiguration } from "@/lib/configuration" +import { handleDeleteStore, handleDownloadStore } from "@/lib/stores" +import { handleApiError } from "@/lib/utils/server-utils" +import { apiInputFromSchema } from "@/types" + +import { + deleteStoreRequestSchema, + deleteStoreResponseSchema, + installOrUpdateStoreRequestSchema, + installOrUpdateStoreResponseSchema, +} from "./schemas" + +export const installOrUpdateStore = async ({ + input: { store }, +}: apiInputFromSchema) => { + try { + await handleDownloadStore(store, true) + + const data: z.infer> = { success: true } + return data + } catch (error: unknown) { + return handleApiError(error) + } +} + +export const deleteStore = async ({ input: { store } }: apiInputFromSchema) => { + try { + const configuration = await getConfiguration() + + // Remove store from configuration + await setConfiguration({ + ...configuration, + plugins: configuration.plugins?.filter((p) => p.name !== store.name), + stores: configuration.stores?.filter((s) => s.name !== store.name), + }) + + // Delete form store folder + await handleDeleteStore(store) + + const data: z.infer> = { success: true } + return data + } catch (error: unknown) { + return handleApiError(error) + } +} diff --git a/packages/cli-app/src/api/stores/queries.ts b/packages/cli-app/src/api/stores/queries.ts new file mode 100644 index 00000000..e7b26a39 --- /dev/null +++ b/packages/cli-app/src/api/stores/queries.ts @@ -0,0 +1,18 @@ +import { z } from "zod" + +import { getStores } from "@/lib/stores" +import { handleApiError } from "@/lib/utils/server-utils" +import { apiInputFromSchema } from "@/types" + +import { getStoresResponseSchema } from "./schemas" + +export const getStoresQuery = async ({}: apiInputFromSchema) => { + try { + const stores = await getStores() + + const data: z.infer> = { stores } + return data + } catch (error: unknown) { + return handleApiError(error) + } +} diff --git a/packages/cli-app/src/api/stores/schemas.ts b/packages/cli-app/src/api/stores/schemas.ts new file mode 100644 index 00000000..dfddbe75 --- /dev/null +++ b/packages/cli-app/src/api/stores/schemas.ts @@ -0,0 +1,20 @@ +import { z } from "zod" + +import { fullStoreSchema } from "@/lib/stores" +import { storeConfigSchema } from "@next-boilerplate/scripts/utils/template-config/index.js" + +export const getStoresResponseSchema = () => z.object({ stores: z.array(fullStoreSchema) }) + +export const installOrUpdateStoreRequestSchema = () => + z.object({ + store: storeConfigSchema, + }) + +export const installOrUpdateStoreResponseSchema = () => z.object({ success: z.boolean() }) + +export const deleteStoreRequestSchema = () => + z.object({ + store: storeConfigSchema, + }) + +export const deleteStoreResponseSchema = () => z.object({ success: z.boolean() }) diff --git a/packages/cli-app/src/app/plugins/content.tsx b/packages/cli-app/src/app/plugins/content.tsx index 8d936ce0..38879f84 100644 --- a/packages/cli-app/src/app/plugins/content.tsx +++ b/packages/cli-app/src/app/plugins/content.tsx @@ -56,7 +56,7 @@ export default function PluginsContent({ ? [...Array(5)].map((_, i) => ( }) { const configuration = trpc.configuration.getConfiguration.useQuery(undefined, { initialData: ssrConfiguration, }) + const stores = trpc.stores.getStores.useQuery(undefined, { + initialData: ssrStores, + }) const [isAddStoreOpen, setIsAddStoreOpen] = useState(false) @@ -66,7 +71,30 @@ export default function StoresContent({ setIsAddStoreOpen(false) }) - const isPending = updateConfigurationMutation.isPending + const installOrUpdateStoreMutation = trpc.stores.installOrUpdateStore.useMutation({ + onSuccess: async () => { + await Promise.all([ + utils.configuration.invalidate(), + utils.stores.invalidate(), + utils.plugins.invalidate(), + utils.templates.invalidate(), + ]) + }, + }) + + const deleteStoreMutation = trpc.stores.deleteStore.useMutation({ + onSuccess: async () => { + await Promise.all([ + utils.configuration.invalidate(), + utils.stores.invalidate(), + utils.plugins.invalidate(), + utils.templates.invalidate(), + ]) + }, + }) + + const isPending = + updateConfigurationMutation.isPending || installOrUpdateStoreMutation.isPending || deleteStoreMutation.isPending return ( <> @@ -86,24 +114,44 @@ export default function StoresContent({ notClickable actions={ <> - + {stores.data.stores.some((s) => s.name === store.name && s.version === store.version) ? ( + + ) : ( + + )}