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

88 lines (87 loc) 3.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chalk = require("chalk"); const fs = require("fs-extra"); const path = require("path"); const inquirer = require("inquirer"); const updateParentIndex_1 = require("./Helpers/updateParentIndex"); const capitalizeFirstLetter_1 = require("./Helpers/capitalizeFirstLetter"); const openInEditor_1 = require("./Helpers/openInEditor"); const getFiles_1 = require("./Helpers/getFiles"); const getBaseDir_1 = require("./Helpers/getBaseDir"); async function init() { console.log(chalk.blue('🛠 Components generator')); let customDir; let needStoryBook; const { type } = await inquirer.prompt({ type: 'list', name: 'type', message: 'What need to create?', choices: ['Component', 'Block', 'UI', 'Type', 'Hook', 'Other'], }); if (type === 'Component' || type === 'Block' || type === 'UI') { const { need } = await inquirer.prompt({ type: 'input', name: 'need', message: `Need storybook? (Y/N):`, validate: (input) => { if (input === '') return 'false'; return true; }, }); needStoryBook = need === "Y" || need === "y" || need === "yes" || need === "Yes"; } if (type === 'Other') { const { dir } = await inquirer.prompt({ type: 'input', name: 'dir', message: `Write specific dir:`, validate: (input) => { if (!input.trim()) return 'Dir can`t be empty!'; return true; }, }); customDir = dir; } const { name } = await inquirer.prompt({ type: 'input', name: 'name', message: `Write title of ${type === 'Component' ? 'component' : type === 'Block' ? 'block' : type === 'UI' ? 'UI-element' : type === 'Type' ? 'type' : type === 'Hook' ? 'hook' : 'file'}`, validate: (input) => { if (!input.trim()) return 'Title can`t be empty!'; return true; }, filter: (input) => type === 'Hook' && input.startsWith('use') ? input : (0, capitalizeFirstLetter_1.capitalizeFirstLetter)(input), }); const baseDir = (0, getBaseDir_1.getBaseDir)(type, customDir); const componentDir = path.join(baseDir, type === 'Type' ? name.replace(/(Types?)$/, '') : type === 'Hook' ? `${name.startsWith('use') ? '' : 'use'}${name}` : name); try { await fs.ensureDir(componentDir); const files = (0, getFiles_1.getFiles)(type, name, needStoryBook); const createdFiles = []; for (const file of files) { const filePath = path.join(componentDir, file.name); await fs.writeFile(filePath, file.content); createdFiles.push(filePath); console.log(chalk.green(`✅ File ${file.name} was created!`)); } const filesToOpen = createdFiles.filter(file => !file.endsWith('index.ts')); (0, openInEditor_1.openInEditor)(filesToOpen); console.log(chalk.bold(`🎉 ${type} "${name}" was generated!`)); } catch (error) { console.error(chalk.red('❌ Error of created:'), error); } await (0, updateParentIndex_1.updateParentIndex)(baseDir, type, name); } init();