rgen-cli
Version:
A developer CLI for initializing React projects, managing utilities, and scaffolding components, hooks, pages, layouts, routes, and contexts quickly.
38 lines (37 loc) • 1.66 kB
JavaScript
import chalk from 'chalk';
import fs from 'node:fs';
import path from 'node:path';
import { generateComponent } from './ai/gemini.js';
import Build from './build.js';
export default class Component extends Build {
constructor(cmd, name, flags) {
super(cmd, name, 'components', flags);
}
async setup() {
try {
await this.init();
const componentPath = path.join(this.baseDir, `${this.uname}.${this.typescript ? 'tsx' : 'jsx'}`);
if (fs.existsSync(componentPath)) {
this.cmd.error(`${chalk.blue('[X]')} Already exists! - ${chalk.blue(componentPath)}`);
}
const componentTemplate = `import { cn } from '@/libs/utils'
${this.typescript ? `import { type ComponentProps } from 'react'\n\ntype Props = ComponentProps<'div'>\n` : ''}
export function ${this.uname}({ className, ...props }${this.typescript ? ': Props' : ''})${this.typescript ? ': React.JSX.Element' : ''} {
return <div className={cn(className)} {...props} />
}\n`;
if (this.flags?.desc) {
const t = await generateComponent(componentTemplate, this.type, this.flags.desc, this.geminiApiKey, this.typescript, this.defaults.model);
fs.writeFileSync(componentPath, t);
}
else {
fs.writeFileSync(componentPath, componentTemplate);
}
this.cmd.log(`${chalk.blue('[+]')} Creating new component ${this.uname} - ${chalk.blue(componentPath)}`);
}
catch (error) {
if (error instanceof Error) {
this.cmd.error(error);
}
}
}
}