nodejs-file-utils
Version:
File Read and Write Apis with Cached content in NodeJs
60 lines (59 loc) • 2.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveYamlFileStore = exports.updateYamlFileStore = exports.readYamlFileStore = void 0;
var tslib_1 = require("tslib");
var promises_1 = require("fs/promises");
var fileStore_1 = require("./fileStore");
var js_yaml_1 = require("js-yaml");
var readYaml = function (path) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var yamlContent, 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:
yamlContent = _a.sent();
json = (0, js_yaml_1.load)(yamlContent);
return [2 /*return*/, json];
}
});
}); };
var writeYaml = function (path, json) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var yaml;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
yaml = (0, js_yaml_1.dump)(json);
return [4 /*yield*/, (0, promises_1.writeFile)(path, yaml)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); };
var readYamlFileStore = 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, readYaml, force)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
exports.readYamlFileStore = readYamlFileStore;
var updateYamlFileStore = function (path, json) {
return (0, fileStore_1.updateFileStore)(path, json);
};
exports.updateYamlFileStore = updateYamlFileStore;
var saveYamlFileStore = 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, writeYaml)];
case 1: return [2 /*return*/, _a.sent()];
}
});
}); };
exports.saveYamlFileStore = saveYamlFileStore;