Skip to content

Commit

Permalink
Enhancement: Better content type creation for Astro #704
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Nov 11, 2023
1 parent e1f6c90 commit a840792
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/constants/ContentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
14 changes: 2 additions & 12 deletions src/dashboardWebView/components/Steps/SelectItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +12,6 @@ export interface ISelectItemProps {

export const SelectItem: React.FunctionComponent<ISelectItemProps> = ({
title,
icon = "select",
buttonTitle,
isSelected,
disabled,
Expand All @@ -30,17 +28,9 @@ export const SelectItem: React.FunctionComponent<ISelectItemProps> = ({
disabled={disabled}
>
{isSelected ? (
icon === "add" ? (
<PlusCircleIconSolid className={`h-4 w-4`} />
) : (
<CheckCircleIconSolid className={`h-4 w-4`} />
)
<CheckCircleIconSolid className={`h-4 w-4`} />
) : (
icon === "add" ? (
<PlusCircleIcon className={`h-4 w-4`} />
) : (
<CheckCircleIcon className={`h-4 w-4`} />
)
<PlusCircleIcon className={`h-4 w-4`} />
)}
<span>{title}</span>
</button>
Expand Down
49 changes: 38 additions & 11 deletions src/listeners/dashboard/SsgListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -58,7 +62,10 @@ export class SsgListener extends BaseListener {
}
}

const contentTypes = Settings.get<ContentType[]>(SETTING_TAXONOMY_CONTENT_TYPES) || [];
let contentTypes = Settings.get<ContentType[]>(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, {});
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = {
Expand All @@ -227,6 +253,7 @@ export class SsgListener extends BaseListener {
choices: field.options || []
} as Field;
break;
case 'datetime':
case 'ZodDate':
ctField = {
name: field.name,
Expand Down
1 change: 1 addition & 0 deletions src/models/AstroCollections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface AstroField {
| 'ZodEnum'
| 'ZodDate'
| 'ZodObject'
| 'datetime'
| 'email'
| 'url'
| 'image';
Expand Down
33 changes: 1 addition & 32 deletions ssg-scripts/astro.collections.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down

0 comments on commit a840792

Please sign in to comment.