UNPKG

nodejs-file-utils

Version:

File Read and Write Apis with Cached content in NodeJs

51 lines (50 loc) 1.91 kB
import { __awaiter, __generator } from "tslib"; import { readFile, writeFile } from "fs/promises"; import { readFileStore, updateFileStore, saveFileStore } from "./fileStore"; var readJson = function (path) { return __awaiter(void 0, void 0, void 0, function () { var jsonContent, _json; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, readFile(path, { encoding: "utf8" })]; case 1: jsonContent = _a.sent(); _json = JSON.parse(jsonContent); return [2 /*return*/, _json]; } }); }); }; var writeJson = function (path, json) { return __awaiter(void 0, void 0, void 0, function () { var _json; return __generator(this, function (_a) { switch (_a.label) { case 0: _json = JSON.stringify(json, null, 2) + "\n"; return [4 /*yield*/, writeFile(path, _json)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; export var readJsonFileStore = function (path, force) { if (force === void 0) { force = false; } return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, readFileStore(path, readJson, force)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; export var updateJsonFileStore = function (path, json) { return updateFileStore(path, json); }; export var saveJsonFileStore = function (path) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, saveFileStore(path, writeJson)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); };