UNPKG

blockiesui

Version:

A UI library for Blockies

180 lines (179 loc) 10.3 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { existsSync, writeFileSync, mkdirSync, readFileSync } from 'fs'; import path from 'path'; import ora from 'ora'; import chalk from 'chalk'; import { componentsRegistry } from '../utils/components-registry'; import { logger } from '../utils/logger'; import { getPackageManager } from './init'; import { installDependencies } from './init'; // Function to update Tailwind config with keyframes and animations function updateTailwindConfig(component) { if (!component.keyframes && !component.animation) return; try { // Find the tailwind config file var possibleConfigFiles = [ 'tailwind.config.js', 'tailwind.config.ts', 'tailwind.config.cjs', 'tailwind.config.mjs' ]; var configPath = void 0; for (var _i = 0, possibleConfigFiles_1 = possibleConfigFiles; _i < possibleConfigFiles_1.length; _i++) { var file = possibleConfigFiles_1[_i]; var filePath = path.join(process.cwd(), file); if (existsSync(filePath)) { configPath = filePath; break; } } if (!configPath) { logger.warning(chalk.yellow('Could not find Tailwind config file. Keyframes and animations not added.')); return; } // Read the config file var configContent = readFileSync(configPath, 'utf8'); // Ensure theme.extend exists if (!configContent.includes('theme: {')) { configContent = configContent.replace(/module.exports\s*=\s*{/, 'module.exports = {\n theme: {\n extend: {},\n },'); } if (!configContent.includes('extend: {')) { configContent = configContent.replace(/theme:\s*{/, 'theme: {\n extend: {'); } // Update keyframes if they exist in the component if (component.keyframes) { for (var _a = 0, _b = Object.entries(component.keyframes); _a < _b.length; _a++) { var _c = _b[_a], keyframeName = _c[0], keyframeValue = _c[1]; // Check if keyframe already exists to avoid duplicates if (!configContent.includes("\"".concat(keyframeName, "\": {"))) { var keyframesRegex = /keyframes:\s*{/; if (keyframesRegex.test(configContent)) { // Replace keyframes object opening with itself plus the new keyframes configContent = configContent.replace(keyframesRegex, "keyframes: {\n \"".concat(keyframeName, "\": ").concat(JSON.stringify(keyframeValue, null, 8).replace(/"([^"]+)":/g, '$1:'), ",")); } else { // Keyframes object doesn't exist yet, need to create it var themeExtendRegex = /theme:\s*{\s*extend:\s*{/; if (themeExtendRegex.test(configContent)) { configContent = configContent.replace(themeExtendRegex, "theme: {\n extend: {\n keyframes: {\n \"".concat(keyframeName, "\": ").concat(JSON.stringify(keyframeValue, null, 8).replace(/"([^"]+)":/g, '$1:'), ",\n },")); } } } } } // Update animations if they exist in the component if (component.animation) { for (var _d = 0, _e = Object.entries(component.animation); _d < _e.length; _d++) { var _f = _e[_d], animationName = _f[0], animationValue = _f[1]; // Check if animation already exists to avoid duplicates if (!configContent.includes("\"".concat(animationName, "\": \"").concat(animationValue, "\""))) { var animationsRegex = /animation:\s*{/; if (animationsRegex.test(configContent)) { // Replace animations object opening with itself plus the new animation configContent = configContent.replace(animationsRegex, "animation: {\n \"".concat(animationName, "\": \"").concat(animationValue, "\",")); } else { // Animations object doesn't exist yet, need to create it var themeExtendRegex = /theme:\s*{\s*extend:\s*{/; if (themeExtendRegex.test(configContent)) { configContent = configContent.replace(themeExtendRegex, "theme: {\n extend: {\n animation: {\n \"".concat(animationName, "\": \"").concat(animationValue, "\",\n },")); } } } } } // Write updated config back to file writeFileSync(configPath, configContent); logger.info(chalk.green("\u2728 Updated Tailwind config with required keyframes and animations")); } catch (error) { logger.warning(chalk.yellow("Failed to update Tailwind config: ".concat(error instanceof Error ? error.message : String(error)))); } } export function add(componentName) { return __awaiter(this, void 0, void 0, function () { var spinner, component, uiDir, componentPath, packageManager; return __generator(this, function (_a) { spinner = ora({ text: "Adding ".concat(componentName, " component..."), color: 'yellow' }).start(); try { component = componentsRegistry[componentName.toLowerCase()]; if (!component) { spinner.fail(chalk.red("Component \"".concat(componentName, "\" not found in registry"))); return [2 /*return*/]; } uiDir = path.join(process.cwd(), 'src', 'components', 'ui'); if (!existsSync(uiDir)) { mkdirSync(uiDir, { recursive: true }); } componentPath = path.join(uiDir, "".concat(component.name, ".tsx")); if (existsSync(componentPath)) { spinner.info(chalk.yellow("Component ".concat(component.name, " already exists. Overwriting..."))); } writeFileSync(componentPath, component.template); // Update Tailwind config with keyframes and animations if needed if (component.keyframes || component.animation) { spinner.text = 'Updating Tailwind config...'; updateTailwindConfig(component); } // Install dependencies if needed if (component.dependencies && component.dependencies.length > 0 && component.dependencies[0] !== '') { spinner.text = 'Installing component dependencies...'; packageManager = getPackageManager(); installDependencies(packageManager, component.dependencies); spinner.succeed(chalk.green("\u2728 Dependencies installed successfully!")); } spinner.succeed(chalk.green("\u2728 ".concat(component.name, " component added successfully!"))); logger.info(chalk.cyan("Component created at: src/components/ui/".concat(component.name, ".tsx"))); } catch (error) { spinner.fail(chalk.red('Failed to add component')); if (error instanceof Error) { logger.error(chalk.red(error.message)); } process.exit(1); } return [2 /*return*/]; }); }); }