UNPKG

modern-dev-cli

Version:

A modern, comprehensive CLI toolkit for streamlining development workflows with Git operations, project scaffolding, and file management

55 lines (44 loc) 1.59 kB
import fs from 'fs-extra'; import path from 'path'; import { logger } from '../utils/logger.js'; export async function bulkRename(directory, searchPattern, replacePattern) { try { const files = await fs.readdir(directory); for (const file of files) { const oldPath = path.join(directory, file); const newName = file.replace(searchPattern, replacePattern); const newPath = path.join(directory, newName); if (file !== newName) { await fs.rename(oldPath, newPath); logger.info(`Renamed: ${file} -> ${newName}`); } } logger.success('Bulk rename completed'); } catch (error) { logger.error('Failed to rename files:', error); throw error; } } export async function searchAndReplace(directory, searchText, replaceText, filePattern = '*') { try { const files = await fs.readdir(directory); for (const file of files) { if (file.match(filePattern)) { const filePath = path.join(directory, file); const stats = await fs.stat(filePath); if (stats.isFile()) { let content = await fs.readFile(filePath, 'utf8'); const newContent = content.replace(new RegExp(searchText, 'g'), replaceText); if (content !== newContent) { await fs.writeFile(filePath, newContent); logger.info(`Updated: ${file}`); } } } } logger.success('Search and replace completed'); } catch (error) { logger.error('Failed to search and replace:', error); throw error; } }