-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-dir.js
71 lines (61 loc) · 1.85 KB
/
create-dir.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
const axios = require("axios");
const fs = require("fs");
require("dotenv").config();
// creates empty directory for the day and adds input
const sessionCookie = process.env.SESSION_COOKIE;
const dayNumber = process.argv[2];
const [folderName, inputUrl, path] = [
`src/day_${dayNumber}`,
`https://adventofcode.com/2023/day/${dayNumber}/input`,
`/Users/roshaanbajwa/aoc23/src/day_${dayNumber}/`
];
const [inputFileName, testInputFileName, indexFileName] = [
`${path}input.txt`,
`${path}input.test.txt`,
`${path}index.js`
];
if (!sessionCookie) {
console.error("Set the SESSION_COOKIE variable in the .env file.");
process.exit(1);
}
if (!dayNumber || isNaN(parseInt(dayNumber))) {
console.error("Provide day number as a command-line argument.");
process.exit(1);
}
try {
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName);
axios
.get(inputUrl, {
headers: {
Cookie: `session=${sessionCookie}`
}
})
.then((response) => {
const input = response.data.trim();
const indexTemplate = fs.readFileSync("./index.template.js", "utf8");
fs.writeFileSync(inputFileName, input);
console.log(
`Puzzle input for Day ${dayNumber} has been saved to ${inputFileName}`
);
fs.writeFileSync(indexFileName, indexTemplate);
console.log(
`Template index.js file has been created at ${indexFileName}`
);
fs.writeFileSync(testInputFileName, "");
console.log(
`Empty test input.txt file has been created at ${testInputFileName}`
);
})
.catch((error) => {
console.error(
"Error fetching puzzle input:",
error.response ? error.response.data : error.message
);
});
} else {
console.log("Error creating folder");
}
} catch (err) {
console.log(err);
}