nodejs-file-utils
Version:
File Read and Write Apis with Cached content in NodeJs
55 lines (54 loc) • 2.22 kB
JavaScript
import { __awaiter, __generator, __spreadArray } from "tslib";
import { readFile, writeFile } from "fs/promises";
import { readFileStore, updateFileStore, saveFileStore } from "./fileStore";
var readIgnoreFile = function (path) { return __awaiter(void 0, void 0, void 0, function () {
var ignoreContent, _ignore;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, 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 __awaiter(void 0, void 0, void 0, function () {
var _ignore, _json;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_ignore = __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*/, writeFile(path, _json)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); };
export var readIgnoreFileStore = 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, readIgnoreFile, force)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
export var updateIgnoreFileStore = function (path, json) {
return updateFileStore(path, json);
};
export var saveIgnoreFileStore = 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, writeIgnoreFile)];
case 1: return [2 /*return*/, _a.sent()];
}
});
}); };