UNPKG

nodejs-file-utils

Version:

File Read and Write Apis with Cached content in NodeJs

57 lines (56 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.saveJsonFileStore = exports.updateJsonFileStore = exports.readJsonFileStore = void 0; var tslib_1 = require("tslib"); var promises_1 = require("fs/promises"); var fileStore_1 = require("./fileStore"); var readJson = function (path) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var jsonContent, _json; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, (0, promises_1.readFile)(path, { encoding: "utf8" })]; case 1: jsonContent = _a.sent(); _json = JSON.parse(jsonContent); return [2 /*return*/, _json]; } }); }); }; var writeJson = function (path, json) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var _json; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _json = JSON.stringify(json, null, 2) + "\n"; return [4 /*yield*/, (0, promises_1.writeFile)(path, _json)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; var readJsonFileStore = function (path, force) { if (force === void 0) { force = false; } return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, (0, fileStore_1.readFileStore)(path, readJson, force)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; exports.readJsonFileStore = readJsonFileStore; var updateJsonFileStore = function (path, json) { return (0, fileStore_1.updateFileStore)(path, json); }; exports.updateJsonFileStore = updateJsonFileStore; var saveJsonFileStore = function (path) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, (0, fileStore_1.saveFileStore)(path, writeJson)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; exports.saveJsonFileStore = saveJsonFileStore;