UNPKG

nodejs-file-utils

Version:

File Read and Write Apis with Cached content in NodeJs

164 lines (163 loc) 7.53 kB
import { __awaiter, __generator } from "tslib"; import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, statSync, writeFileSync } from "fs"; import { copyFile, mkdir, readdir, stat } from "fs/promises"; import { tmpdir } from "os"; import { dirname, join, join as pathJoin, relative } from "path"; import { sync as rimrafSync } from "rimraf"; export var createTempDir = function (prefix) { var tempDirPath = mkdtempSync(pathJoin(tmpdir(), prefix)); var tempDirPathUnixStyle = tempDirPath.split("\\").join("/"); return tempDirPathUnixStyle; }; export var deleteDir = function (dir) { rimrafSync(dir); }; export var createFiles = function (dir, paths) { Object.keys(paths).forEach(function (path) { if (path.endsWith("/")) { var completePath = pathJoin(dir, path); mkdirSync(completePath, { recursive: true }); } else { var data = paths[path]; var completePath = pathJoin(dir, path); var pathDirectory = dirname(completePath); mkdirSync(pathDirectory, { recursive: true }); writeFileSync(completePath, data); } }); }; export var readFiles = function (dir) { if (!existsSync(dir)) { return {}; } var files = {}; var queue = ["."]; var currentDir = queue.shift(); var _loop_1 = function () { var completePath = pathJoin(dir, currentDir); var dirFiles = readdirSync(completePath); if (dirFiles.length == 0) { dirFiles[currentDir + "/"] = ""; } dirFiles.forEach(function (file) { var filePath = pathJoin(completePath, file); var stats = statSync(filePath); if (stats.isDirectory()) { queue.push(pathJoin(currentDir, file)); } else if (stats.isFile()) { var data = readFileSync(filePath, { encoding: "utf8" }); files[pathJoin(currentDir, file).split("\\").join("/")] = data; } }); currentDir = queue.shift(); }; while (currentDir) { _loop_1(); } return files; }; export var copyDirectory = function (source, target) { return __awaiter(void 0, void 0, void 0, function () { var dirsToCopy, _loop_2; return __generator(this, function (_a) { switch (_a.label) { case 0: dirsToCopy = [source]; _loop_2 = function () { var dirToCopy, files; return __generator(this, function (_b) { switch (_b.label) { case 0: dirToCopy = dirsToCopy.shift(); return [4 /*yield*/, readdir(dirToCopy)]; case 1: files = _b.sent(); return [4 /*yield*/, Promise.all(files.map(function (file) { return __awaiter(void 0, void 0, void 0, function () { var fileOrDirPath, stats, targetPath, targetDir; return __generator(this, function (_a) { switch (_a.label) { case 0: fileOrDirPath = join(dirToCopy, file); return [4 /*yield*/, stat(fileOrDirPath)]; case 1: stats = _a.sent(); if (!stats.isDirectory()) return [3 /*break*/, 2]; dirsToCopy.push(fileOrDirPath); return [3 /*break*/, 5]; case 2: targetPath = join(target, relative(source, fileOrDirPath)); targetDir = dirname(targetPath); return [4 /*yield*/, mkdir(targetDir, { recursive: true })]; case 3: _a.sent(); return [4 /*yield*/, copyFile(fileOrDirPath, targetPath)]; case 4: _a.sent(); _a.label = 5; case 5: return [2 /*return*/]; } }); }); }))]; case 2: _b.sent(); return [2 /*return*/]; } }); }; _a.label = 1; case 1: if (!(dirsToCopy.length > 0)) return [3 /*break*/, 3]; return [5 /*yield**/, _loop_2()]; case 2: _a.sent(); return [3 /*break*/, 1]; case 3: return [2 /*return*/]; } }); }); }; export var listFiles = function (dir, suffix) { return __awaiter(void 0, void 0, void 0, function () { var filePaths, queue, currentDir, files, normalizedFiles; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!dir || dir.trim().length == 0) { throw new Error("dir can not be empty"); } filePaths = []; queue = ["."]; currentDir = queue.shift(); _a.label = 1; case 1: if (!currentDir) return [3 /*break*/, 4]; return [4 /*yield*/, readdir(pathJoin(dir, currentDir))]; case 2: files = _a.sent(); return [4 /*yield*/, Promise.all(files.map(function (file) { return __awaiter(void 0, void 0, void 0, function () { var stats; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, stat(pathJoin(dir, currentDir, file))]; case 1: stats = _a.sent(); if (stats.isDirectory()) { queue.push(pathJoin(currentDir, file)); } else if (!suffix || file.endsWith(suffix)) { filePaths.push(pathJoin(currentDir, file)); } return [2 /*return*/]; } }); }); }))]; case 3: _a.sent(); currentDir = queue.shift(); return [3 /*break*/, 1]; case 4: filePaths.sort(); normalizedFiles = filePaths.map(function (file) { return file.split("\\").join("/"); }); return [2 /*return*/, normalizedFiles]; } }); }); };