-
Notifications
You must be signed in to change notification settings - Fork 0
/
plopfile.js
209 lines (164 loc) · 4.94 KB
/
plopfile.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
import { selectAll } from "css-select";
import render from "dom-serializer";
import { parseDocument } from "htmlparser2";
import TurndownService from "turndown";
import cacache from "cacache";
import "dotenv/config";
// AoC
const baseUrl = new URL(`https://adventofcode.com/`);
const sessionToken = process.env.AOC_SESSION_TOKEN;
const contact = process.env.AOC_CONTACT;
const opts = sessionToken
? {
headers: {
cookie: `session=${sessionToken}`,
"user-agent": `https://github.com/titonobre/adventofcode by ${contact}`,
},
}
: {};
// HTML to Markdown
TurndownService.prototype.escape = (value) => value;
const turndownService = new TurndownService({
headingStyle: "atx",
codeBlockStyle: "fenced",
bulletListMarker: "-",
preformattedCode: true,
});
turndownService.addRule("strikethrough", {
filter: ["title"],
replacement: function (content) {
return "# " + content;
},
});
const markdownReplacements = [
[/`_(.*)_`/gi, "_`$1`_"], // italic code
];
// Cache
const cachePath = resolve(dirname(fileURLToPath(import.meta.url)), ".cache");
const cacheTtl = 1000 * 60 * 60; // one hour
// Aux Functions
async function fetchContents(url) {
const cacheKey = url.href;
const cachedDataInfo = await cacache.get.info(cachePath, cacheKey);
const cacheValid =
!!cachedDataInfo && Date.now() - cachedDataInfo.time < cacheTtl;
if (cacheValid) {
try {
const cachedData = await cacache.get(cachePath, cacheKey);
if (cachedData) {
return cachedData.data.toString("utf-8");
}
} catch (e) {
// no cache available
}
}
console.log("Fetching " + url);
const response = await fetch(url, opts);
const text = await response.text();
await cacache.put(cachePath, cacheKey, text);
return text;
}
async function fetchReadme(year, day) {
const url = new URL(`/${year}/day/${day}`, baseUrl);
const html = await fetchContents(url);
const document = parseDocument(html);
const contents = selectAll(`title, article`, document);
const markdown = turndownService.turndown(render(contents));
return markdownReplacements.reduce(
(source, replacement) => source.replace(...replacement),
markdown
);
}
async function fetchExample(year, day) {
const url = new URL(`/${year}/day/${day}`, baseUrl);
const html = await fetchContents(url);
const document = parseDocument(html);
const elements = selectAll(`article > pre > code`, document);
return elements.at(0)?.children[0].nodeValue ?? "";
}
async function fetchAnswers(year, day) {
const url = new URL(`/${year}/day/${day}`, baseUrl);
const html = await fetchContents(url);
const document = parseDocument(html);
const elements = selectAll(
`p:contains('Your puzzle answer was') > code`,
document
);
const answers = elements.map((el) => el.children[0].nodeValue);
return answers.join("\n") + "\n";
}
async function fetchInput(year, day) {
const url = new URL(`/${year}/day/${day}/input`, baseUrl);
return await fetchContents(url);
}
function padDay(text) {
return text.padStart(2, "0");
}
export default async function (plop) {
plop.setHelper("padDay", padDay);
plop.setGenerator("solution", {
description: "solution to advent of code puzzle",
prompts: [
{
type: "input",
name: "year",
message: "year",
default: new Date().getFullYear(),
validate: (value) => /^\d{4}$/.test(value),
},
{
type: "input",
name: "day",
message: "day",
validate: (value) => /^\d{1,2}$/.test(value),
},
],
actions: [
{
type: "add",
path: "{{year}}/{{padDay day}}/readme.md",
force: true,
transform: async (_, { year, day }) => fetchReadme(year, day),
},
{
type: "add",
path: "{{year}}/{{padDay day}}/example.txt",
force: true,
transform: async (_, { year, day }) => fetchExample(year, day),
},
{
type: "add",
path: "{{year}}/{{padDay day}}/input.txt",
force: true,
transform: async (_, { year, day }) => fetchInput(year, day),
},
{
type: "add",
path: "{{year}}/{{padDay day}}/answers.txt",
force: true,
transform: async (_, { year, day }) => fetchAnswers(year, day),
},
{
type: "add",
path: "{{year}}/{{padDay day}}/index.js",
templateFile: "0000/00/index.js",
skipIfExists: true,
},
{
type: "add",
path: "{{year}}/{{padDay day}}/solution.js",
templateFile: "0000/00/solution.js",
skipIfExists: true,
},
{
type: "add",
path: "{{year}}/{{padDay day}}/test.js",
templateFile: "0000/00/test.js",
skipIfExists: true,
transform: async (template, { year, day }) => template.replace("0000/00", `${year}/${padDay(day)}`),
},
],
});
}