@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
200 lines • 5.92 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.File = void 0;
exports.newFile = newFile;
exports.newFileWithMeta = newFileWithMeta;
exports.newDirFileWithMeta = newDirFileWithMeta;
exports.newDirFile = newDirFile;
const filemeta_1 = require("./filemeta");
const fileinfo_1 = require("./fileinfo");
const path = __importStar(require("path"));
/**
* File represents a file with metadata
* TypeScript version of Go's File struct
*/
class File {
constructor(file, fileMeta, fs, isDir = false) {
this.file = file;
this.fileMeta = fileMeta;
this.fs = fs;
this._isDir = isDir;
}
/**
* Check if this is a no-op file (null file)
*/
isNop() {
return this.file === null;
}
/**
* Close the file
*/
async close() {
if (this.file === null) {
return;
}
return await this.file.close();
}
/**
* ReadDir reads directory entries
*/
async readDir(count) {
const result = [];
if (this._isDir && this.file) {
const fis = await this.file.readdir(count);
for (const fi of fis) {
const filename = this.joinPath(this.fileMeta.fileName(), fi.name());
const meta = (0, filemeta_1.newFileMeta)(filename, async () => {
return await this.fs.open(filename);
});
meta.merge(this.fileMeta);
const fim = (0, fileinfo_1.newFileInfo)(fi, filename);
fim.setMeta(meta);
result.push(fim);
}
}
return result;
}
/**
* Get the file metadata
*/
meta() {
return this.fileMeta;
}
/**
* Simple path join implementation
*/
joinPath(dir, name) {
if (dir.endsWith('/')) {
return dir + name;
}
return dir + '/' + name;
}
// Delegate File interface methods to the underlying file
async read(buffer) {
if (this.file === null) {
throw new Error('File is null');
}
return await this.file.read(buffer);
}
async readAt(buffer, offset) {
if (this.file === null) {
throw new Error('File is null');
}
return await this.file.readAt(buffer, offset);
}
async seek(offset, whence) {
if (this.file === null) {
throw new Error('File is null');
}
return await this.file.seek(offset, whence);
}
async write(buffer) {
if (this.file === null) {
throw new Error('File is null');
}
return await this.file.write(buffer);
}
async writeAt(buffer, offset) {
if (this.file === null) {
throw new Error('File is null');
}
return await this.file.writeAt(buffer, offset);
}
name() {
return path.basename(this.fileMeta.fileName());
}
async readdir(count) {
if (this.file === null) {
throw new Error('File is null');
}
return await this.readDir(count);
}
async readdirnames(n) {
if (this.file === null) {
throw new Error('File is null');
}
return await this.file.readdirnames(n);
}
async stat() {
if (this.file === null) {
throw new Error('File is null');
}
return await this.file.stat();
}
async sync() {
if (this.file === null) {
return;
}
return await this.file.sync();
}
async truncate(size) {
if (this.file === null) {
throw new Error('File is null');
}
return await this.file.truncate(size);
}
async writeString(s) {
if (this.file === null) {
throw new Error('File is null');
}
return await this.file.writeString(s);
}
}
exports.File = File;
/**
* Creates a new File instance
*/
function newFile(file, filename, fs) {
const meta = (0, filemeta_1.newFileMeta)(filename);
return new File(file, meta, fs);
}
/**
* Creates a new File with existing FileMeta
*/
function newFileWithMeta(file, meta, fs) {
return new File(file, meta, fs);
}
function newDirFileWithMeta(file, meta, fs) {
return new File(file, meta, fs, true);
}
/**
* Creates a new directory File
*/
function newDirFile(file, filename, fs) {
const meta = (0, filemeta_1.newFileMeta)(filename);
return new File(file, meta, fs, true);
}
//# sourceMappingURL=file.js.map