-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
go.js
executable file
·127 lines (113 loc) · 3.27 KB
/
go.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env node
const fs = require('fs')
const {
spawnSync,
getExerciseBranches,
getVariants,
getExtraCreditTitles,
} = require('./scripts/utils')
const actions = {
changeExercise,
startExtraCredit,
}
const currentBranch = spawnSync('git rev-parse --abbrev-ref HEAD')
async function go() {
if (currentBranch === 'main') {
// if we're on main then you can't do anything else
await changeExercise()
return
}
const {action} = await (
await import('inquirer')
).default.prompt([
{
name: 'action',
message: `What do you want to do?`,
type: 'list',
choices: [
{name: 'Change Exercise', value: 'changeExercise'},
{name: 'Start Extra Credit', value: 'startExtraCredit'},
],
},
])
await actions[action]()
}
function getDisplayName(exerciseBranch) {
const match = exerciseBranch.match(
/exercises\/(?<number>\d\d)-(?<title>.*?)$/,
)
const title = match.groups.title.split('-').join(' ')
const capitalizedTitle = title.slice(0, 1).toUpperCase() + title.slice(1)
return `${match.groups.number}. ${capitalizedTitle}`
}
async function changeExercise() {
const {branch} = await (
await import('inquirer')
).default.prompt([
{
name: 'branch',
message: `Which exercise do you want to start working on?`,
type: 'list',
default: currentBranch,
choices: [
{name: 'Return to main', value: 'main'},
...getExerciseBranches().map(b => ({
name: getDisplayName(b),
value: b,
})),
],
},
])
spawnSync('git add -A')
spawnSync('git reset --hard HEAD')
spawnSync(`git checkout ${branch}`)
if (branch.startsWith('exercises/')) {
spawnSync('node ./scripts/swap exercise')
}
console.log(`✅ Ready to start work in ${branch}`)
}
async function startExtraCredit() {
const variants = getVariants()
const maxExtra = Math.max(
...Object.values(variants)
.reduce((acc, v) => [...acc, ...v.extras], [])
.map(e => e.number),
)
const extraCreditTitles = getExtraCreditTitles()
function getVariantDisplayName(variant) {
if (variant === 'final') return 'Final'
return `Extra Credit ${variant}: ${extraCreditTitles[variant - 1]}`
}
const {variant} = await (
await import('inquirer')
).default.prompt([
{
name: 'variant',
message: `Which part do you want to work on?`,
type: 'list',
choices: [
{name: 'Final', value: 'final'},
...Array.from({length: maxExtra}, (v, i) => ({
name: getVariantDisplayName(i + 1),
value: i + 1,
})),
],
},
])
for (const {extras, exercise, final} of Object.values(variants)) {
const availableECs = extras.map(e => e.number).filter(n => n < variant)
const maxEC = Math.max(...availableECs)
const maxExtra = extras.find(e => e.number === maxEC)
if (variant === 'final' || (!maxExtra && !final)) {
// reset the exercise to the original state
spawnSync(`git checkout -- ${exercise.file}`)
} else {
const newExerciseContents = fs.readFileSync((maxExtra || final).file, {
encoding: 'utf-8',
})
fs.writeFileSync(exercise.file, newExerciseContents)
}
}
console.log(`✅ Ready to start working on ${getVariantDisplayName(variant)}`)
}
go()