forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-challenge.ts
38 lines (31 loc) · 1.12 KB
/
delete-challenge.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { unlink } from 'fs/promises';
import { prompt } from 'inquirer';
import { getProjectPath } from './helpers/get-project-info';
import { getMetaData, updateMetaData } from './helpers/project-metadata';
import { getChallengeOrderFromMeta } from './helpers/get-challenge-order';
import { getFileName } from './helpers/get-file-name';
const deleteChallenge = async () => {
const path = getProjectPath();
const challenges = getChallengeOrderFromMeta();
const challengeToDelete = (await prompt({
name: 'id',
message: 'Which challenge should be deleted?',
type: 'list',
choices: challenges.map(({ id, title }) => ({
name: title,
value: id
}))
})) as { id: string };
const indexToDelete = challenges.findIndex(
({ id }) => id === challengeToDelete.id
);
const fileToDelete = await getFileName(challengeToDelete.id);
if (!fileToDelete) {
throw new Error(`File not found for challenge ${challengeToDelete.id}`);
}
await unlink(`${path}${fileToDelete}`);
const meta = getMetaData();
meta.challengeOrder.splice(indexToDelete, 1);
updateMetaData(meta);
};
void deleteChallenge();