import path from 'node:path'; import chalk from 'chalk'; import fs from 'node:fs'; export const readPayloadFile = (params: { payloadsDirectory: string; fileName: string }) => { const { payloadsDirectory, fileName } = params; const filePath = path.join(payloadsDirectory, fileName); // Create the file if it doesn't exist if (fs.existsSync(filePath)) { const fileContent = fs.readFileSync(filePath, { encoding: 'utf8' }); try { return JSON.parse(fileContent); } catch (error: any) { throw new Error(chalk.red(`Invalid JSON in ${filePath}: ${String(error.message)}`)); } } else { fs.writeFileSync(filePath, '{}'); console.log(chalk.red(`Initialized ${filePath} with empty JSON object.`)); throw new Error(chalk.green(`Please add the product payload to ${filePath}`)); } };