UNPKG

rgen-cli

Version:

A developer CLI for initializing React projects, managing utilities, and scaffolding components, hooks, pages, layouts, routes, and contexts quickly.

43 lines (41 loc) 1.77 kB
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 Layout extends Build { constructor(cmd, name, flags) { super(cmd, name, 'layouts', flags); } async setup() { try { await this.init(); const layoutPath = path.join(this.baseDir, `${this.uname}Layout.${this.typescript ? 'tsx' : 'jsx'}`); if (fs.existsSync(layoutPath)) { this.cmd.error(`${chalk.blue('[X]')} Already exists! - ${chalk.blue(layoutPath)}`); } const layouttemplate = `import { cn } from '@/libs/utils'${this.typescript ? `\nimport type { ReactNode } from 'react'\n\ninterface ${this.uname}LayoutProps { children: ReactNode className?: string }` : ''} export function ${this.uname}Layout({ children, className }${this.typescript ? `: ${this.uname}LayoutProps` : ''})${this.typescript ? ': React.JSX.Element' : ''} { return <div className={cn('p-6 bg-white rounded-xl shadow-lg', className)}>{children}</div> }\n`; if (this.flags?.desc) { const t = await generateComponent(layouttemplate, this.type, this.flags.desc, this.geminiApiKey, this.typescript, this.defaults.model); fs.writeFileSync(layoutPath, t); } else { fs.writeFileSync(layoutPath, layouttemplate); } this.cmd.log(`${chalk.blue('[+]')} Creating new ${this.uname}Layout - ${chalk.blue(layoutPath)}`); } catch (error) { if (error instanceof Error) { this.cmd.error(error); } } } }