UNPKG

@androbinco/prompts-cli

Version:

Robin CLI for importing generative AI prompts

42 lines (36 loc) 1.44 kB
/* eslint-disable no-console */ import path from 'node:path'; import fs from 'node:fs/promises'; /** * Ensures that a directory exists. If it doesn't, it's created. * The path is resolved relative to the current working directory. * @param {string} relativeDirName - The name or relative path of the directory. * @returns {Promise<string>} A promise that resolves with the absolute path to the directory. * @throws {Error} If directory creation fails. */ export async function ensureDirectoryExists(relativeDirName) { const absoluteDirPath = path.resolve(process.cwd(), relativeDirName); try { await fs.mkdir(absoluteDirPath, { recursive: true }); return absoluteDirPath; } catch (error) { console.error(`Error creating or ensuring directory ${absoluteDirPath}:`, error.message); throw error; } } /** * Writes content to a specified file. * If the file exists, it will be overwritten. * @param {string} absoluteFilePath - The absolute path to the file. * @param {string} content - The content to write to the file. * @returns {Promise<void>} A promise that resolves when the file has been written. * @throws {Error} If writing the file fails. */ export async function writeFileContent(absoluteFilePath, content) { try { await fs.writeFile(absoluteFilePath, content, 'utf8'); } catch (error) { console.error(`Error writing file ${absoluteFilePath}:`, error.message); throw error; } }