UNPKG

aoc-copilot

Version:

Advent of Code automatic runner for examples and inputs

49 lines 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CACHE_DIR = void 0; exports.cachedDays = cachedDays; exports.read = read; exports.remove = remove; exports.write = write; const node_fs_1 = require("node:fs"); const promises_1 = require("node:fs/promises"); const node_path_1 = require("node:path"); // https://stackoverflow.com/a/26227660 const CACHE_DIR = (0, node_path_1.join)(process.env.LOCALAPPDATA || (process.platform == 'darwin' // APPDATA for roaming ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.config"), 'AoC-Copilot'); exports.CACHE_DIR = CACHE_DIR; function cachedDays(subdir) { return (0, node_fs_1.readdirSync)(CACHE_DIR) .filter(dir => /^(?:\d{4})$/.test(dir) && parseInt(dir) >= 2015 && parseInt(dir) <= new Date().getFullYear() - (new Date().getMonth() < 12 ? 1 : 0)) .map(Number) .sort((a, b) => a - b) .map(year => { const dir = (0, node_path_1.join)(CACHE_DIR, year.toString(), subdir); const days = (0, node_fs_1.readdirSync)(dir, { withFileTypes: true }) .filter(e => e.isFile() && (subdir === 'puzzles' ? /^\d+\.html$/ : /^\d+\.json$/).test(e.name)) .map(e => ({ day: parseInt((0, node_path_1.parse)(e.name).name), path: (0, node_path_1.join)(dir, e.name) })) .sort((a, b) => a.day - b.day); return { year, days }; }); } async function read(filePath) { const pathname = (0, node_path_1.join)(CACHE_DIR, filePath); await (0, promises_1.mkdir)((0, node_path_1.dirname)(pathname), { recursive: true }); return (0, promises_1.readFile)(pathname, { encoding: 'utf-8' }); } function remove(filePath) { const pathname = (0, node_path_1.join)(CACHE_DIR, filePath); return (0, promises_1.rm)(pathname, { force: true }); } async function write(filePath, data) { const pathname = (0, node_path_1.join)(CACHE_DIR, filePath); await (0, promises_1.mkdir)((0, node_path_1.dirname)(pathname), { recursive: true }); return (0, promises_1.writeFile)(pathname, data); } //# sourceMappingURL=cache.js.map