file-cms
Version:
File based Content Management System, easy to use with content stored in native file system
69 lines (68 loc) • 3.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyDir = void 0;
var tslib_1 = require("tslib");
var path_1 = require("path");
var Config_1 = require("./Config");
var promises_1 = require("fs/promises");
var copyDir = function (sourceDir, targetDir) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var rootDir, fullSourceDir, dirsToCopy, _loop_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
rootDir = Config_1.Config.getRootDir();
fullSourceDir = (0, path_1.join)(rootDir, sourceDir);
dirsToCopy = [fullSourceDir];
_loop_1 = 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, targetDirname;
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)(targetDir, (0, path_1.relative)(fullSourceDir, fileOrDirPath));
targetDirname = (0, path_1.dirname)(targetPath);
return [4 /*yield*/, (0, promises_1.mkdir)(targetDirname, { 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_1()];
case 2:
_a.sent();
return [3 /*break*/, 1];
case 3: return [2 /*return*/];
}
});
}); };
exports.copyDir = copyDir;