UNPKG

nodejs-file-utils

Version:

File Read and Write Apis with Cached content in NodeJs

173 lines (172 loc) 8.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listFiles = exports.copyDirectory = exports.readFiles = exports.createFiles = exports.deleteDir = exports.createTempDir = void 0; var tslib_1 = require("tslib"); var fs_1 = require("fs"); var promises_1 = require("fs/promises"); var os_1 = require("os"); var path_1 = require("path"); var rimraf_1 = require("rimraf"); var createTempDir = function (prefix) { var tempDirPath = (0, fs_1.mkdtempSync)((0, path_1.join)((0, os_1.tmpdir)(), prefix)); var tempDirPathUnixStyle = tempDirPath.split("\\").join("/"); return tempDirPathUnixStyle; }; exports.createTempDir = createTempDir; var deleteDir = function (dir) { (0, rimraf_1.sync)(dir); }; exports.deleteDir = deleteDir; var createFiles = function (dir, paths) { Object.keys(paths).forEach(function (path) { if (path.endsWith("/")) { var completePath = (0, path_1.join)(dir, path); (0, fs_1.mkdirSync)(completePath, { recursive: true }); } else { var data = paths[path]; var completePath = (0, path_1.join)(dir, path); var pathDirectory = (0, path_1.dirname)(completePath); (0, fs_1.mkdirSync)(pathDirectory, { recursive: true }); (0, fs_1.writeFileSync)(completePath, data); } }); }; exports.createFiles = createFiles; var readFiles = function (dir) { if (!(0, fs_1.existsSync)(dir)) { return {}; } var files = {}; var queue = ["."]; var currentDir = queue.shift(); var _loop_1 = function () { var completePath = (0, path_1.join)(dir, currentDir); var dirFiles = (0, fs_1.readdirSync)(completePath); if (dirFiles.length == 0) { dirFiles[currentDir + "/"] = ""; } dirFiles.forEach(function (file) { var filePath = (0, path_1.join)(completePath, file); var stats = (0, fs_1.statSync)(filePath); if (stats.isDirectory()) { queue.push((0, path_1.join)(currentDir, file)); } else if (stats.isFile()) { var data = (0, fs_1.readFileSync)(filePath, { encoding: "utf8" }); files[(0, path_1.join)(currentDir, file).split("\\").join("/")] = data; } }); currentDir = queue.shift(); }; while (currentDir) { _loop_1(); } return files; }; exports.readFiles = readFiles; var copyDirectory = function (source, target) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var dirsToCopy, _loop_2; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: dirsToCopy = [source]; _loop_2 = function () { var dirToCopy, files; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: dirToCopy = dirsToCopy.shift(); return [4 /*yield*/, (0, promises_1.readdir)(dirToCopy)]; case 1: files = _b.sent(); return [4 /*yield*/, Promise.all(files.map(function (file) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var fileOrDirPath, stats, targetPath, targetDir; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: fileOrDirPath = (0, path_1.join)(dirToCopy, file); return [4 /*yield*/, (0, promises_1.stat)(fileOrDirPath)]; case 1: stats = _a.sent(); if (!stats.isDirectory()) return [3 /*break*/, 2]; dirsToCopy.push(fileOrDirPath); return [3 /*break*/, 5]; case 2: targetPath = (0, path_1.join)(target, (0, path_1.relative)(source, fileOrDirPath)); targetDir = (0, path_1.dirname)(targetPath); return [4 /*yield*/, (0, promises_1.mkdir)(targetDir, { recursive: true })]; case 3: _a.sent(); return [4 /*yield*/, (0, promises_1.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*/]; } }); }); }; exports.copyDirectory = copyDirectory; var listFiles = function (dir, suffix) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var filePaths, queue, currentDir, files, normalizedFiles; return tslib_1.__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*/, (0, promises_1.readdir)((0, path_1.join)(dir, currentDir))]; case 2: files = _a.sent(); return [4 /*yield*/, Promise.all(files.map(function (file) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var stats; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, (0, promises_1.stat)((0, path_1.join)(dir, currentDir, file))]; case 1: stats = _a.sent(); if (stats.isDirectory()) { queue.push((0, path_1.join)(currentDir, file)); } else if (!suffix || file.endsWith(suffix)) { filePaths.push((0, path_1.join)(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]; } }); }); }; exports.listFiles = listFiles;