UNPKG

automin

Version:

"Create a minified mirror version of your js, css, html, json files"

44 lines (43 loc) 1.37 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.save_cache = exports.load_cache = void 0; const env_res_1 = __importDefault(require("env-res")); const fs_1 = require("fs"); const path_1 = require("path"); const ROOT = __dirname; const load_cache = (name) => { try { const p = (0, path_1.join)(ROOT, `${name}.json`); let content = (0, fs_1.readFileSync)(p, { encoding: "utf8" }); // Validate content = JSON.stringify(JSON.parse(content)); if (content.length < 2) content = "{}"; env_res_1.default.set(name, content); } catch (err) { env_res_1.default.set(name, "{}"); } }; exports.load_cache = load_cache; const save_cache = (name) => { try { const p = (0, path_1.join)(ROOT, `${name}.json`); let content = env_res_1.default.get(name); // Try to parse then stringify for any errors content = JSON.stringify(JSON.parse(content)); (0, fs_1.writeFileSync)(p, content, { encoding: "utf8", flag: "w" }); } catch (err) { console.error("Failed to save cache"); } }; exports.save_cache = save_cache;