nodejs-file-utils
Version:
File Read and Write Apis with Cached content in NodeJs
54 lines (53 loc) • 1.95 kB
JavaScript
import { __awaiter, __generator } from "tslib";
import { readFile, writeFile } from "fs/promises";
import { readFileStore, updateFileStore, saveFileStore } from "./fileStore";
import { load, dump } from "js-yaml";
var readYaml = function (path) { return __awaiter(void 0, void 0, void 0, function () {
var yamlContent, json;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, readFile(path, {
encoding: "utf8"
})];
case 1:
yamlContent = _a.sent();
json = load(yamlContent);
return [2 /*return*/, json];
}
});
}); };
var writeYaml = function (path, json) { return __awaiter(void 0, void 0, void 0, function () {
var yaml;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
yaml = dump(json);
return [4 /*yield*/, writeFile(path, yaml)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); };
export var readYamlFileStore = 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, readYaml, force)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
export var updateYamlFileStore = function (path, json) {
return updateFileStore(path, json);
};
export var saveYamlFileStore = 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, writeYaml)];
case 1: return [2 /*return*/, _a.sent()];
}
});
}); };