-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.js
61 lines (48 loc) · 1.7 KB
/
generate.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
import { mkdir, readFile, writeFile } from 'fs/promises';
import { uniq } from 'lodash-es';
import { resolve } from 'path';
import { dateToTz } from './lib/dates.js';
const HEADER_PATH = 'templates/header.js';
const STUB_PATH = 'templates/stub.js';
const PART_1_NAME = '1.js';
const PART_2_NAME = '2.js';
const INPUT_NAME = 'input.txt';
const hydrateTemplate = (template, props = {}) => {
const propNames = template.match(/\{\{[A-Z]+?}}/g).map(match => match.slice(2, -2));
const passedProps = Object.fromEntries(
Object.entries(props).map(([key, val]) => [key.toUpperCase(), val])
);
let hydrated = template;
for (const name of uniq(propNames)) {
hydrated = hydrated.replaceAll(`{{${name}}}`, passedProps[name] ?? '');
}
return hydrated;
}
const now = dateToTz('America/New_York');
const isStart = now.month === 11 && now.day > 23;
const isDecember = now.month === 12;
const day = isStart ? 1 : isDecember ? Math.min(25, now.day + 1) : 25;
const year = isStart || isDecember ? now.year : now.year - 1;
const headerBytes = await readFile(resolve(process.cwd(), HEADER_PATH));
const stubBytes = await readFile(resolve(process.cwd(), STUB_PATH));
const header = headerBytes.toString();
const stub = stubBytes.toString();
const part1Header = hydrateTemplate(header, {
day,
year,
name: 'TBD',
subtitle: '(Part 1)'
});
const part2Header = hydrateTemplate(header, {
day,
year,
name: 'TBD',
subtitle: '(Part 2)',
suffix: '#part2'
});
const dirName = process.argv[2] || 'wip';
const dir = resolve(process.cwd(), dirName);
await mkdir(dir);
writeFile(resolve(dir, PART_1_NAME), part1Header + stub);
writeFile(resolve(dir, PART_2_NAME), part2Header + stub);
writeFile(resolve(dir, INPUT_NAME), '');