From 82dbf0128f54fc59c70b362e49defc986d2a74db Mon Sep 17 00:00:00 2001 From: gbuomprisco Date: Fri, 28 Jun 2024 00:20:35 +0800 Subject: [PATCH] Refactor Keystatic configuration and update turbo.json The keystatic.config.ts has been again refactor with new field validations and a new dedicated function to get collections. The "turbo.json" file is also updated with a new "ui" field. The refactor aims to streamline the collection generation, and the "ui" field in "turbo.json" enhances its configuration capabilities. --- .../cms/keystatic/src/keystatic.config.ts | 121 ++++++++++-------- turbo.json | 1 + 2 files changed, 69 insertions(+), 53 deletions(-) diff --git a/packages/cms/keystatic/src/keystatic.config.ts b/packages/cms/keystatic/src/keystatic.config.ts index cbef49825..b94aea03a 100644 --- a/packages/cms/keystatic/src/keystatic.config.ts +++ b/packages/cms/keystatic/src/keystatic.config.ts @@ -17,7 +17,9 @@ const local = z.object({ const cloud = z.object({ kind: z.literal('cloud'), - project: z.string(), + project: z.string().min(1), + branchPrefix: z.string().optional(), + pathPrefix: z.string().optional(), }) satisfies ZodOutputFor; const github = z.object({ @@ -26,6 +28,8 @@ const github = z.object({ branchPrefix: z.string().optional(), pathPrefix: z.string().optional(), githubToken: z.string({ + description: + 'The GitHub token to use for authentication with the GitHub API', required_error: 'Please provide a GitHub token', }), }) satisfies ZodOutputFor; @@ -82,59 +86,70 @@ function createKeyStaticConfig(path = '') { path += '/'; } + const cloud = { + project: storage.kind === 'cloud' ? storage.project : '', + }; + + const collections = getKeystaticCollections(path); + return config({ storage, - collections: { - posts: collection({ - label: 'Posts', - slugField: 'title', - path: `${path}posts/*`, - format: { contentField: 'content' }, - schema: { - title: fields.slug({ name: { label: 'Title' } }), - image: fields.image({ - label: 'Image', - directory: 'public/site/images', - publicPath: '/site/images', - }), - categories: fields.array(fields.text({ label: 'Category' })), - tags: fields.array(fields.text({ label: 'Tag' })), - description: fields.text({ label: 'Description' }), - publishedAt: fields.date({ label: 'Published At' }), - parent: fields.relationship({ - label: 'Parent', - collection: 'posts', - }), - language: fields.text({ label: 'Language' }), - order: fields.number({ label: 'Order' }), - content: getContentField(), - }, - }), - documentation: collection({ - label: 'Documentation', - slugField: 'title', - path: `${path}documentation/**`, - format: { contentField: 'content' }, - schema: { - title: fields.slug({ name: { label: 'Title' } }), - content: getContentField(), - image: fields.image({ - label: 'Image', - directory: 'public/site/images', - publicPath: '/site/images', - }), - description: fields.text({ label: 'Description' }), - publishedAt: fields.date({ label: 'Published At' }), - order: fields.number({ label: 'Order' }), - language: fields.text({ label: 'Language' }), - parent: fields.relationship({ - label: 'Parent', - collection: 'documentation', - }), - categories: fields.array(fields.text({ label: 'Category' })), - tags: fields.array(fields.text({ label: 'Tag' })), - }, - }), - }, + cloud, + collections, }); } + +function getKeystaticCollections(path: string) { + return { + posts: collection({ + label: 'Posts', + slugField: 'title', + path: `${path}posts/*`, + format: { contentField: 'content' }, + schema: { + title: fields.slug({ name: { label: 'Title' } }), + image: fields.image({ + label: 'Image', + directory: 'public/site/images', + publicPath: '/site/images', + }), + categories: fields.array(fields.text({ label: 'Category' })), + tags: fields.array(fields.text({ label: 'Tag' })), + description: fields.text({ label: 'Description' }), + publishedAt: fields.date({ label: 'Published At' }), + parent: fields.relationship({ + label: 'Parent', + collection: 'posts', + }), + language: fields.text({ label: 'Language' }), + order: fields.number({ label: 'Order' }), + content: getContentField(), + }, + }), + documentation: collection({ + label: 'Documentation', + slugField: 'title', + path: `${path}documentation/**`, + format: { contentField: 'content' }, + schema: { + title: fields.slug({ name: { label: 'Title' } }), + content: getContentField(), + image: fields.image({ + label: 'Image', + directory: 'public/site/images', + publicPath: '/site/images', + }), + description: fields.text({ label: 'Description' }), + publishedAt: fields.date({ label: 'Published At' }), + order: fields.number({ label: 'Order' }), + language: fields.text({ label: 'Language' }), + parent: fields.relationship({ + label: 'Parent', + collection: 'documentation', + }), + categories: fields.array(fields.text({ label: 'Category' })), + tags: fields.array(fields.text({ label: 'Tag' })), + }, + }), + }; +} diff --git a/turbo.json b/turbo.json index b98709af5..a4231f693 100644 --- a/turbo.json +++ b/turbo.json @@ -3,6 +3,7 @@ "globalDependencies": [ "**/.env" ], + "ui": "stream", "globalEnv": [ "STRIPE_SECRET_KEY", "STRIPE_WEBHOOK_SECRET",