-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaffold.puzzle.ts
112 lines (90 loc) · 2.69 KB
/
scaffold.puzzle.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
102
103
104
105
106
107
108
109
110
111
112
import { parseArgs } from "@std/cli";
import { join } from "@std/path";
const scaffoldArgs = parseArgs(Deno.args);
const puzzleNumber = await parsePuzzleNumber(scaffoldArgs._);
const puzzleDirname = puzzleNumber.toString().padStart(2, "0");
console.log(
"Scaffolding puzzle",
puzzleNumber,
"into dir",
`./${puzzleDirname}`,
);
await Deno.mkdir(puzzleDirname);
await createDenoJson();
await createEntrypoint();
await createTests();
await createInput();
await createReadme();
await updateWorkspace();
async function parsePuzzleNumber(args: (string | number)[]): Promise<number> {
if (args.length > 0 && typeof args[0] === "number") {
return args[0];
}
return await getNextPuzzleNumber();
}
async function getNextPuzzleNumber() {
const dirs = Deno.readDir(".");
let lastPuzzle = 0;
for await (const dir of dirs) {
const potentialPuzzle = Number.parseInt(dir.name, 10);
if (isNaN(potentialPuzzle) || potentialPuzzle < lastPuzzle) {
continue;
}
lastPuzzle = potentialPuzzle;
}
return lastPuzzle + 1;
}
async function createDenoJson() {
const denoJson = {
name: `@puzzle/${puzzleDirname}`,
exports: "./main.ts",
tasks: {
dev: "deno run --watch main.ts",
},
};
await Deno.writeTextFile(
join(puzzleDirname, "deno.json"),
JSON.stringify(denoJson, null, 2),
);
}
async function createEntrypoint() {
const content = `
import { getPuzzleInput } from "./input.ts";
if (import.meta.main) {
const puzzleInput = await getPuzzleInput();
}`;
await Deno.writeTextFile(join(puzzleDirname, "main.ts"), content);
}
async function createTests() {
const content = `
import { expect } from "@std/expect";
import { getPuzzleInput } from "./input.ts";
Deno.test("can read puzzle input", async () => {
const input = await getPuzzleInput();
expect(input).toBeDefined();
})`;
await Deno.writeTextFile(join(puzzleDirname, "main.test.ts"), content);
}
async function createInput() {
const content = `
import { getPuzzleInputFromFileUrl } from "@puzzle/shared";
export function getPuzzleInput() {
return getPuzzleInputFromFileUrl(
import.meta.resolve("./input.txt"),
);
}`;
await Deno.writeTextFile(join(puzzleDirname, "input.ts"), content);
await Deno.writeTextFile(
join(puzzleDirname, "input.txt"),
"",
);
}
async function createReadme() {
await Deno.writeTextFile(join(puzzleDirname, "README.md"), "# Learnings");
}
async function updateWorkspace() {
const workspaceConfig = await Deno.readTextFile("deno.json");
const workspaceJson = JSON.parse(workspaceConfig);
workspaceJson.workspace = [...workspaceJson.workspace, `./${puzzleDirname}`];
await Deno.writeTextFile("deno.json", JSON.stringify(workspaceJson, null, 2));
}