From a8407920bc079e4e71a87b1bc093cc71f3734003 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Sat, 11 Nov 2023 11:19:11 -0800 Subject: [PATCH] Enhancement: Better content type creation for Astro #704 --- src/constants/ContentType.ts | 2 +- .../components/Steps/SelectItem.tsx | 14 +----- src/listeners/dashboard/SsgListener.ts | 49 ++++++++++++++----- src/models/AstroCollections.ts | 1 + ssg-scripts/astro.collections.mjs | 33 +------------ 5 files changed, 43 insertions(+), 56 deletions(-) diff --git a/src/constants/ContentType.ts b/src/constants/ContentType.ts index 258feb09..c8880ecd 100644 --- a/src/constants/ContentType.ts +++ b/src/constants/ContentType.ts @@ -3,7 +3,7 @@ import { ContentType } from './../models/PanelSettings'; export const DEFAULT_CONTENT_TYPE_NAME = 'default'; export const DEFAULT_CONTENT_TYPE: ContentType = { - name: 'default', + name: DEFAULT_CONTENT_TYPE_NAME, pageBundle: false, previewPath: null, fields: [ diff --git a/src/dashboardWebView/components/Steps/SelectItem.tsx b/src/dashboardWebView/components/Steps/SelectItem.tsx index 0adbc032..05ae5d2b 100644 --- a/src/dashboardWebView/components/Steps/SelectItem.tsx +++ b/src/dashboardWebView/components/Steps/SelectItem.tsx @@ -4,7 +4,6 @@ import { CheckCircleIcon as CheckCircleIconSolid, PlusCircleIcon as PlusCircleIc export interface ISelectItemProps { title: string; - icon?: "add" | "select"; buttonTitle: string; isSelected: boolean; disabled?: boolean; @@ -13,7 +12,6 @@ export interface ISelectItemProps { export const SelectItem: React.FunctionComponent = ({ title, - icon = "select", buttonTitle, isSelected, disabled, @@ -30,17 +28,9 @@ export const SelectItem: React.FunctionComponent = ({ disabled={disabled} > {isSelected ? ( - icon === "add" ? ( - - ) : ( - - ) + ) : ( - icon === "add" ? ( - - ) : ( - - ) + )} {title} diff --git a/src/listeners/dashboard/SsgListener.ts b/src/listeners/dashboard/SsgListener.ts index 29a3ce1a..49d86b30 100644 --- a/src/listeners/dashboard/SsgListener.ts +++ b/src/listeners/dashboard/SsgListener.ts @@ -5,7 +5,11 @@ import { BaseListener } from './BaseListener'; import { exec } from 'child_process'; import { Extension, Logger, Settings } from '../../helpers'; import { Folders } from '../../commands'; -import { SETTING_TAXONOMY_CONTENT_TYPES, SsgScripts } from '../../constants'; +import { + DEFAULT_CONTENT_TYPE_NAME, + SETTING_TAXONOMY_CONTENT_TYPES, + SsgScripts +} from '../../constants'; import { SettingsListener } from './SettingsListener'; import { Terminal } from '../../services'; import { existsAsync, readFileAsync } from '../../utils'; @@ -58,7 +62,10 @@ export class SsgListener extends BaseListener { } } - const contentTypes = Settings.get(SETTING_TAXONOMY_CONTENT_TYPES) || []; + let contentTypes = Settings.get(SETTING_TAXONOMY_CONTENT_TYPES) || []; + + // Filter out the default content type + contentTypes = contentTypes.filter((ct) => ct.name !== DEFAULT_CONTENT_TYPE_NAME); if (contentTypes.find((ct) => ct.name === collection.name)) { SsgListener.sendRequest(command as any, requestId, {}); @@ -150,7 +157,7 @@ export class SsgListener extends BaseListener { // Update the vite reference, as it is not a direct dependency of the project let scriptContents = await readFileAsync(scriptPath.fsPath, 'utf8'); - scriptContents = scriptContents.replace(`"vite"`, `"${vitePath}"`); + scriptContents = scriptContents.replace(`'vite'`, `'${vitePath}'`); await workspace.fs.writeFile(tempScriptPath, Buffer.from(scriptContents, 'utf8')); } } else { @@ -209,16 +216,35 @@ export class SsgListener extends BaseListener { } as Field; break; case 'ZodBoolean': - ctField = { - name: field.name, - type: 'boolean' - } as Field; + if (field.name === 'published') { + ctField = { + name: field.name, + type: 'draft' + } as Field; + } else { + ctField = { + name: field.name, + type: 'boolean' + } as Field; + } break; case 'ZodArray': - ctField = { - name: field.name, - type: 'list' - } as Field; + if (field.name === 'tags') { + ctField = { + name: field.name, + type: 'tags' + } as Field; + } else if (field.name === 'categories') { + ctField = { + name: field.name, + type: 'categories' + } as Field; + } else { + ctField = { + name: field.name, + type: 'list' + } as Field; + } break; case 'ZodEnum': ctField = { @@ -227,6 +253,7 @@ export class SsgListener extends BaseListener { choices: field.options || [] } as Field; break; + case 'datetime': case 'ZodDate': ctField = { name: field.name, diff --git a/src/models/AstroCollections.ts b/src/models/AstroCollections.ts index f07b9c3f..b91fcc75 100644 --- a/src/models/AstroCollections.ts +++ b/src/models/AstroCollections.ts @@ -14,6 +14,7 @@ export interface AstroField { | 'ZodEnum' | 'ZodDate' | 'ZodObject' + | 'datetime' | 'email' | 'url' | 'image'; diff --git a/ssg-scripts/astro.collections.mjs b/ssg-scripts/astro.collections.mjs index b96d59a7..717270c6 100644 --- a/ssg-scripts/astro.collections.mjs +++ b/ssg-scripts/astro.collections.mjs @@ -3,8 +3,7 @@ import { join } from 'path'; import { createServer } from 'vite'; import zod from 'astro/zod'; -const { ZodDefault, ZodObject, ZodOptional, ZodString, ZodEffects, ZodEnum, ZodUnion, ZodArray } = - zod; +const { ZodDefault, ZodObject, ZodOptional, ZodString, ZodEffects, ZodEnum, ZodUnion } = zod; /** * Process the Zod field @@ -73,26 +72,6 @@ function generateFieldInfo(name, type) { required: !isFieldOptional }; - switch (fieldInfo.type) { - case 'ZodString': - fieldInfo.type = 'string'; - break; - case 'ZodNumber': - fieldInfo.type = 'number'; - break; - case 'ZodBoolean': - fieldInfo.type = 'boolean'; - break; - case 'ZodDate': - fieldInfo.type = 'datetime'; - break; - case 'ZodArray': - fieldInfo.type = 'choice'; - break; - default: - break; - } - if (fieldType instanceof ZodObject) { const subFields = extractFieldInfoFromShape(fieldType); @@ -111,16 +90,6 @@ function generateFieldInfo(name, type) { fieldInfo.options = fieldType.options; } - if (fieldType instanceof ZodArray) { - if (fieldInfo.name === 'tags') { - fieldInfo.type = 'tags'; - } else if (fieldInfo.name === 'categories') { - fieldInfo.type = 'categories'; - } else { - fieldInfo.options = fieldType.options; - } - } - if (fieldType instanceof ZodString) { // https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/string.ts#L45 if (fieldType._def.checks && fieldType._def.checks.length > 0) {