// Utilities for FigmaMind
const fs = require('fs-extra');
const path = require('path');
// Create necessary directories
const LOGS_DIR = path.join(__dirname, 'logs');
const OUTPUT_DIR = path.join(__dirname, 'examples', 'output');
const ASSETS_DIR = path.join(OUTPUT_DIR, 'assets');
// Ensure directories exist
try {
fs.ensureDirSync(LOGS_DIR);
fs.ensureDirSync(OUTPUT_DIR);
fs.ensureDirSync(ASSETS_DIR);
} catch (error) {
console.error('Error creating directories:', error);
}
/**
* Log function that writes to file
* @param {string} message - Message to log
* @param {string} level - Log level (info, debug, error, warn)
*/
function log(message, level = 'info') {
try {
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] ${level.toUpperCase()}: ${message}\n`;
// Write to specific log file based on level
const logFile = level === 'error' ? 'error.log' : 'server.log';
fs.appendFileSync(path.join(LOGS_DIR, logFile), logMessage);
} catch (e) {
// If we can't log to file, at least try console (except in production)
if (process.env.NODE_ENV !== 'production') {
console.error('Error writing to log file:', e.message);
}
}
}
/**
* Extract file key from Figma URL
* @param {string} url - Figma URL
* @returns {string} Figma file key
*/
function extractFigmaKey(url) {
// Handle various Figma URL formats
const regexPatterns = [
/figma.com\/file\/([A-Za-z0-9]+)\//, // Standard URL
/figma.com\/proto\/([A-Za-z0-9]+)\//, // Prototype URL
/figma.com\/([A-Za-z0-9]+)\//, // Short URL
/^([A-Za-z0-9]+)$/ // Just the key
];
for (const pattern of regexPatterns) {
const match = url.match(pattern);
if (match && match[1]) {
return match[1];
}
}
throw new Error('Invalid Figma URL format');
}
module.exports = {
log,
extractFigmaKey,
paths: {
LOGS_DIR,
OUTPUT_DIR,
ASSETS_DIR
}
};