UNPKG

file-cms

Version:

File based Content Management System, easy to use with content stored in native file system

65 lines (64 loc) 3.61 kB
import { __awaiter, __generator } from "tslib"; import { dirname, join, relative } from "path"; import { Config } from "./Config"; import { copyFile, mkdir, readdir, stat } from "fs/promises"; export var copyDir = function (sourceDir, targetDir) { return __awaiter(void 0, void 0, void 0, function () { var rootDir, fullSourceDir, dirsToCopy, _loop_1; return __generator(this, function (_a) { switch (_a.label) { case 0: rootDir = Config.getRootDir(); fullSourceDir = join(rootDir, sourceDir); dirsToCopy = [fullSourceDir]; _loop_1 = 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, targetDirname; 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(targetDir, relative(fullSourceDir, fileOrDirPath)); targetDirname = dirname(targetPath); return [4 /*yield*/, mkdir(targetDirname, { 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_1()]; case 2: _a.sent(); return [3 /*break*/, 1]; case 3: return [2 /*return*/]; } }); }); };