Skip to content

Commit 0ae0184

Browse files
refactor: share common intro creation code (freeCodeCamp#61814)
1 parent 91f87ea commit 0ae0184

File tree

4 files changed

+45
-72
lines changed

4 files changed

+45
-72
lines changed

tools/challenge-helper-scripts/create-language-block.ts

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '../../shared/config/curriculum';
1313
import { createDialogueFile, validateBlockName } from './utils';
1414
import { getBaseMeta } from './helpers/get-base-meta';
15+
import { createIntroMD } from './helpers/create-intro';
1516

1617
const helpCategories = ['English'] as const;
1718

@@ -42,12 +43,12 @@ async function createLanguageBlock(
4243
if (!title) {
4344
title = block;
4445
}
45-
void updateIntroJson(superBlock, block, title);
46+
await updateIntroJson(superBlock, block, title);
4647

4748
const challengeId = await createDialogueChallenge(superBlock, block);
48-
void createMetaJson(superBlock, block, title, helpCategory, challengeId);
49+
await createMetaJson(superBlock, block, title, helpCategory, challengeId);
4950
// TODO: remove once we stop relying on markdown in the client.
50-
void createIntroMD(superBlock, block, title);
51+
await createIntroMD(superBlock, block, title);
5152
}
5253

5354
async function updateIntroJson(
@@ -100,28 +101,6 @@ async function createMetaJson(
100101
);
101102
}
102103

103-
async function createIntroMD(superBlock: string, block: string, title: string) {
104-
const introMD = `---
105-
title: Introduction to the ${title}
106-
block: ${block}
107-
superBlock: ${superBlock}
108-
---
109-
110-
## Introduction to the ${title}
111-
112-
This page is for the ${title}
113-
`;
114-
const dirPath = path.resolve(
115-
__dirname,
116-
`../../client/src/pages/learn/${superBlock}/${block}/`
117-
);
118-
const filePath = path.resolve(dirPath, 'index.md');
119-
if (!existsSync(dirPath)) {
120-
await withTrace(fs.mkdir, dirPath);
121-
}
122-
void withTrace(fs.writeFile, filePath, introMD, { encoding: 'utf8' });
123-
}
124-
125104
async function createDialogueChallenge(
126105
superBlock: SuperBlocks,
127106
block: string

tools/challenge-helper-scripts/create-project.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '../../shared/config/curriculum';
1212
import { createStepFile, validateBlockName } from './utils';
1313
import { getBaseMeta } from './helpers/get-base-meta';
14+
import { createIntroMD } from './helpers/create-intro';
1415

1516
const helpCategories = [
1617
'HTML-CSS',
@@ -117,28 +118,6 @@ async function createMetaJson(
117118
);
118119
}
119120

120-
async function createIntroMD(superBlock: string, block: string, title: string) {
121-
const introMD = `---
122-
title: Introduction to the ${title}
123-
block: ${block}
124-
superBlock: ${superBlock}
125-
---
126-
127-
## Introduction to the ${title}
128-
129-
This is a test for the new project-based curriculum.
130-
`;
131-
const dirPath = path.resolve(
132-
__dirname,
133-
`../../client/src/pages/learn/${superBlock}/${block}/`
134-
);
135-
const filePath = path.resolve(dirPath, 'index.md');
136-
if (!existsSync(dirPath)) {
137-
await withTrace(fs.mkdir, dirPath);
138-
}
139-
void withTrace(fs.writeFile, filePath, introMD, { encoding: 'utf8' });
140-
}
141-
142121
async function createFirstChallenge(
143122
superBlock: SuperBlocks,
144123
block: string

tools/challenge-helper-scripts/create-quiz.ts

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '../../shared/config/curriculum';
1212
import { createQuizFile, validateBlockName } from './utils';
1313
import { getBaseMeta } from './helpers/get-base-meta';
14+
import { createIntroMD } from './helpers/create-intro';
1415

1516
const helpCategories = [
1617
'HTML-CSS',
@@ -48,17 +49,17 @@ async function createQuiz(
4849
if (!title) {
4950
title = block;
5051
}
51-
void updateIntroJson(superBlock, block, title);
52+
await updateIntroJson(superBlock, block, title);
5253

5354
const challengeId = await createQuizChallenge(
5455
superBlock,
5556
block,
5657
title,
5758
questionCount
5859
);
59-
void createMetaJson(superBlock, block, title, helpCategory, challengeId);
60+
await createMetaJson(superBlock, block, title, helpCategory, challengeId);
6061
// TODO: remove once we stop relying on markdown in the client.
61-
void createIntroMD(superBlock, block, title);
62+
await createIntroMD(superBlock, block, title);
6263
}
6364

6465
async function updateIntroJson(
@@ -109,28 +110,6 @@ async function createMetaJson(
109110
);
110111
}
111112

112-
async function createIntroMD(superBlock: string, block: string, title: string) {
113-
const introMD = `---
114-
title: Introduction to the ${title}
115-
block: ${block}
116-
superBlock: ${superBlock}
117-
---
118-
119-
## Introduction to the ${title}
120-
121-
This page is for the ${title}
122-
`;
123-
const dirPath = path.resolve(
124-
__dirname,
125-
`../../client/src/pages/learn/${superBlock}/${block}/`
126-
);
127-
const filePath = path.resolve(dirPath, 'index.md');
128-
if (!existsSync(dirPath)) {
129-
await withTrace(fs.mkdir, dirPath);
130-
}
131-
void withTrace(fs.writeFile, filePath, introMD, { encoding: 'utf8' });
132-
}
133-
134113
async function createQuizChallenge(
135114
superBlock: SuperBlocks,
136115
block: string,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import path from 'node:path';
2+
import fs from 'node:fs/promises';
3+
4+
function introTemplate(
5+
superBlock: string,
6+
block: string,
7+
title: string
8+
): string {
9+
return `---
10+
title: Introduction to the ${title}
11+
block: ${block}
12+
superBlock: ${superBlock}
13+
---
14+
15+
## Introduction to the ${title}
16+
17+
This page is for the ${title}
18+
`;
19+
}
20+
21+
export async function createIntroMD(
22+
superBlock: string,
23+
block: string,
24+
title: string
25+
) {
26+
const dirPath = path.resolve(
27+
__dirname,
28+
`../../../client/src/pages/learn/${superBlock}/${block}/`
29+
);
30+
await fs.mkdir(dirPath, { recursive: true });
31+
32+
const filePath = path.resolve(dirPath, 'index.md');
33+
await fs.writeFile(filePath, introTemplate(superBlock, block, title), {
34+
encoding: 'utf8'
35+
});
36+
}

0 commit comments

Comments
 (0)