import fs from 'fs';
import path from 'path';
import { glob } from 'glob';
interface GeneratorOptions {
outputDir: string;
useSWRInfinite?: boolean;
graphqlInfiniteQueries?: string[];
}
export function crawlHooks(rootPath: string) {
const dir = path.join(rootPath, 'hooks');
if (!fs.existsSync(dir)) return [];
const files = glob.sync('**/*.ts', { cwd: dir });
return files.map((file: any) => {
const content = fs.readFileSync(path.join(dir, file), 'utf-8');
const match = content.match(/type\s+(\w+)Props\s*=\s*{([^}]*)}/);
const props: Record<string, string> = {};
if (match) {
match[2].split('\n').forEach((line: string) => {
const [key, val] = line.split(':').map((s) => s.trim());
if (key && val) props[key] = val;
});
}
return { name: path.basename(file, '.tsx'), props, path: file };
});
}