Skip to content

Commit f498eb2

Browse files
committed
fix: code review
1 parent 57adec8 commit f498eb2

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

packages/astro/src/default/utils/content.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ChapterSchema, Lesson, LessonSchema, PartSchema, Tutorial, TutorialSchema } from '@tutorialkit/types';
22
import { interpolateString } from '@tutorialkit/types';
33
import { getCollection } from 'astro:content';
4-
import mm from 'micromatch';
4+
import micromatch from 'micromatch';
55
import path from 'node:path';
66
import { DEFAULT_LOCALIZATION } from './content/default-localization';
77
import { squash } from './content/squash.js';
@@ -257,14 +257,14 @@ export async function getTutorial(): Promise<Tutorial> {
257257
};
258258

259259
if (lesson.data.template && typeof lesson.data.template !== 'string' && lesson.data.template.visibleFiles?.length) {
260-
const templateFilesRef = await getFilesRefList(lesson.data.template.name, TEMPLATES_DIR);
260+
const [, tempalteFiles] = await getFilesRefList(lesson.data.template.name, TEMPLATES_DIR);
261261

262-
for (const filename of templateFilesRef[1]) {
262+
for (const filename of tempalteFiles) {
263263
if (lesson.files[1].includes(filename)) {
264264
continue;
265265
}
266266

267-
if (mm.isMatch(filename, lesson.data.template.visibleFiles, { format: formatTemplateFile })) {
267+
if (micromatch.isMatch(filename, lesson.data.template.visibleFiles, { format: formatTemplateFile })) {
268268
lesson.files[1].push(filename);
269269
}
270270
}

packages/runtime/src/store/index.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export class TutorialStore {
176176
this._lessonFiles = files;
177177
this._lessonSolution = solution;
178178
this._lessonTemplate = template;
179-
this._visibleTemplateFiles = filterEntries(template, lesson.files[1]);
179+
this._visibleTemplateFiles = pick(template, lesson.files[1]);
180180

181181
const editorFiles = { ...this._visibleTemplateFiles, ...this._lessonFiles };
182182
this._editorStore.setDocuments(editorFiles);
@@ -374,6 +374,14 @@ export class TutorialStore {
374374
}
375375
}
376376

377-
function filterEntries<T extends object>(obj: T, filter: string[]) {
378-
return Object.fromEntries(Object.entries(obj).filter(([entry]) => filter.includes(entry)));
377+
function pick<T>(obj: Record<string, T>, entries: string[]) {
378+
const result: Record<string, T> = {};
379+
380+
for (const entry of entries) {
381+
if (entry in obj) {
382+
result[entry] = obj[entry];
383+
}
384+
}
385+
386+
return result;
379387
}

packages/types/src/schemas/common.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,17 @@ export const webcontainerSchema = commandsSchema.extend({
177177
template: z
178178
.union([
179179
// name of the template
180-
z.string().optional(),
180+
z.string(),
181181

182182
z.strictObject({
183183
// name of the template
184184
name: z.string(),
185185

186186
// list of globs of files that should be visible
187-
visibleFiles: z.array(z.string()).optional(),
187+
visibleFiles: z.array(z.string()).optional().describe('Specifies which files from template should be visible'),
188188
}),
189189
])
190+
.optional()
190191
.describe(
191192
'Specifies which folder from the `src/templates/` directory should be used as the basis for the code. See the "Code templates" guide for a detailed explainer.',
192193
),

0 commit comments

Comments
 (0)