forked from Argeento/advent-of-code-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-day.civet
65 lines (48 loc) · 1.63 KB
/
create-day.civet
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
fs from fs
path from path
process from process
axios from axios
dotenv from dotenv
dotenv.config()
unless process.env.SESSION
console.error 'Error: SESSION environment variable not set'
process.exit 1
unless process.env.YEAR
console.error 'Error: YEAR environment variable not set'
process.exit 1
unless process.argv.2
process.argv.2 = new Date().getDate().toString()
dirName := process.argv.2.padStart 2, '0'
dirPath := path.join process.cwd(), 'src', dirName
unless fs.existsSync dirPath
fs.mkdirSync dirPath, recursive: true
day := parseInt dirName
year := parseInt process.env.YEAR
{ data } := await axios.get `https://adventofcode.com/${year}/day/${day}`
titleMatch := data.match /<h2>[-]{3}(.*)[-]{3}<\/h2>/
unless titleMatch?.1
console.error 'Error: Could not fetch title'
process.exit 1
if !fs.existsSync path.join(dirPath, 'story.md')
fs.writeFileSync
path.join(dirPath, 'story.md'),
`## ${titleMatch[1].trim()} ⭐⭐\n`
console.log `✅ ${dirName}/story.md`
if !fs.existsSync path.join(dirPath, 'index.civet')
fs.writeFileSync
path.join(dirPath, 'index.civet'),
`// Path: src/${dirName}/index.civet\n\n`
console.log `✅ ${dirName}/index.civet`
if !fs.existsSync path.join(dirPath, 'input.txt')
{ data } := await axios.get `https://adventofcode.com/${year}/day/${day}/input`,
headers:
Cookie: `session=${process.env.SESSION}`
fs.writeFileSync
path.join(dirPath, 'input.txt'),
data
console.log `✅ ${dirName}/input.txt`
console.log `
\x1b[32m🎄🎅 Advent of Code 2024 🎅🎄\x1b[0m
\x1b[33m --- ${titleMatch[1].trim()} --- \x1b[0m
\x1b[1mHappy coding and good luck!\x1b[0m
`