UNPKG

bowling-analysis-system

Version:

A comprehensive system for analyzing bowling techniques using video processing and metrics calculation

65 lines (57 loc) 2 kB
/** * File Writer Processor * * Writes data to a file * @module core/processors/FileWriter */ const fs = require('fs').promises; const path = require('path'); const BaseProcessor = require('../BaseProcessor'); /** * @class FileWriter * @description Processor that writes data to a file * @extends BaseProcessor */ class FileWriter extends BaseProcessor { /** * Create a new FileWriter processor * @param {Object} config - Processor configuration * @param {string} config.filePath - Path to the file to write * @param {string} [config.encoding='utf8'] - File encoding * @param {boolean} [config.createDirectories=true] - Whether to create directories if they don't exist */ constructor(config = {}) { super('fileWriter', config); this.filePath = config.filePath; this.encoding = config.encoding || 'utf8'; this.createDirectories = config.createDirectories !== false; } /** * Process input data * @param {*} input - Input data to write * @param {Object} context - Processing context * @returns {Promise<string>} Path to the written file */ async process(input, context) { const filePath = this.filePath || context?.pipeline?.options?.outputPath; if (!filePath) { throw new Error('No file path specified'); } try { // Resolve path (support relative paths) const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(process.cwd(), filePath); // Create directories if needed if (this.createDirectories) { const directory = path.dirname(resolvedPath); await fs.mkdir(directory, { recursive: true }); } // Write file contents await fs.writeFile(resolvedPath, input, this.encoding); return resolvedPath; } catch (error) { throw new Error(`Failed to write file: ${error.message}`); } } } module.exports = { FileWriter };