blockiesui
Version:
A UI library for Blockies
225 lines (224 loc) • 14.9 kB
JavaScript
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 };
}
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import { existsSync, writeFileSync, mkdirSync, readFileSync } from 'fs';
import path from 'path';
import ora from 'ora';
import chalk from 'chalk';
import { execSync } from 'child_process';
import { generateComponentsConfig, generateUtils, generateGlobalStyles, generateTailwindConfig } from '../utils/templates';
import { logger } from '../utils/logger';
import inquirer from 'inquirer';
export function getPackageManager() {
if (existsSync('bun.lockb'))
return 'bun';
if (existsSync('pnpm-lock.yaml'))
return 'pnpm';
if (existsSync('yarn.lock'))
return 'yarn';
if (existsSync('package-lock.json'))
return 'npm';
return 'npm';
}
export function installDependencies(packageManager, dependencies) {
var commands = {
npm: "npm install ".concat(dependencies.join(' '), " --save"),
yarn: "yarn add ".concat(dependencies.join(' ')),
pnpm: "pnpm add ".concat(dependencies.join(' ')),
bun: "bun add ".concat(dependencies.join(' '))
};
var command = commands[packageManager];
var spinner = ora({
text: "Installing dependencies with ".concat(packageManager, "..."),
color: 'yellow'
}).start();
try {
// Verificar si el package manager está instalado
execSync("".concat(packageManager, " --version"), { stdio: 'ignore' });
// Instalar dependencias
execSync(command, { stdio: 'ignore' });
spinner.succeed('Dependencies installed successfully!');
}
catch (error) {
spinner.fail('Failed to install dependencies');
if (error instanceof Error && error.message.includes('not found')) {
throw new Error("".concat(packageManager, " is not installed in your system. Please install it first."));
}
throw new Error("Error installing dependencies using ".concat(packageManager, ". ").concat(error instanceof Error ? error.message : 'Unknown error'));
}
}
export function init() {
return __awaiter(this, void 0, void 0, function () {
var spinner, tailwindVersion, packageManager, dependencies, srcDir, libDir, stylesDir, componentsDir, uiDir, globalsPath, newGlobalStyles, currentContent, lines, importsAndTailwind_1, newLines, newImportsAndTailwind, contentWithoutImportsAndTailwind, newContentWithoutImportsAndTailwind, updatedContent, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// Make a gradient
console.log(chalk.hex('#FF8C00')("\n\n\n\n\u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2557 \u2588\u2588\u2557\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2557 \u2588\u2588\u2557\u2588\u2588\u2557\n\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2551 \u2588\u2588\u2554\u2550\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255D\u2588\u2588\u2551 \u2588\u2588\u2554\u255D\u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255D\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255D \u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2551\n\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255D\u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2551 \u2588\u2588\u2588\u2588\u2588\u2554\u255D \u2588\u2588\u2551\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2551\n\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2551 \u2588\u2588\u2554\u2550\u2588\u2588\u2557 \u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u255D \u255A\u2550\u2550\u2550\u2550\u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2551\n\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255D\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u255A\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255D\u255A\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2551 \u2588\u2588\u2557\u2588\u2588\u2551\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2551 \u255A\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255D\u2588\u2588\u2551\n\u255A\u2550\u2550\u2550\u2550\u2550\u255D \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u255D \u255A\u2550\u2550\u2550\u2550\u2550\u255D \u255A\u2550\u2550\u2550\u2550\u2550\u255D\u255A\u2550\u255D \u255A\u2550\u255D\u255A\u2550\u255D\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u255D\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u255D \u255A\u2550\u2550\u2550\u2550\u2550\u255D \u255A\u2550\u255D\n\n\n\n"));
spinner = ora({
text: chalk.hex('#FF00')('🚀 Initializing UI Library...'),
color: 'yellow'
}).stop();
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, inquirer.prompt([
{
type: 'list',
name: 'tailwindVersion',
message: 'Which version of Tailwind CSS are you using?',
choices: [
{ name: 'Tailwind CSS v3', value: 'v3' },
{ name: 'Tailwind CSS v4', value: 'v4' }
],
default: 'v4'
}
])];
case 2:
tailwindVersion = (_a.sent()).tailwindVersion;
// Restart spinner after selection
spinner.start();
spinner.info(chalk.blue("\uD83D\uDCE6 Using Tailwind CSS ".concat(tailwindVersion)));
packageManager = getPackageManager();
spinner.info(chalk.blue("\uD83D\uDCE6 You are using ".concat(packageManager, " as package manager")));
dependencies = [
'class-variance-authority@^0.7.1',
'clsx@^2.1.1',
'lucide-react@^0.485.0',
'tailwind-merge@^3.0.2',
'tw-animate-css@^1.2.5'
];
// Add tailwindcss-animate for v3
if (tailwindVersion === 'v3') {
dependencies.push('tailwindcss-animate@^1.0.7');
}
spinner.text = "\n\n\n\nInstalling dependencies...\n\n\n\n";
installDependencies(packageManager, dependencies);
srcDir = path.join(process.cwd(), './src');
libDir = path.join(srcDir, './lib');
stylesDir = path.join(srcDir, './styles');
componentsDir = path.join(srcDir, './components');
uiDir = path.join(componentsDir, './ui');
// Create directories
[srcDir, libDir, stylesDir, componentsDir, uiDir].forEach(function (dir) {
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
});
// Generate configuration files
spinner.text = 'Creating configuration files...';
// Create components.json in root
writeFileSync(path.join(process.cwd(), 'components.json'), generateComponentsConfig());
logger.info(chalk.green('✓ Created: components.json'));
// Create utils.ts
writeFileSync(path.join(libDir, 'utils.ts'), generateUtils());
logger.info(chalk.green('✓ Created: src/lib/utils.ts'));
globalsPath = path.join(stylesDir, 'globals.css');
newGlobalStyles = generateGlobalStyles(tailwindVersion);
if (existsSync(globalsPath)) {
currentContent = readFileSync(globalsPath, 'utf8');
lines = currentContent.split('\n');
importsAndTailwind_1 = new Set(lines.filter(function (line) {
var trimmed = line.trim();
return trimmed.startsWith('@import') || trimmed.startsWith('@tailwind');
}));
newLines = newGlobalStyles.split('\n');
newImportsAndTailwind = newLines.filter(function (line) {
var trimmed = line.trim();
return trimmed.startsWith('@import') || trimmed.startsWith('@tailwind');
});
// Combinar sin duplicados
newImportsAndTailwind.forEach(function (imp) { return importsAndTailwind_1.add(imp); });
contentWithoutImportsAndTailwind = lines
.filter(function (line) {
var trimmed = line.trim();
return !trimmed.startsWith('@import') && !trimmed.startsWith('@tailwind');
})
.join('\n')
.trim();
newContentWithoutImportsAndTailwind = newLines
.filter(function (line) {
var trimmed = line.trim();
return !trimmed.startsWith('@import') && !trimmed.startsWith('@tailwind');
})
.join('\n')
.trim();
updatedContent = __spreadArray(__spreadArray([], Array.from(importsAndTailwind_1).sort(), true), [
'', // Línea en blanco después
// Resto del contenido nuevo
newContentWithoutImportsAndTailwind
], false).join('\n');
writeFileSync(globalsPath, updatedContent);
logger.info(chalk.green('✓ Updated: src/styles/globals.css'));
}
else {
writeFileSync(globalsPath, newGlobalStyles);
logger.info(chalk.green('✓ Created: src/styles/globals.css'));
}
// Create tailwind.config.ts only for v3
if (tailwindVersion === 'v3') {
writeFileSync(path.join(process.cwd(), 'tailwind.config.ts'), generateTailwindConfig(tailwindVersion));
logger.info(chalk.green('✓ Created: tailwind.config.ts'));
}
spinner.succeed(chalk.green('✨ UI Library initialized successfully!'));
logger.info(chalk.cyan('\n📝 Next steps:'));
logger.info(chalk.white('Start using the library in your project:'));
return [3 /*break*/, 4];
case 3:
error_1 = _a.sent();
spinner.fail(chalk.red('❌ Failed to initialize UI Library'));
if (error_1 instanceof Error) {
logger.error(chalk.red(error_1.message));
}
else {
logger.error(chalk.red('Un error desconocido ha ocurrido'));
}
process.exit(1);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
});
}