@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
203 lines • 7.28 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseFs = void 0;
exports.newBaseFs = newBaseFs;
const filemeta_1 = require("../../../domain/fs/vo/filemeta");
const fileinfo_1 = require("../../../domain/fs/vo/fileinfo");
const file_1 = require("../../../domain/fs/vo/file");
const dir_1 = require("../../../domain/fs/vo/dir");
const path = __importStar(require("path"));
const log_1 = require("../../../../pkg/log");
// Create domain-specific logger for fs operations
const log = (0, log_1.getDomainLogger)('fs', { component: 'basefs' });
class BaseFs {
constructor(fs, roots) {
this.fs = fs;
this.roots = roots;
}
/**
* Convert relative path to absolute path using the first root
* In BaseFs context, all paths are relative to the BaseFs root, even if they start with "/"
*/
toAbsolutePath(name) {
// Use the first root as the base path (like Go's BasePathFs)
if (this.roots.length === 0) {
return name;
}
const root = this.roots[0];
// Handle empty string as root directory
if (name === '') {
return root;
}
// Ensure the name is relative to the root
// If name is already absolute, we assume it is relative to the root
if (path.isAbsolute(name)) {
if (name.startsWith(root)) {
return name; // Already absolute and starts with root
}
// If it is absolute but does not start with root, we treat it as relative
return path.join(root, name.substring(1)); // Remove leading "/" if present
}
return path.join(root, name); // Join root with relative name
}
/**
* Stat returns file info with opener function
*/
async stat(name) {
const absPath = this.toAbsolutePath(name);
try {
const fi = await this.fs.stat(absPath);
if (fi.isDir()) {
const fileInfo = (0, fileinfo_1.newFileInfo)(fi, absPath);
fileInfo.meta().setOpenFunc(async () => {
return await this.openDir(name);
});
return fileInfo;
}
const fileInfo = (0, fileinfo_1.newFileInfo)(fi, absPath);
const root = this.getRoot(absPath);
fileInfo.meta().setComponentRoot(root);
fileInfo.meta().setOpenFunc(async () => {
return await this.openInternal(name);
});
return fileInfo;
}
catch (error) {
throw error;
}
}
/**
* Open opens a file or directory
*/
async open(name) {
const absPath = this.toAbsolutePath(name);
const fi = await this.fs.stat(absPath);
if (fi.isDir()) {
return await this.openDir(name);
}
return await this.openInternal(name);
}
/**
* Private method to open a file internally
*/
async openInternal(name, isDir = false) {
const absPath = this.toAbsolutePath(name);
const f = await this.fs.open(absPath);
const root = this.getRoot(absPath);
const meta = (0, filemeta_1.newFileMeta)(absPath);
meta.setComponentRoot(root);
if (isDir) {
return (0, file_1.newDirFileWithMeta)(f, meta, this.fs);
}
return (0, file_1.newFileWithMeta)(f, meta, this.fs);
}
/**
* Get the matching root path for a given name
*/
getRoot(name) {
for (const root of this.roots) {
if (name.startsWith(root)) {
return root;
}
}
return '';
}
/**
* Private method to open a directory
*/
async openDir(name) {
const f = await this.openInternal(name, true);
const absPath = this.toAbsolutePath(name);
const root = this.getRoot(absPath);
const meta = (0, filemeta_1.newFileMeta)(absPath);
meta.setComponentRoot(root);
return (0, dir_1.newDirFile)(f, meta, this.fs);
}
// Implement Fs interface methods by delegating to the underlying fs
async create(name) {
const absPath = this.toAbsolutePath(name);
return await this.fs.create(absPath);
}
async mkdir(name, perm) {
const absPath = this.toAbsolutePath(name);
return await this.fs.mkdir(absPath, perm);
}
async mkdirAll(path, perm) {
const absPath = this.toAbsolutePath(path);
return await this.fs.mkdirAll(absPath, perm);
}
async openFile(name, flag, perm) {
const absPath = this.toAbsolutePath(name);
return await this.fs.openFile(absPath, flag, perm);
}
async remove(name) {
const absPath = this.toAbsolutePath(name);
return await this.fs.remove(absPath);
}
async removeAll(path) {
const absPath = this.toAbsolutePath(path);
return await this.fs.removeAll(absPath);
}
async rename(oldname, newname) {
const oldAbsPath = this.toAbsolutePath(oldname);
const newAbsPath = this.toAbsolutePath(newname);
return await this.fs.rename(oldAbsPath, newAbsPath);
}
name() {
return `BaseFs(${this.fs.name()})`;
}
async chmod(name, mode) {
const absPath = this.toAbsolutePath(name);
return await this.fs.chmod(absPath, mode);
}
async chown(name, uid, gid) {
const absPath = this.toAbsolutePath(name);
return await this.fs.chown(absPath, uid, gid);
}
async chtimes(name, atime, mtime) {
const absPath = this.toAbsolutePath(name);
return await this.fs.chtimes(absPath, atime, mtime);
}
}
exports.BaseFs = BaseFs;
/**
* Creates a new BaseFs instance
* TypeScript version of Go's NewBaseFs function
*/
function newBaseFs(fs, roots) {
return new BaseFs(fs, roots);
}
//# sourceMappingURL=basefs.js.map