UNPKG

easy-component-generator

Version:

![npm](https://img.shields.io/npm/v/easy-component-generator) ![license](https://img.shields.io/npm/l/easy-component-generator) ![downloads](https://img.shields.io/npm/dt/easy-component-generator) ![size](https://img.shields.io/bundlephobia/min/easy-compo

40 lines (39 loc) 2.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateParentIndex = updateParentIndex; const path = require("path"); const chalk = require("chalk"); const fs = require("fs-extra"); async function updateParentIndex(parentDir, type, componentName) { const indexPath = path.join(parentDir, 'index.ts'); try { // Создаем директорию и файл, если они не существуют await fs.ensureDir(parentDir); if (!await fs.pathExists(indexPath)) { await fs.writeFile(indexPath, ''); } // Читаем текущее содержимое файла let currentContent = await fs.readFile(indexPath, 'utf8'); // Генерируем новую строку экспорта const newExport = type === 'Type' ? `export type { T${componentName.replace(/(Types?)$/, '')} } from './${componentName}';` : type === 'Hook' ? `export { ${componentName.startsWith('use') ? '' : 'use'}${componentName} } from './${componentName.startsWith('use') ? '' : 'use'}${componentName}';` : `export { ${componentName} } from './${componentName}';`; // Проверяем, существует ли уже такой экспорт if (currentContent.includes(newExport)) { console.log(chalk.yellow(`⚠️ Export for ${componentName} already exists in index.ts`)); return; } // Добавляем новый экспорт в конец файла const updatedContent = currentContent.trim() ? `${currentContent.trim()}\n${newExport}\n` : `${newExport}\n`; await fs.writeFile(indexPath, updatedContent); console.log(chalk.green(`✅ Export for ${componentName} added to parent index.ts!`)); } catch (error) { console.error(chalk.red(`❌ Error updating parent index.ts: ${error}`)); throw error; } }