@staticcms/proxy-server-lite
Version:
Proxy server to be used with Static CMS proxy backend
71 lines • 2.8 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUpdateDate = exports.move = exports.deleteFile = exports.writeFile = exports.listRepoFiles = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = require("fs");
async function listFiles(dir, extension, depth) {
if (depth <= 0) {
return [];
}
try {
const dirents = await fs_1.promises.readdir(dir, { withFileTypes: true });
const files = await Promise.all(dirents.map(dirent => {
const res = path_1.default.join(dir, dirent.name);
const isDirectory = dirent.isDirectory();
if (depth > 1 && isDirectory) {
return listFiles(res, extension, depth - 1);
}
return {
file: extension === '' ? res : [res].filter(f => f.endsWith(extension))[0],
isDirectory,
};
}));
return [].concat(...files);
}
catch (e) {
return [];
}
}
async function listRepoFiles(repoPath, folder, extension, depth) {
const files = await listFiles(path_1.default.join(repoPath, folder), extension, depth);
return files
.filter(f => f.file)
.map(f => {
return { file: f.file.slice(repoPath.length + 1), isDirectory: f.isDirectory };
});
}
exports.listRepoFiles = listRepoFiles;
async function writeFile(filePath, content) {
await fs_1.promises.mkdir(path_1.default.dirname(filePath), { recursive: true });
await fs_1.promises.writeFile(filePath, content);
}
exports.writeFile = writeFile;
async function deleteFile(repoPath, filePath) {
await fs_1.promises.unlink(path_1.default.join(repoPath, filePath)).catch(() => undefined);
}
exports.deleteFile = deleteFile;
async function moveFile(from, to) {
await fs_1.promises.mkdir(path_1.default.dirname(to), { recursive: true });
await fs_1.promises.rename(from, to);
}
async function move(from, to) {
// move file
await moveFile(from, to);
// move children
const sourceDir = path_1.default.dirname(from);
const destDir = path_1.default.dirname(to);
const allFiles = await listFiles(sourceDir, '', 100);
await Promise.all(allFiles.map(item => moveFile(item.file, item.file.replace(sourceDir, destDir))));
}
exports.move = move;
async function getUpdateDate(repoPath, filePath) {
return fs_1.promises
.stat(path_1.default.join(repoPath, filePath))
.then(stat => stat.mtime)
.catch(() => new Date());
}
exports.getUpdateDate = getUpdateDate;
//# sourceMappingURL=fs.js.map