import { pascal } from "change-case"; import findPackageJson from "find-package-json"; import fs, { readFileSync } from "fs"; import path from "path"; const args = process.argv.slice(2); const project = getProjectPath(); const dirs = { dir: project.dir, config: "", src: (() => { const src = path.join(project.dir, "src"); const exists = fs.existsSync(src); return exists ? src : project.dir; })(), cliPath: path.resolve(__dirname, "../") }; (() => { switch (args[0]) { case "g": case "generate": switch (args[1]) { case "component": case "c": const arg: string | undefined = args[2]; if (!arg) throw new Error(); const templatePath = path.resolve( dirs.cliPath, "./static/templates/function-component.tsx" ); const parsed = path.parse(arg); const fileDir = path.resolve(dirs.src, parsed.dir); const filePath = path.join(fileDir, parsed.name + ".tsx"); const oldTemplate = readFileSync(templatePath).toString(); const newTemplate = oldTemplate.replace( /ComponentName/g, pascal(parsed.name) ); fs.mkdirSync(fileDir, { recursive: true }); fs.writeFileSync(filePath, newTemplate); break; default: throw new Error(); } break; default: throw new Error(); } })(); function getProjectPath(): path.ParsedPath { const pkgpath = findPackageJson().next().filename; if (!pkgpath) throw new Error(`Could not find pkg json using process.cwd()`); return path.parse(pkgpath); }