From fee4b29787b6d831ff07a762f628c0b9920cc9a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Wed, 24 Jul 2024 10:52:53 +0300 Subject: [PATCH] feat: support object type in `template` schema --- packages/astro/src/default/utils/content.ts | 1 + packages/astro/src/remark/import-file.ts | 2 +- packages/runtime/src/lesson-files.ts | 3 ++- .../1-basics/1-introduction/1-welcome/content.md | 2 ++ packages/types/src/schemas/common.spec.ts | 11 ++++++++++- packages/types/src/schemas/common.ts | 10 +++++++++- 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/astro/src/default/utils/content.ts b/packages/astro/src/default/utils/content.ts index ea8aaa4b..03962f0c 100644 --- a/packages/astro/src/default/utils/content.ts +++ b/packages/astro/src/default/utils/content.ts @@ -242,6 +242,7 @@ export async function getTutorial(): Promise { const partMetadata = _tutorial.parts[lesson.part.id].data; const chapterMetadata = _tutorial.parts[lesson.part.id].chapters[lesson.chapter.id].data; + // now we inherit options from upper levels lesson.data = { ...lesson.data, ...squash( diff --git a/packages/astro/src/remark/import-file.ts b/packages/astro/src/remark/import-file.ts index 8447b746..0c177652 100644 --- a/packages/astro/src/remark/import-file.ts +++ b/packages/astro/src/remark/import-file.ts @@ -78,7 +78,7 @@ function getTemplateName(file: string) { ); if (meta.attributes.template) { - return meta.attributes.template; + return typeof meta.attributes.template === 'string' ? meta.attributes.template : meta.attributes.template.name; } /** diff --git a/packages/runtime/src/lesson-files.ts b/packages/runtime/src/lesson-files.ts index 422bcee4..119915db 100644 --- a/packages/runtime/src/lesson-files.ts +++ b/packages/runtime/src/lesson-files.ts @@ -42,7 +42,8 @@ export class LessonFilesFetcher { } async getLessonTemplate(lesson: Lesson): Promise { - const templatePathname = `template-${lesson.data.template}.json`; + const templateName = typeof lesson.data.template === 'string' ? lesson.data.template : lesson.data.template?.name; + const templatePathname = `template-${templateName}.json`; if (this._map.has(templatePathname)) { return this._map.get(templatePathname)!; diff --git a/packages/template/src/content/tutorial/1-basics/1-introduction/1-welcome/content.md b/packages/template/src/content/tutorial/1-basics/1-introduction/1-welcome/content.md index 9c35459b..14d17781 100644 --- a/packages/template/src/content/tutorial/1-basics/1-introduction/1-welcome/content.md +++ b/packages/template/src/content/tutorial/1-basics/1-introduction/1-welcome/content.md @@ -11,6 +11,8 @@ prepareCommands: - ['node -e setTimeout(()=>{process.exit(1)},5000)', 'This is going to fail'] terminal: panels: ['terminal', 'output'] +template: + name: default --- # Kitchen Sink [Heading 1] diff --git a/packages/types/src/schemas/common.spec.ts b/packages/types/src/schemas/common.spec.ts index 1d5c7b37..c229ab6a 100644 --- a/packages/types/src/schemas/common.spec.ts +++ b/packages/types/src/schemas/common.spec.ts @@ -350,13 +350,22 @@ describe('webcontainerSchema', () => { }).not.toThrow(); }); - it('should allow specifying the template', () => { + it('should allow specifying the template by name', () => { expect(() => { webcontainerSchema.parse({ template: 'default', }); }).not.toThrow(); }); + it('should allow specifying the template by object type', () => { + expect(() => { + webcontainerSchema.parse({ + template: { + name: 'default', + }, + }); + }).not.toThrow(); + }); it('should allow specifying the terminal', () => { expect(() => { webcontainerSchema.parse({ diff --git a/packages/types/src/schemas/common.ts b/packages/types/src/schemas/common.ts index 3c4bb9e0..82aeda4d 100644 --- a/packages/types/src/schemas/common.ts +++ b/packages/types/src/schemas/common.ts @@ -138,7 +138,15 @@ export type TerminalSchema = z.infer; export const webcontainerSchema = commandsSchema.extend({ previews: previewSchema.optional(), autoReload: z.boolean().optional(), - template: z.string().optional(), + template: z.union([ + // name of the template + z.string().optional(), + + z.strictObject({ + // name of the template + name: z.string(), + }), + ]), terminal: terminalSchema.optional(), focus: z.string().optional(), editor: z.union([