Skip to content

Commit e72a5dc

Browse files
Jeevankumar-sojeytonwilliamsSembauke
authored
refactor(tools) : migrate inquirer prompts (freeCodeCamp#66139)
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Sem Bauke <sem@freecodecamp.org>
1 parent 80cace6 commit e72a5dc

15 files changed

Lines changed: 1035 additions & 887 deletions

pnpm-lock.yaml

Lines changed: 495 additions & 375 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 284 additions & 215 deletions
Large diffs are not rendered by default.

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

Lines changed: 120 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs/promises';
22
import path from 'path';
3-
import { prompt } from 'inquirer';
3+
import { select, input, number } from '@inquirer/prompts';
44
import { format } from 'prettier';
55
import { ObjectId } from 'bson';
66

@@ -267,149 +267,136 @@ async function getModules(superBlock: string, chapterName: string) {
267267
}
268268

269269
void getAllBlocks()
270-
.then(existingBlocks =>
271-
prompt([
272-
{
273-
name: 'superBlock',
274-
message: 'Which certification does this belong to?',
275-
default: SuperBlocks.RespWebDesignV9,
276-
type: 'list',
277-
choices: Object.values(SuperBlocks)
278-
},
279-
{
280-
name: 'block',
281-
message: 'What is the dashed name (in kebab-case) for this project?',
282-
validate: (block: string) => validateBlockName(block, existingBlocks),
283-
filter: (block: string) => {
284-
return block.toLowerCase().trim();
285-
}
286-
},
287-
{
288-
name: 'title',
289-
default: ({ block }: { block: string }) => block
290-
},
291-
{
292-
name: 'helpCategory',
293-
message: 'Choose a help category',
294-
default: 'HTML-CSS',
295-
type: 'list',
296-
choices: helpCategories
297-
},
298-
{
299-
name: 'blockLabel',
270+
.then(async existingBlocks => {
271+
const superBlock = await select<SuperBlocks>({
272+
message: 'Which certification does this belong to?',
273+
default: SuperBlocks.RespWebDesignV9,
274+
choices: Object.values(SuperBlocks).map(value => ({
275+
name: value,
276+
value
277+
}))
278+
});
279+
280+
const rawBlock = await input({
281+
message: 'What is the dashed name (in kebab-case) for this project?',
282+
validate: (value: string) => validateBlockName(value, existingBlocks)
283+
});
284+
285+
const block = rawBlock.toLowerCase().trim();
286+
287+
const title = await input({
288+
message: 'Enter a title for this project:',
289+
default: block
290+
});
291+
292+
const helpCategory = await select<string>({
293+
message: 'Choose a help category',
294+
default: 'HTML-CSS',
295+
choices: helpCategories.map(value => ({
296+
name: value,
297+
value
298+
}))
299+
});
300+
301+
let blockLabel: BlockLabel | undefined;
302+
let blockLayout: BlockLayouts | undefined;
303+
let questionCount: number | undefined;
304+
let chapter: string | undefined;
305+
let module: string | undefined;
306+
let position: number | undefined;
307+
let order: number | undefined;
308+
309+
if (chapterBasedSuperBlocks.includes(superBlock)) {
310+
blockLabel = await select<BlockLabel>({
300311
message: 'Choose a block label',
301312
default: BlockLabel.lab,
302-
type: 'list',
303-
choices: Object.values(BlockLabel),
304-
when: (answers: CreateProjectArgs) =>
305-
chapterBasedSuperBlocks.includes(answers.superBlock)
306-
},
307-
{
308-
name: 'blockLayout',
309-
message: 'Choose a block layout',
313+
choices: Object.values(BlockLabel).map(value => ({
314+
name: value,
315+
value
316+
}))
317+
});
310318

311-
default: (answers: { blockLabel: BlockLabel }) =>
312-
answers.blockLabel == BlockLabel.quiz
319+
blockLayout = await select<BlockLayouts>({
320+
message: 'Choose a block layout',
321+
default:
322+
blockLabel === BlockLabel.quiz
313323
? BlockLayouts.Link
314324
: BlockLayouts.ChallengeList,
315-
type: 'list',
316-
choices: Object.values(BlockLayouts),
317-
when: (answers: CreateProjectArgs) =>
318-
chapterBasedSuperBlocks.includes(answers.superBlock)
319-
},
320-
{
321-
name: 'questionCount',
322-
message: 'Choose a question count',
323-
default: 20,
324-
type: 'list',
325-
choices: [10, 20],
326-
when: (answers: CreateProjectArgs) =>
327-
answers.blockLabel === BlockLabel.quiz
328-
},
329-
{
330-
name: 'chapter',
325+
choices: Object.values(BlockLayouts).map(value => ({
326+
name: value,
327+
value
328+
}))
329+
});
330+
331+
if (blockLabel === BlockLabel.quiz) {
332+
questionCount = await select<number>({
333+
message: 'Choose a question count',
334+
default: 20,
335+
choices: [
336+
{ name: '10', value: 10 },
337+
{ name: '20', value: 20 }
338+
]
339+
});
340+
}
341+
342+
const chapters = await getChapters(superBlock);
343+
chapter = await select({
331344
message: 'What chapter should this project go in?',
332-
default: 'html',
333-
type: 'list',
334-
choices: async (answers: CreateProjectArgs) => {
335-
const chapters = await getChapters(answers.superBlock);
336-
return chapters.map(x => x.dashedName);
337-
},
338-
when: (answers: CreateProjectArgs) =>
339-
chapterBasedSuperBlocks.includes(answers.superBlock)
340-
},
341-
{
342-
name: 'module',
345+
choices: chapters.map(x => ({
346+
name: x.dashedName,
347+
value: x.dashedName
348+
}))
349+
});
350+
351+
const modules = await getModules(superBlock, chapter);
352+
module = await select({
343353
message: 'What module should this project go in?',
344-
default: 'html',
345-
type: 'list',
346-
choices: async (answers: CreateProjectArgs) => {
347-
const modules = await getModules(
348-
answers.superBlock,
349-
answers.chapter!
350-
);
351-
return modules!.map(x => x.dashedName);
352-
},
353-
when: (answers: CreateProjectArgs) =>
354-
chapterBasedSuperBlocks.includes(answers.superBlock)
355-
},
356-
{
357-
name: 'position',
354+
choices: modules!.map(x => ({
355+
name: x.dashedName,
356+
value: x.dashedName
357+
}))
358+
});
359+
360+
position = await number({
358361
message: 'At which position does this appear in the module?',
359362
default: 1,
360-
validate: (position: string) => {
361-
return parseInt(position, 10) > 0
363+
validate: (value: number | undefined) =>
364+
value && value > 0
362365
? true
363-
: 'Position must be an number greater than zero.';
364-
},
365-
when: (answers: CreateProjectArgs) =>
366-
chapterBasedSuperBlocks.includes(answers.superBlock),
367-
filter: (position: string) => {
368-
return parseInt(position, 10);
369-
}
370-
},
371-
{
372-
name: 'order',
366+
: 'Position must be a number greater than zero.'
367+
});
368+
} else {
369+
order = await number({
373370
message: 'Which position does this appear in the certificate?',
374371
default: 42,
375-
validate: (order: string) => {
376-
return parseInt(order, 10) > 0
372+
validate: (value: number | undefined) =>
373+
value && value > 0
377374
? true
378-
: 'Order must be an number greater than zero.';
379-
},
380-
when: (answers: CreateProjectArgs) =>
381-
!chapterBasedSuperBlocks.includes(answers.superBlock),
382-
filter: (order: string) => {
383-
return parseInt(order, 10);
384-
}
385-
}
386-
]).then(
387-
async ({
388-
superBlock,
389-
block,
390-
title,
391-
helpCategory,
392-
blockLabel,
393-
blockLayout,
394-
questionCount,
395-
chapter,
396-
module,
397-
position,
398-
order
399-
}: CreateProjectArgs) =>
400-
await createProject({
401-
superBlock,
402-
block,
403-
helpCategory,
404-
blockLabel,
405-
blockLayout,
406-
questionCount,
407-
title,
408-
chapter,
409-
module,
410-
position,
411-
order
412-
})
375+
: 'Order must be a number greater than zero.'
376+
});
377+
}
378+
379+
return {
380+
superBlock,
381+
block,
382+
title,
383+
helpCategory,
384+
blockLabel,
385+
blockLayout,
386+
questionCount,
387+
chapter,
388+
module,
389+
position,
390+
order
391+
};
392+
})
393+
.then(async (answers: CreateProjectArgs) => {
394+
await createProject(answers);
395+
})
396+
.then(() => console.log('All set. Refresh the page to see the changes.'))
397+
.catch((err: unknown) =>
398+
console.error(
399+
'Error creating project:',
400+
err instanceof Error ? err.message : String(err)
413401
)
414-
)
415-
.then(() => console.log('All set. Refresh the page to see the changes.'));
402+
);

0 commit comments

Comments
 (0)