-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.mjs
48 lines (39 loc) · 1.32 KB
/
main.mjs
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
import fs from 'fs';
import path from 'path';
import readline from 'readline';
import { exec } from 'child_process';
const days = fs
.readdirSync('.')
.filter(dir => dir.match(/\d+/))
.map((day) => {
const parts = fs
.readdirSync(`./${day}`)
.map((file) => {
const matches = file.match(/part([\d-]+)\.mjs$/);
return matches ? matches[1] : null;
})
.filter(part => part)
.map(part => parseInt(part, 10));
return { day: parseInt(day, 10), parts };
});
const maxDay = days.reduce((p, d) => (d.day > p ? d.day : p), 0);
const rli = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rli.question(`Choose a Day (1-${maxDay}): `, (dayChoice) => {
const day = days.filter(d => d.day === parseInt(dayChoice, 10))[0];
rli.question(`Choose a Part (${day.parts.join(', ')}): `, (partChoice) => {
const dayDir = dayChoice.padStart(2, '0');
const cwd = `${path.resolve()}/${dayDir}`;
const command = `node day${dayDir}-part${partChoice}.mjs`;
exec(command, { cwd }, (err, stdout) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
rli.close();
});
});