UNPKG

aoc-automation

Version:

Advent of Code tool to automate the repetitive parts of AoC.

174 lines (152 loc) 4.97 kB
import { stripIndents } from "common-tags"; import toFixed from "../utils/toFixed.js"; import { readGlobalReadme } from "../io/readme.js"; const renderGlobalYearInfo = (config) => { try { const currentYear = config.year; let globalReadme = readGlobalReadme(); let totalStars = 0; let totalTime = 0; let stars = config.days.map(({ part1, part2 }, index) => { if (part1.solved) { totalStars++; totalTime += part1.time ?? 0; } if (part2.solved) { totalStars++; totalTime += part2.time ?? 0; } const star = part1.solved && part2.solved ? "\u2605" : part1.solved || part2.solved ? "\u2606" : "\u2B52"; return star; }).join(""); let regex = /<!--SOLUTIONS-->([\s\S]+?)<!--\/SOLUTIONS-->/; let match = globalReadme.match(regex); let badges = ""; let results = ""; if (match != null) { const lines = match[1].split("\n").filter((line) => line.trim() != ""); let yearReplaceIndex = lines.findIndex( (line) => line.includes(`badge/${currentYear}`) ); if (yearReplaceIndex == -1) { let yearInsertIndex = -1; for (let index = 0; index < lines.length; index++) { const [left, right] = lines[index].split("badge/"); if (right != void 0) { if (Number(right.substring(0, 4)) < currentYear) { yearInsertIndex = index; break; } } } if (yearInsertIndex == -1) { yearInsertIndex = lines.length; } lines.splice(yearReplaceIndex = yearInsertIndex, 0, ""); } if (totalStars >= 49) { stars = stars.replaceAll("\u2605", "\u2728"); } const color = totalStars >= 40 ? "green" : totalStars >= 20 ? "yellow" : "gray"; lines[yearReplaceIndex] = `[![Year](https://badgen.net/badge/${currentYear}/${stars}/${color}?icon=typescript&labelColor=blue&scale=1.3)](src/${currentYear}) `; badges = lines.join("\n"); } regex = /<!--RESULTS-->([\s\S]+?)<!--\/RESULTS-->/; match = globalReadme.match(regex); if (match != null) { let lines = match[1].split("\n"); const firstContentIndex = lines.findIndex( (line) => line.trim() !== "" ); let lastContentIndex = -1; for (let index = lines.length - 1; index >= 0; index--) { const line = lines[index]; if (line.trim() !== "") { lastContentIndex = index; break; } } lines = lines.slice(firstContentIndex, lastContentIndex + 1); let yearReplaceIndex = lines.findIndex((line) => line.includes(`Year ${currentYear}`)) - 1; if (yearReplaceIndex < 0) { let yearInsertIndex = -1; for (let index = 0; index < lines.length; index++) { const [left, right] = lines[index].split("Year "); if (right != void 0) { if (Number(right) < currentYear) { yearInsertIndex = index - 1; break; } } } if (yearInsertIndex > -1) { yearReplaceIndex = yearInsertIndex; } else { yearReplaceIndex = lines.length; } lines.splice(yearReplaceIndex, 0, ...["", "", "", "", "", ""]); } const yearInfo = [ "```", `Year ${currentYear}`, `Total stars: ${totalStars}/50`, `Total time: ${toFixed(totalTime)}ms`, "```", "" ]; lines.splice(yearReplaceIndex, 6, ...yearInfo); if (lines[lines.length - 1] == "") { lines.pop(); } results = lines.join("\n"); } return { badges, results }; } catch (error) { if (error.message.indexOf("no such file or directory") == -1) { console.error({ error }); } return void 0; } }; const readmeMD = ({ language }, startCmd, installCmd) => { const lang = language === "ts" ? "TypeScript" : "JavaScript"; const yearBadges = ""; const results = ""; return stripIndents` <!-- Entries between SOLUTIONS and RESULTS tags are auto-generated --> [![Node](https://badgen.net/badge/Node/v16.13.0+/blue)](https://nodejs.org/en/download/) ![Language](https://badgen.net/badge/Language/${lang}/blue) [![Template](https://badgen.net/badge/Template/aoc-automation/blue)](https://github.com/terryaney/aoc-automation) # 🎄 Advent of Code 🎄 ## Solutions <!--SOLUTIONS--> ${yearBadges} <!--/SOLUTIONS--> _Click a badge to go to the specific year._ --- ## Installation \`\`\` ${installCmd} \`\`\` ## Running in dev mode \`\`\` ${startCmd} <year> <day> \`\`\` Example: \`\`\` ${startCmd} 2023 1 \`\`\` --- ## Results <!--RESULTS--> ${results} <!--/RESULTS--> --- ✨🎄🎁🎄🎅🎄🎁🎄✨ `; }; var readmeMD_default = readmeMD; export { readmeMD_default as default, renderGlobalYearInfo };