UNPKG

@mdfriday/foundry

Version:

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

159 lines 6.22 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.Publisher = void 0; const path = __importStar(require("path")); const log_1 = require("../../../../pkg/log"); const log = (0, log_1.getDomainLogger)('resources', { component: 'publisher' }); /** * Publisher for Resources domain * TypeScript equivalent of golang/internal/domain/resources/entity/publisher.go */ class Publisher { constructor(pubFs, urlSvc) { this.pubFs = pubFs; this.urlSvc = urlSvc; } /** * PublishContentToTarget publishes string content to target path * TypeScript equivalent of PublishContentToTarget method from Go */ // async publishContentToTarget(content: string, target: string): Promise<void> { // const f = await this.openPublishFileForWriting(target); // try { // const contentBytes = new TextEncoder().encode(content); // await f.write(contentBytes); // } finally { // await f.close(); // } // } /** * OpenPublishFileForWriting opens a file for writing in the publish directory * TypeScript equivalent of OpenPublishFileForWriting method from Go */ async openPublishFileForWriting(filename) { const cleanFilename = filename.replace(/^\/+/, ''); // Remove leading slashes try { const file = await this.pubFs.create(cleanFilename); return new FileWritable(file); } catch (error) { // If creation failed, try creating the directory first if (error.code === 'ENOENT' || error.message.includes('ENOENT')) { const dir = path.dirname(cleanFilename); await this.pubFs.mkdirAll(dir, 0o777); // 0o777 before umask const file = await this.pubFs.create(cleanFilename); // Wrap the file to add logging to its close method const originalClose = file.close.bind(file); file.close = async () => { try { await originalClose(); } catch (error) { log.errorf("❌ [Publisher.File.close] Error closing publish file (retry) %s, $s", cleanFilename, error); throw error; } }; return new FileWritable(file); } throw error; } } /** * Helper method to open files for writing using a MultiWriter approach * TypeScript equivalent of helpers.OpenFilesForWriting function from Go */ async openFilesForWriting(...filenames) { if (filenames.length === 0) { throw new Error('No filenames provided'); } // For now, we'll just use the first filename (similar to the simple case in Go) // In the future, we might need to implement a MultiWriter for multiple files const filename = filenames[0]; return await this.openFileForWriting(filename); } /** * 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 openFileForWriting(filename) { const cleanFilename = path.normalize(filename); try { // Create will truncate if file already exists return await this.pubFs.create(cleanFilename); } catch (error) { // If the error is not "file not found", re-throw it if (!this.isFileNotFoundError(error)) { throw error; } // Create the directory and try again const dir = path.dirname(cleanFilename); await this.pubFs.mkdirAll(dir, 0o777); // 0o777 before umask return await this.pubFs.create(cleanFilename); } } /** * Helper to check if error is a file not found error */ isFileNotFoundError(error) { return error && (error.code === 'ENOENT' || error.message?.includes('no such file or directory') || error.message?.includes('not found')); } } exports.Publisher = Publisher; const stream_1 = require("stream"); class FileWritable extends stream_1.Writable { constructor(file) { super(); this.file = file; } _write(chunk, _encoding, callback) { // Wrap the File.write() promise-style API into callback-style this.file.write(chunk) .then(() => callback()) .catch(callback); } _final(callback) { // Optionally call file.sync() here if needed this.file.sync?.() .then(() => this.file.close()) .then(() => callback()) .catch(callback); } } //# sourceMappingURL=publisher.js.map