forked from kentcdodds/bookshelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.js
107 lines (92 loc) · 2.95 KB
/
swap.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
const fs = require('fs')
const inquirer = require('inquirer')
const {spawnSync, getVariants, getExtraCreditTitles} = require('./utils')
const branch = spawnSync('git rev-parse --abbrev-ref HEAD')
if (branch === 'main') {
throw new Error('Cannot run swap on main as there are no exercises.')
}
go()
async function go() {
let {2: match} = process.argv
const allowedTypes = [/^exercise$/, /^final$/, /^\d+$/]
const variants = getVariants()
const maxExtra = Math.max(
...Object.values(variants)
.reduce((m, v) => [...m, ...v.extras], [])
.map(e => e.number),
)
if (!Object.keys(variants).length) {
console.log(`There are no variants needing a swap.`)
return
}
const extraCreditTitles = getExtraCreditTitles()
if (!match) {
const prompt = await inquirer.prompt([
{
name: 'matchVal',
message: `Which modules do you want loaded?`,
type: 'list',
choices: [
{name: 'Exercise', value: 'exercise'},
{name: 'Final', value: 'final'},
...Array.from({length: maxExtra}, (v, i) => ({
name: `Extra Credit ${i + 1}: ${extraCreditTitles[i]}`,
value: i + 1,
})),
],
},
])
match = prompt.matchVal
}
if (!allowedTypes.some(t => t.test(match))) {
throw new Error(
`The given match of "${match}" is not one of the allowed types of: ${allowedTypes.join(
', ',
)}`,
)
}
console.log(`Changing used files to those matching "${match}"`)
function getmainFileContents({main, exercise, final, extras}) {
let uncommentedLines
if (match === 'exercise') {
uncommentedLines = exercise.exportLines
} else if (match === 'final') {
uncommentedLines = final ? final.exportLines : exercise.exportLines
} else if (Number.isFinite(Number(match))) {
const availableECs = extras
.map(e => e.number)
.filter(n => n <= Number(match))
const maxEC = Math.max(...availableECs)
const maxExtra = extras.find(e => e.number === maxEC)
uncommentedLines = (maxExtra || final || exercise).exportLines
} else {
console.log('this should not happen...', match)
}
if (!uncommentedLines) {
throw new Error(`No variant found to enable for "${match}" in "${main}"`)
}
const l = lines =>
lines
? lines === uncommentedLines
? lines.join('\n')
: `// ${lines.join('\n// ')}`
: ''
const extrasLines = extras
.map(
({exportLines, title}) =>
`// 💯 ${title}\n${l(exportLines) || '// no extra credit'}`,
)
.join('\n\n')
return (
`
${l((final || {}).exportLines) || '// no final'}
${l(exercise.exportLines) || '// no exercise'}
${extrasLines}
`.trim() + '\n'
)
}
for (const [main, {final, exercise, extras}] of Object.entries(variants)) {
const contents = getmainFileContents({main, final, exercise, extras})
fs.writeFileSync(main, contents)
}
}