Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

chore: modify scripts to recurse aot directories

bautistaaa38ce67bccb6a
diff --git a/packages/db/package.json b/packages/db/package.json
index eca570dc..41c1711c 100644
--- a/packages/db/package.json
+++ b/packages/db/package.json
@@ -23,7 +23,7 @@
"db:push": "dotenv -e ../../.env prisma db push",
"db:reset": "dotenv -e ../../.env prisma db push -- --force-reset",
"db:seed": "dotenv -e ../../.env tsx ./seed/dev.ts",
- "db:prod:seed": "dotenv -e ../../.env tsx ./temp/populate-aot-2023.ts && tsx ./seed/prod.ts ",
+ "db:prod:seed": "dotenv -e ../../.env tsx ./temp/populate-aot-challenges.ts && tsx ./seed/prod.ts",
"db:studio": "dotenv -e ../../.env prisma studio",
"format": "prettier . --check --cache --cache-location=\"node_modules/.cache/prettiercache\"",
"format:fix": "prettier . --write --cache --cache-location=\"node_modules/.cache/prettiercache\" --log-level=warn",
diff --git a/packages/db/seed/data/challenge-ingest.ts b/packages/db/seed/data/challenge-ingest.ts
index 2f90b26e..e0d1b76f 100644
--- a/packages/db/seed/data/challenge-ingest.ts
+++ b/packages/db/seed/data/challenge-ingest.ts
@@ -3,23 +3,39 @@ import fs from 'node:fs';
import path from 'node:path';
const defaultExcludes = ['blank', 'solutions', 'aot'];
-export async function ingestChallenges(challengePath: string, excludes = defaultExcludes) {
+
+export async function ingestChallenges(
+ challengePath: string,
+ excludes = defaultExcludes,
+): Promise<(Prisma.ChallengeCreateManyInput & { author: string })[]> {
const challengesToCreate: (Prisma.ChallengeCreateManyInput & { author: string })[] = [];
+
try {
const items = await fs.promises.readdir(challengePath);
for (const item of items) {
const itemPath = path.join(challengePath, item);
+ // Skip items that are excluded.
if (excludes.some((x) => itemPath.includes(x))) {
+ console.log('Skipping:', itemPath);
continue;
}
const stats = await fs.promises.stat(itemPath);
if (stats.isDirectory()) {
- const challengeToCreate = await buildChallenge(itemPath, excludes);
- challengesToCreate.push(challengeToCreate);
+ // Recursively ingest challenges from subdirectories.
+ const nestedChallenges = await ingestChallenges(itemPath, excludes);
+ challengesToCreate.push(...nestedChallenges);
+ } else if (stats.isFile()) {
+ // Process individual files as part of a challenge.
+ const challengeToCreate = await buildChallenge(challengePath, excludes);
+ // only valid challenges will have a slug
+ if (challengeToCreate?.slug) {
+ challengesToCreate.push(challengeToCreate);
+ }
+ break; // Exit after processing files for a challenge.
}
}
} catch (error) {
@@ -29,96 +45,66 @@ export async function ingestChallenges(challengePath: string, excludes = default
return challengesToCreate;
}
-async function buildChallenge(pathToDirectory: string, excludes: string[]) {
+async function buildChallenge(
+ pathToDirectory: string,
+ excludes: string[],
+): Promise<(Prisma.ChallengeCreateManyInput & { author: string }) | null> {
const challengeToCreate: Prisma.ChallengeCreateManyInput & { author: string } = {
status: 'ACTIVE',
} as Prisma.ChallengeCreateManyInput & { author: string };
- const files = await fs.promises.readdir(pathToDirectory);
+ try {
+ const files = await fs.promises.readdir(pathToDirectory);
+ console.log(files);
- for (const file of files) {
- const itemPath = path.join(pathToDirectory, file);
+ for (const file of files) {
+ const itemPath = path.join(pathToDirectory, file);
+ console.log('buildChallenge Processing:', itemPath);
- if (excludes.some((x) => itemPath.includes(x))) {
- continue;
- }
+ // Skip excluded files.
+ if (excludes.some((x) => itemPath.includes(x))) {
+ console.log('Skipping:', itemPath);
+ continue;
+ }
- const stats = await fs.promises.stat(itemPath);
+ const stats = await fs.promises.stat(itemPath);
- if (stats.isFile()) {
- const fileName = path.parse(itemPath).name;
+ if (stats.isFile()) {
+ const fileName = path.parse(itemPath).name;
- if (fileName === 'prompt') {
- try {
- const fileContents = await fs.promises.readFile(itemPath, 'utf8');
- challengeToCreate.description = fileContents;
- } catch (jsonError) {
- console.error('Error parsing JSON:', jsonError);
- }
- }
- if (fileName === 'user') {
- try {
- const fileContents = await fs.promises.readFile(itemPath, 'utf8');
- challengeToCreate.code = fileContents;
- } catch (jsonError) {
- console.error('Error parsing JSON:', jsonError);
- }
- }
- if (fileName === 'tests') {
try {
const fileContents = await fs.promises.readFile(itemPath, 'utf8');
- challengeToCreate.tests = fileContents;
- } catch (jsonError) {
- console.error('Error parsing JSON:', jsonError);
- }
- }
- if (fileName === 'metadata') {
- try {
- const fileContents = await fs.promises.readFile(itemPath, 'utf8');
- const jsonData = JSON.parse(fileContents);
- challengeToCreate.difficulty = jsonData.difficulty.toUpperCase();
- challengeToCreate.name = jsonData.label;
- challengeToCreate.slug = jsonData.id;
- challengeToCreate.shortDescription = jsonData.description;
- challengeToCreate.author = jsonData.author;
- } catch (jsonError) {
- console.error('Error parsing JSON:', jsonError);
- }
- }
- if (fileName === 'tsconfig') {
- try {
- const fileContents = await fs.promises.readFile(itemPath, 'utf8');
- const jsonData = JSON.parse(fileContents);
- if (jsonData.compilerOptions != null) {
- challengeToCreate.tsconfig = jsonData.compilerOptions;
+ if (fileName === 'prompt') {
+ challengeToCreate.description = fileContents;
+ }
+ if (fileName === 'user') {
+ challengeToCreate.code = fileContents;
+ }
+ if (fileName === 'tests') {
+ challengeToCreate.tests = fileContents;
+ }
+ if (fileName === 'metadata') {
+ const jsonData = JSON.parse(fileContents);
+ challengeToCreate.difficulty = jsonData.difficulty.toUpperCase();
+ challengeToCreate.name = jsonData.label;
+ challengeToCreate.slug = jsonData.id;
+ challengeToCreate.shortDescription = jsonData.description;
+ challengeToCreate.author = jsonData.author;
+ }
+ if (fileName === 'tsconfig') {
+ const jsonData = JSON.parse(fileContents);
+ if (jsonData.compilerOptions != null) {
+ challengeToCreate.tsconfig = jsonData.compilerOptions;
+ }
}
} catch (jsonError) {
- console.error('Error parsing JSON:', jsonError);
+ console.error(`Error reading or parsing ${fileName}:`, jsonError);
}
}
- // @TODO: we'll ingest solutions later
- // const isWithinSolutionsDirectory = challengePath.split('/').at(-1) === 'solutions';
- //
- // if (isWithinSolutionsDirectory) {
- // if (fileExtension === '.ts') {
- // try {
- // const fileContents = await fs.promises.readFile(itemPath, 'utf8');
- // // console.log('Parsed ts:', fileContents);
- // } catch (jsonError) {
- // console.error('Error parsing JSON:', jsonError);
- // }
- // }
- // } else {
- // // console.log(`File: ${itemPath} (Extension: ${fileExtension})`);
- // }
}
+ return challengeToCreate;
+ } catch (error) {
+ console.error('Error reading directory:', error);
+ return null;
}
-
- return challengeToCreate;
-}
-
-function hyphenatedToPascalCase(input: string): string {
- const words = input.split('-');
- const pascalWords = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1));
- return pascalWords.join(' ');
}
diff --git a/packages/db/seed/prod.ts b/packages/db/seed/prod.ts
index 7a55f4e8..e376076d 100644
--- a/packages/db/seed/prod.ts
+++ b/packages/db/seed/prod.ts
@@ -73,7 +73,7 @@ try {
},
});
- if (track.name === 'Advent of TypeScript 2023' || track.name === 'Advent of TypeScript 2024') {
+ if (track.name === 'Advent of TypeScript 2023') {
const challengesForTrack = await prisma.challenge.findMany({
where: {
slug: {
@@ -116,6 +116,49 @@ try {
});
}
+ if (track.name === 'Advent of TypeScript 2024') {
+ const challengesForTrack = await prisma.challenge.findMany({
+ where: {
+ slug: {
+ in: [
+ '2024-1',
+ '2024-2',
+ '2024-3',
+ '2024-4',
+ '2024-5',
+ '2024-6',
+ '2024-7',
+ '2024-8',
+ '2024-9',
+ '2024-10',
+ '2024-11',
+ '2024-12',
+ '2024-13',
+ '2024-14',
+ '2024-15',
+ '2024-16',
+ '2024-17',
+ '2024-18',
+ '2024-19',
+ '2024-20',
+ '2024-21',
+ '2024-22',
+ '2024-23',
+ '2024-24',
+ '2024-25',
+ ],
+ },
+ },
+ });
+ await prisma.trackChallenge.createMany({
+ data: challengesForTrack.map((challenge, index) => ({
+ challengeId: challenge.id,
+ trackId: createdTrack.id,
+ orderId: index,
+ })),
+ });
+ }
+
if (track.name === 'TypeScript Foundations') {
const challengesForTrack = await prisma.challenge.findMany({
where: {
diff --git a/packages/db/temp/populate-aot-2023.ts b/packages/db/temp/populate-aot-challenges.ts
similarity index 96%
rename from packages/db/temp/populate-aot-2023.ts
rename to packages/db/temp/populate-aot-challenges.ts
index 9262b2ee..c6506fb2 100644
--- a/packages/db/temp/populate-aot-2023.ts
+++ b/packages/db/temp/populate-aot-challenges.ts
@@ -7,9 +7,9 @@ import { prisma } from '../src';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
-const challengePath = path.join(__dirname, '../../../challenges/aot/2023');
+const challengePath = path.join(__dirname, '../../../challenges/aot');
-const excludes = ['blank', 'solutions'];
+const excludes = ['blank', 'solutions', 'metadata.schema.json'];
export const slugify = (str: string) => str.toLowerCase().replace(/\s/g, '-');
const TYPEHERO_ID = uuidByString('typehero');