UNPKG

file-cms

Version:

File based Content Management System, easy to use with content stored in native file system

39 lines (38 loc) 1.64 kB
import { __awaiter, __generator } from "tslib"; import path from "path"; import { readFile } from "fs/promises"; import { parseContent } from "./parseContent"; import { NotFoundError } from "../errors/NotFoundError"; var cache = {}; export var fetchContentCached = function (rootDir, type, slug) { return __awaiter(void 0, void 0, void 0, function () { var normalizedPath, fileContents, content; return __generator(this, function (_a) { switch (_a.label) { case 0: normalizedPath = path.normalize(path.join(rootDir, type, slug + ".md")); if (!(cache[normalizedPath] == undefined)) return [3 /*break*/, 2]; return [4 /*yield*/, readContentFile(rootDir, type, slug)]; case 1: fileContents = _a.sent(); content = parseContent(fileContents, type, slug); cache[normalizedPath] = content; _a.label = 2; case 2: return [2 /*return*/, cache[normalizedPath]]; } }); }); }; var readContentFile = function (rootDir, type, slug) { return __awaiter(void 0, void 0, void 0, function () { var e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, readFile(path.join(rootDir, type, slug + ".md"), "utf8")]; case 1: return [2 /*return*/, _a.sent()]; case 2: e_1 = _a.sent(); throw new NotFoundError(rootDir, type, slug, e_1); case 3: return [2 /*return*/]; } }); }); };