rgen-cli
Version:
A developer CLI for initializing React projects, managing utilities, and scaffolding components, hooks, pages, layouts, routes, and contexts quickly.
49 lines (45 loc) • 1.84 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 Hook extends Build {
constructor(cmd, name, flags) {
super(cmd, name, 'hooks', flags);
}
async setup() {
try {
await this.init();
const hookPath = path.join(this.baseDir, `use${this.uname}.${this.typescript ? 'ts' : 'js'}`);
if (fs.existsSync(hookPath)) {
this.cmd.error(`${chalk.blue('[X]')} Already exists! - ${chalk.blue(hookPath)}`);
}
const hookTemplate = `import { useState, useEffect${this.typescript ? ', type Dispatch, type SetStateAction' : ''} } from 'react'${this.typescript
? `\n\nexport type Use${this.uname}Return<T> = {
state: T | null
setState: Dispatch<SetStateAction<T | null>>
}`
: ''}
export function use${this.uname}${this.typescript ? '<T>' : ''}()${this.typescript ? `: Use${this.uname}Return<T>` : ''} {
const [state, setState] = useState${this.typescript ? '<T | null>' : ''}(null)
useEffect(() => {
// TODO: add effect logic
}, [])
return { state, setState }
}\n`;
if (this.flags?.desc) {
const t = await generateComponent(hookTemplate, this.type, this.flags.desc, this.geminiApiKey, this.typescript, this.defaults.model);
fs.writeFileSync(hookPath, t);
}
else {
fs.writeFileSync(hookPath, hookTemplate);
}
this.cmd.log(`${chalk.blue('[+]')} Creating new hook use${this.uname} - ${chalk.blue(hookPath)}`);
}
catch (error) {
if (error instanceof Error) {
this.cmd.error(error);
}
}
}
}