UNPKG

@mdfriday/foundry

Version:

The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.

281 lines 9.95 kB
"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.MockFs = void 0; const path = __importStar(require("path")); class MockFile { constructor(content, filePath) { this.position = 0; this.content = typeof content === 'string' ? new TextEncoder().encode(content) : content; this.filePath = filePath; } async close() { // Mock implementation } async read(buffer) { const remaining = this.content.length - this.position; const bytesToRead = Math.min(buffer.length, remaining); buffer.set(this.content.subarray(this.position, this.position + bytesToRead)); this.position += bytesToRead; return { bytesRead: bytesToRead, buffer }; } async readAt(buffer, offset) { const remaining = this.content.length - offset; const bytesToRead = Math.min(buffer.length, remaining); if (bytesToRead > 0) { buffer.set(this.content.subarray(offset, offset + bytesToRead)); } return { bytesRead: bytesToRead, buffer }; } async seek(offset, whence) { switch (whence) { case 0: // SEEK_SET this.position = offset; break; case 1: // SEEK_CUR this.position += offset; break; case 2: // SEEK_END this.position = this.content.length + offset; break; } return this.position; } async write(buffer) { // Expand content if necessary const newSize = Math.max(this.content.length, this.position + buffer.length); const newContent = new Uint8Array(newSize); newContent.set(this.content); newContent.set(buffer, this.position); this.content = newContent; this.position += buffer.length; return { bytesWritten: buffer.length, buffer }; } async writeAt(buffer, offset) { const newSize = Math.max(this.content.length, offset + buffer.length); const newContent = new Uint8Array(newSize); newContent.set(this.content); newContent.set(buffer, offset); this.content = newContent; return { bytesWritten: buffer.length, buffer }; } name() { return this.filePath; } async readdir(count) { return []; // Mock implementation } async readdirnames(n) { return []; // Mock implementation } async stat() { return { name: () => path.basename(this.filePath), size: () => this.content.length, mode: () => 0o644, modTime: () => new Date(), isDir: () => false, sys: () => null }; } async sync() { // Mock implementation } async truncate(size) { this.content = this.content.subarray(0, size); } async writeString(s) { const buffer = new TextEncoder().encode(s); const result = await this.write(buffer); return { bytesWritten: result.bytesWritten }; } } class MockFs { constructor() { this.files = new Map(); this.directories = new Set(); } // Add method to set mock file content setFile(filePath, content) { const resolvedPath = path.resolve(filePath); this.files.set(resolvedPath, content); // Also add directory path const dir = path.dirname(resolvedPath); this.addDirectoryPath(dir); } addDirectoryPath(dirPath) { let currentPath = path.resolve(dirPath); while (currentPath !== path.dirname(currentPath)) { this.directories.add(currentPath); currentPath = path.dirname(currentPath); } } async create(name) { const resolvedPath = path.resolve(name); this.files.set(resolvedPath, ''); this.addDirectoryPath(path.dirname(resolvedPath)); return new MockFile('', resolvedPath); } async mkdir(name, perm) { const resolvedPath = path.resolve(name); this.directories.add(resolvedPath); } async mkdirAll(dirPath, perm) { const resolvedPath = path.resolve(dirPath); this.addDirectoryPath(resolvedPath); } async open(name) { const resolvedPath = path.resolve(name); const content = this.files.get(resolvedPath); if (content === undefined) { throw new Error(`ENOENT: no such file or directory, open '${name}'`); } return new MockFile(content, resolvedPath); } async openFile(name, flag, perm) { return this.open(name); } async remove(name) { const resolvedPath = path.resolve(name); this.files.delete(resolvedPath); this.directories.delete(resolvedPath); } async removeAll(dirPath) { const resolvedPath = path.resolve(dirPath); // Remove all files under this directory for (const filePath of this.files.keys()) { if (filePath.startsWith(resolvedPath + path.sep) || filePath === resolvedPath) { this.files.delete(filePath); } } // Remove all directories under this directory for (const dirPath of this.directories) { if (dirPath.startsWith(resolvedPath + path.sep) || dirPath === resolvedPath) { this.directories.delete(dirPath); } } } async rename(oldname, newname) { const oldPath = path.resolve(oldname); const newPath = path.resolve(newname); const content = this.files.get(oldPath); if (content !== undefined) { this.files.set(newPath, content); this.files.delete(oldPath); } } async stat(name) { const resolvedPath = path.resolve(name); if (this.files.has(resolvedPath)) { const content = this.files.get(resolvedPath); return { name: () => path.basename(resolvedPath), size: () => content.length, mode: () => 0o644, modTime: () => new Date(), isDir: () => false, sys: () => null }; } if (this.directories.has(resolvedPath)) { return { name: () => path.basename(resolvedPath), size: () => 0, mode: () => 0o755, modTime: () => new Date(), isDir: () => true, sys: () => null }; } throw new Error(`ENOENT: no such file or directory, stat '${name}'`); } name() { return 'MockFs'; } async chmod(name, mode) { // Mock implementation } async chown(name, uid, gid) { // Mock implementation } async chtimes(name, atime, mtime) { // Mock implementation } // Legacy methods for compatibility with existing tests async readFile(filePath) { const resolvedPath = path.resolve(filePath); const content = this.files.get(resolvedPath); if (content === undefined) { throw new Error(`ENOENT: no such file or directory, open '${filePath}'`); } return Buffer.from(content); } async writeFile(filePath, data) { const resolvedPath = path.resolve(filePath); this.files.set(resolvedPath, data.toString()); this.addDirectoryPath(path.dirname(resolvedPath)); } async exists(filePath) { const resolvedPath = path.resolve(filePath); return this.files.has(resolvedPath) || this.directories.has(resolvedPath); } async readDir(dirPath) { const resolvedPath = path.resolve(dirPath); const files = []; // Find all files and directories under this path for (const filePath of this.files.keys()) { if (path.dirname(filePath) === resolvedPath) { files.push(path.basename(filePath)); } } for (const dirPath of this.directories) { if (path.dirname(dirPath) === resolvedPath) { files.push(path.basename(dirPath)); } } return files; } async copy(src, dest) { const srcPath = path.resolve(src); const destPath = path.resolve(dest); const content = this.files.get(srcPath); if (content !== undefined) { this.files.set(destPath, content); this.addDirectoryPath(path.dirname(destPath)); } } } exports.MockFs = MockFs; //# sourceMappingURL=mock-fs.js.map