easy-component-generator
Version:
    • 2.01 kB
JavaScript
;
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;
}
}