@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
138 lines • 4.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Publisher = void 0;
exports.openFileForWriting = openFileForWriting;
const path_1 = __importDefault(require("path"));
/**
* Publisher publishes a result file
* TypeScript equivalent of Publisher struct from Go
*/
class Publisher {
constructor(fs) {
this.fs = fs;
}
/**
* PublishSource publishes content from a source buffer to files
* TypeScript equivalent of PublishSource method from Go
*/
async publishSource(src, ...filenames) {
const files = await this.openFilesForWriting(...filenames);
try {
const content = typeof src === 'string' ? new TextEncoder().encode(src) : src;
await this.copyToWriter(files, content);
}
finally {
await files.close();
}
}
/**
* PublishFiles publishes content from a readable source to files
* TypeScript equivalent of PublishFiles method from Go
*/
async publishFiles(src, ...filenames) {
const files = await this.openFilesForWriting(...filenames);
try {
await this.copyStreamToWriter(files, src);
}
finally {
await files.close();
}
}
/**
* Helper method to open multiple files for writing using a MultiWriter approach
*/
async openFilesForWriting(...filenames) {
const files = [];
for (const filename of filenames) {
const file = await openFileForWriting(this.fs, filename);
files.push(file);
}
return new MultiWriter(files);
}
/**
* Copy Uint8Array content to writer
*/
async copyToWriter(writer, content) {
if (writer instanceof MultiWriter) {
await writer.write(content);
}
else {
await writer.write(content);
}
}
/**
* Copy ReadableStream content to writer
*/
async copyStreamToWriter(writer, stream) {
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done)
break;
if (writer instanceof MultiWriter) {
await writer.write(value);
}
else {
await writer.write(value);
}
}
}
finally {
reader.releaseLock();
}
}
}
exports.Publisher = Publisher;
/**
* MultiWriter writes to multiple files simultaneously
* TypeScript equivalent of Go's MultiWriter concept
*/
class MultiWriter {
constructor(files) {
this.files = files;
}
async write(data) {
const writePromises = this.files.map(file => file.write(data));
await Promise.all(writePromises);
}
async close() {
const closePromises = this.files.map(file => file.close());
await Promise.all(closePromises);
}
}
/**
* OpenFileForWriting opens or creates the given file. If the target directory
* does not exist, it gets created.
* TypeScript equivalent of OpenFileForWriting function from Go
*/
async function openFileForWriting(fs, filename) {
const cleanFilename = path_1.default.normalize(filename);
try {
// Create will truncate if file already exists
return await fs.create(cleanFilename);
}
catch (error) {
// If the error is not "file not found", re-throw it
if (!isFileNotFoundError(error)) {
throw error;
}
// Create the directory and try again
const dir = path_1.default.dirname(cleanFilename);
await fs.mkdirAll(dir, 0o777); // 0o777 before umask
return await fs.create(cleanFilename);
}
}
/**
* Helper function to check if an error is a "file not found" error
*/
function isFileNotFoundError(error) {
return error && (error.code === 'ENOENT' ||
error.code === 'FILE_NOT_FOUND' ||
error.message?.includes('not found') ||
error.message?.includes('no such file'));
}
//# sourceMappingURL=publisher.js.map