-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
66 lines (58 loc) · 1.92 KB
/
index.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
require("dotenv").config();
const { spawn } = require("child_process");
import { mkdirSync, existsSync, writeFileSync, readdirSync } from "fs";
import { downloadInputForYearAndDay, getPuzzleDescription } from "./utils/aoc-actions";
import { cp } from "shelljs";
const languageMappings = {
rust: "rs",
rs: "rs",
typescript: "ts",
ts: "ts",
} as const;
type Language = keyof typeof languageMappings;
const action = process.argv[2];
const year = process.argv[3];
const day = process.argv[4];
const lang = process.argv[5];
const createFromTemplate = async () => {
let path = `./challenges/${year}/${day}`;
if (!existsSync(path)) {
console.log(`Creating challenge to ${path} from template...`);
mkdirSync(`challenges/${year}/${day}`, { recursive: true });
//Copy template
cp("-rf", `template/${lang ? languageMappings[lang as keyof typeof languageMappings] : "ts"}/*`, path);
}
if (!existsSync(`${path}/input.txt`)) {
console.log(`Downloading input...`);
let input = await downloadInputForYearAndDay(day, year);
writeFileSync(`${path}/input.txt`, input as string);
}
let readme = await getPuzzleDescription(year, day);
writeFileSync(`${path}/README.md`, readme as string);
};
if (action === "create") {
createFromTemplate();
}
if (action === "run") {
const folder = `challenges/${year}/${day}/`;
const filesInFolder = readdirSync(folder);
const extension = filesInFolder.find((e) => e.includes("index"))?.split(".")[1] as Language;
const file = `index.${extension}`;
if (existsSync(folder + file)) {
switch (extension) {
case "rs":
case "rust":
spawn("cargo", ["run", folder + file], {
stdio: "inherit",
shell: true,
cwd: folder,
});
break;
default:
spawn("nodemon", ["-x", "ts-node", `challenges/${year}/${day}/index.ts ${year} ${day}`], {
stdio: "inherit",
shell: true,
});
}
}
}