generated from MJGTwo/advent-of-code-nodets-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-template.ts
101 lines (90 loc) · 3.34 KB
/
copy-template.ts
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
import path from "path";
import { make, find, read, write, run, position } from "promise-path";
import { reportGenerator } from "./util.ts";
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const report = reportGenerator(__filename);
const fromHere = position(path.dirname(__filename));
async function fetchAOCDInput(currentYear: number, currentDay: number) {
report(
"Using AOCD to attempt to download your puzzle input, see: https://github.com/wimglenn/advent-of-code-data"
);
try {
const { stdout, stderr } = await run(`aocd ${currentDay} ${currentYear}`);
if (stderr) {
report(`AOCD ${currentYear} / ${currentDay}`, stderr);
}
if (stdout) {
report(`Downloaded ${stderr.bytes} bytes of data using AOCD.`);
}
return stdout;
} catch (ex: any) {
report(`Could not fetch input for ${currentYear} / ${currentDay}`, ex);
}
return "PASTE YOUR INPUT HERE";
}
export async function copyTemplate() {
const newFolderName = process.argv[2];
const templateFolderPath = "solutions/template";
const targetFolderPath: string = fromHere(`solutions/${newFolderName}`);
if (!newFolderName) {
return await report(
"No path specified to copy to.",
"Please specify a folder name as an argument to this script.",
"e.g. node copy-template day5"
);
}
const existingFiles = await find(`${targetFolderPath}/*`);
if (existingFiles.length > 0) {
report("Existing files found:");
console.log(existingFiles.map((n) => " " + n).join("\n"));
return report("Path", newFolderName, "already exists, doing nothing.");
}
report(
"Creating:",
`solutions/${newFolderName}`,
"from template",
templateFolderPath
);
const templateFiles = await find(fromHere(`${templateFolderPath}/*`));
await make(fromHere(`solutions/${newFolderName}`));
await Promise.all(
templateFiles.map(async (filepath: string) => {
const contents = await read(filepath);
const filename = path.parse(filepath).base;
const newFilePath = `solutions/${newFolderName}/${filename}`;
report("Creating:", newFilePath);
return write(fromHere(newFilePath), contents);
})
);
report("Attemping to download puzzle input for this date");
const currentPath = fromHere("/");
const currentFolder = (await currentPath).split("/").reverse()[1];
const currentYearString: string | undefined = currentFolder.split("-").pop();
const currentDay: number = Number.parseInt(newFolderName.replace("day", ""));
if (!currentYearString || !currentDay) {
report(`Invalid year (${currentYearString}) / day (${currentDay})`);
process.exit(1);
}
const currentYear: number = +currentYearString;
report(
`Based on the path, ${currentFolder} I think its: ${currentYearString}, and you're trying to solve: Day ${currentDay}`
);
if (+currentYear > 0 && currentDay > 0) {
report(`Potentially valid year (${currentYear}) / day (${currentDay})`);
try {
const aocInputText = await fetchAOCDInput(+currentYear, currentDay);
console.log("newfoldername", newFolderName);
await write(
fromHere(`solutions/${newFolderName}/input.txt`),
aocInputText,
"utf8"
);
} catch (ex: any) {
console.error(ex);
}
} else {
report(`Invalid year (${currentYear}) / day (${currentDay})`);
}
report("Done.");
}