2023-10-07 05:27:11 +00:00
|
|
|
import { calculateTotalPoints, shuffleOptions } from './utils';
|
2023-10-06 09:06:03 +00:00
|
|
|
import type { ExerciseTemplate } from '$lib/utils/types';
|
2023-10-04 08:18:33 +01:00
|
|
|
|
2024-09-23 01:21:05 +01:00
|
|
|
// Rule to add new template
|
|
|
|
|
// The key and value must match
|
|
|
|
|
// The template should have both `{VALUE}_IDS` and `{VALUE}_TEMPLATES`
|
2023-10-04 08:18:33 +01:00
|
|
|
export const TAGS = {
|
|
|
|
|
HTML: 'HTML',
|
|
|
|
|
CSS: 'CSS',
|
|
|
|
|
JS: 'JS',
|
|
|
|
|
Typescript: 'Typescript',
|
|
|
|
|
ReactJS: 'ReactJS',
|
2023-10-10 06:09:39 +00:00
|
|
|
VueJS: 'VueJS',
|
2023-10-10 06:39:23 +00:00
|
|
|
NodeJS: 'NodeJS',
|
2023-10-20 02:06:58 +00:00
|
|
|
Python: 'Python',
|
2023-10-11 11:47:51 +00:00
|
|
|
PHP: 'PHP',
|
|
|
|
|
GIT: 'GIT'
|
2023-10-04 08:18:33 +01:00
|
|
|
};
|
|
|
|
|
|
2024-09-23 01:21:05 +01:00
|
|
|
export interface GeneratedTemplates {
|
|
|
|
|
[key: string]: LangTemplate[];
|
|
|
|
|
}
|
|
|
|
|
interface LangTemplate {
|
|
|
|
|
id: string;
|
|
|
|
|
title: string;
|
|
|
|
|
questions: number;
|
|
|
|
|
points: number;
|
|
|
|
|
data: ExerciseTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function generateTemplateForTag(
|
2023-10-06 09:06:03 +00:00
|
|
|
keys: { [key: string]: string },
|
2024-09-23 01:21:05 +01:00
|
|
|
template: {
|
|
|
|
|
[x: string]: () => ExerciseTemplate;
|
2023-10-06 09:06:03 +00:00
|
|
|
}
|
|
|
|
|
) {
|
2024-09-23 01:21:05 +01:00
|
|
|
const templates: LangTemplate[] = [];
|
2023-10-06 09:06:03 +00:00
|
|
|
|
|
|
|
|
for (const key in keys) {
|
|
|
|
|
const id = keys[key];
|
2024-09-23 01:21:05 +01:00
|
|
|
const data = await template[id]();
|
|
|
|
|
|
|
|
|
|
data.questionnaire.questions = data.questionnaire.questions.map((q) => {
|
2023-10-07 05:27:11 +00:00
|
|
|
q.options = shuffleOptions(q.options);
|
|
|
|
|
return q;
|
|
|
|
|
});
|
2024-09-23 01:21:05 +01:00
|
|
|
templates.push({
|
2023-10-06 09:06:03 +00:00
|
|
|
id,
|
2024-09-23 01:21:05 +01:00
|
|
|
title: data.title,
|
|
|
|
|
questions: data.questionnaire.questions.length,
|
|
|
|
|
points: calculateTotalPoints(data),
|
|
|
|
|
data: data
|
2023-10-06 09:06:03 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-23 01:21:05 +01:00
|
|
|
return templates;
|
2023-10-06 09:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-23 01:21:05 +01:00
|
|
|
export const getAllTemplates = async (): Promise<GeneratedTemplates> => {
|
|
|
|
|
const templateModules = {
|
|
|
|
|
HTML: await import('./html'),
|
|
|
|
|
CSS: await import('./css'),
|
|
|
|
|
JS: await import('./js'),
|
|
|
|
|
Typescript: await import('./typescript'),
|
|
|
|
|
ReactJS: await import('./react'),
|
|
|
|
|
VueJS: await import('./vue'),
|
|
|
|
|
NodeJS: await import('./node'),
|
|
|
|
|
Python: await import('./python'),
|
|
|
|
|
PHP: await import('./php'),
|
|
|
|
|
GIT: await import('./git')
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const templates: GeneratedTemplates = {};
|
|
|
|
|
|
|
|
|
|
for (const tag in templateModules) {
|
|
|
|
|
try {
|
|
|
|
|
const module = templateModules[tag];
|
|
|
|
|
const ids = module[`${tag.toUpperCase()}_IDS`];
|
|
|
|
|
const templatesData = module[`${tag.toUpperCase()}_TEMPLATES`];
|
|
|
|
|
if (!templatesData) {
|
|
|
|
|
throw 'Templates not found';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
templates[tag] = await generateTemplateForTag(ids, templatesData);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('error finding template', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return templates;
|
2023-10-04 08:18:33 +01:00
|
|
|
};
|