nodejs-file-utils
Version:
File Read and Write Apis with Cached content in NodeJs
61 lines (60 loc) • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveIgnoreFileStore = exports.updateIgnoreFileStore = exports.readIgnoreFileStore = void 0;
var tslib_1 = require("tslib");
var promises_1 = require("fs/promises");
var fileStore_1 = require("./fileStore");
var readIgnoreFile = function (path) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var ignoreContent, _ignore;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, promises_1.readFile)(path, { encoding: "utf8" })];
case 1:
ignoreContent = _a.sent();
_ignore = ignoreContent.trim().length == 0 ? [] : ignoreContent.split("\n");
return [2 /*return*/, _ignore];
}
});
}); };
var writeIgnoreFile = function (path, ignore) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var _ignore, _json;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_ignore = tslib_1.__spreadArray([], ignore, true);
if (_ignore[_ignore.length - 1] != "") {
_ignore.push(""); // add a new line at the end of the file
}
_json = _ignore.join("\n");
return [4 /*yield*/, (0, promises_1.writeFile)(path, _json)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); };
var readIgnoreFileStore = 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, readIgnoreFile, force)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
exports.readIgnoreFileStore = readIgnoreFileStore;
var updateIgnoreFileStore = function (path, json) {
return (0, fileStore_1.updateFileStore)(path, json);
};
exports.updateIgnoreFileStore = updateIgnoreFileStore;
var saveIgnoreFileStore = 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, writeIgnoreFile)];
case 1: return [2 /*return*/, _a.sent()];
}
});
}); };
exports.saveIgnoreFileStore = saveIgnoreFileStore;