forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-challenge-order.ts
55 lines (46 loc) · 1.64 KB
/
update-challenge-order.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { prompt } from 'inquirer';
import { getMetaData, updateMetaData } from './helpers/project-metadata';
import { getChallengeOrderFromMeta } from './helpers/get-challenge-order';
const updateChallengeOrder = async () => {
const oldChallengeOrder = getChallengeOrderFromMeta();
console.log('Current challenge order is: ');
console.table(oldChallengeOrder.map(({ title }) => ({ title })));
const newChallengeOrder: { id: string; title: string }[] = [];
while (oldChallengeOrder.length) {
const nextChallenge = (await prompt({
name: 'id',
message: newChallengeOrder.length
? `What challenge comes after ${
newChallengeOrder[newChallengeOrder.length - 1].title
}?`
: 'What is the first challenge?',
type: 'list',
choices: oldChallengeOrder.map(({ id, title }) => ({
name: title,
value: id
}))
})) as { id: string };
const nextChallengeIndex = oldChallengeOrder.findIndex(
({ id }) => id === nextChallenge.id
);
const targetChallenge = oldChallengeOrder[nextChallengeIndex];
oldChallengeOrder.splice(nextChallengeIndex, 1);
newChallengeOrder.push(targetChallenge);
}
console.log('New challenge order is: ');
console.table(newChallengeOrder.map(({ title }) => ({ title })));
const confirm = await prompt({
name: 'correct',
message: 'Is this correct?',
type: 'confirm',
default: false
});
if (!confirm.correct) {
console.error('Aborting.');
return;
}
const meta = getMetaData();
meta.challengeOrder = newChallengeOrder;
updateMetaData(meta);
};
void (async () => await updateChallengeOrder())();