|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | + |
| 4 | +import inquirer from "inquirer"; |
| 5 | +import { execSync } from "child_process"; |
| 6 | +import path from "path"; |
| 7 | +import fs from "fs"; |
| 8 | +// const { execSync } = require("child_process"); |
| 9 | +// const path = require("path"); |
| 10 | +// const fs = require("fs"); |
| 11 | +// const inquirer = require("inquirer"); |
| 12 | + |
| 13 | + |
| 14 | +const run = (cmd, cwd = process.cwd()) => { |
| 15 | + console.log(`\n📦 Running: ${cmd}`); |
| 16 | + execSync(cmd, { stdio: "inherit", cwd }); |
| 17 | +}; |
| 18 | + |
| 19 | +(async () => { |
| 20 | + // 1. Ask project name |
| 21 | + const { projectName } = await inquirer.prompt([ |
| 22 | + { type: "input", name: "projectName", message: "Enter project name:" } |
| 23 | + ]); |
| 24 | + |
| 25 | + // 2. Ask for CSS framework |
| 26 | + const { cssFramework } = await inquirer.prompt([ |
| 27 | + { |
| 28 | + type: "list", |
| 29 | + name: "cssFramework", |
| 30 | + message: "Choose a CSS framework:", |
| 31 | + choices: ["Tailwind", "Bootstrap", "MUI"] |
| 32 | + } |
| 33 | + ]); |
| 34 | + |
| 35 | + // 3. Ask optional packages |
| 36 | + const { packages } = await inquirer.prompt([ |
| 37 | + { |
| 38 | + type: "checkbox", |
| 39 | + name: "packages", |
| 40 | + message: "Select optional packages:", |
| 41 | + choices: [ |
| 42 | + { name: "Axios", value: "axios" }, |
| 43 | + { name: "React Icons", value: "react-icons" }, |
| 44 | + { name: "React Hook Form", value: "react-hook-form" }, |
| 45 | + { name: "Yup", value: "yup" }, |
| 46 | + { name: "Formik", value: "formik" }, |
| 47 | + { name: "Moment.js", value: "moment" } |
| 48 | + ] |
| 49 | + } |
| 50 | + ]); |
| 51 | + |
| 52 | + // 4. Create Vite + React project |
| 53 | + run(`npm create vite@latest ${projectName} -- --template react`); |
| 54 | + const projectPath = path.join(process.cwd(), projectName); |
| 55 | + |
| 56 | + // 5. Install chosen CSS framework |
| 57 | + if (cssFramework === "Tailwind") { |
| 58 | + run(`npm install tailwindcss @tailwindcss/vite`, projectPath); |
| 59 | + |
| 60 | + const viteConfigPath = path.join(projectPath, "vite.config.js"); |
| 61 | + let viteConfig = fs.readFileSync(viteConfigPath, "utf-8"); |
| 62 | + viteConfig = `import tailwindcss from '@tailwindcss/vite'\n` + viteConfig; |
| 63 | + viteConfig = viteConfig.replace(/plugins:\s*\[/, "plugins: [\n tailwindcss(),"); |
| 64 | + fs.writeFileSync(viteConfigPath, viteConfig); |
| 65 | + |
| 66 | + fs.writeFileSync(path.join(projectPath, "src", "index.css"), `@import "tailwindcss";\n`); |
| 67 | + |
| 68 | + const mainFile = fs.existsSync(path.join(projectPath, "src/main.jsx")) |
| 69 | + ? "src/main.jsx" |
| 70 | + : "src/main.tsx"; |
| 71 | + const mainPath = path.join(projectPath, mainFile); |
| 72 | + let mainContent = fs.readFileSync(mainPath, "utf-8"); |
| 73 | + mainContent = mainContent.replace(/import\s+['"]\.\/index\.css['"];?/g, ""); // remove default CSS import |
| 74 | + if (!mainContent.includes(`import './index.css'`)) { |
| 75 | + mainContent = `import './index.css';\n` + mainContent; |
| 76 | + } |
| 77 | + fs.writeFileSync(mainPath, mainContent); |
| 78 | + } else if (cssFramework === "Bootstrap") { |
| 79 | + run(`npm install bootstrap`, projectPath); |
| 80 | + const mainFile = fs.existsSync(path.join(projectPath, "src/main.jsx")) |
| 81 | + ? "src/main.jsx" |
| 82 | + : "src/main.tsx"; |
| 83 | + const mainPath = path.join(projectPath, mainFile); |
| 84 | + let mainContent = fs.readFileSync(mainPath, "utf-8"); |
| 85 | + mainContent = mainContent |
| 86 | + .replace(/import\s+['"]\.\/index\.css['"];?/g, "") // remove vite css import |
| 87 | + .replace(/import\s+['"]\.\/App\.css['"];?/g, ""); // remove App.css import |
| 88 | + mainContent = `import 'bootstrap/dist/css/bootstrap.min.css';\n` + mainContent; |
| 89 | + fs.writeFileSync(mainPath, mainContent); |
| 90 | + } else if (cssFramework === "MUI") { |
| 91 | + run(`npm install @mui/material @emotion/react @emotion/styled`, projectPath); |
| 92 | + const mainFile = fs.existsSync(path.join(projectPath, "src/main.jsx")) |
| 93 | + ? "src/main.jsx" |
| 94 | + : "src/main.tsx"; |
| 95 | + const mainPath = path.join(projectPath, mainFile); |
| 96 | + let mainContent = fs.readFileSync(mainPath, "utf-8"); |
| 97 | + mainContent = mainContent |
| 98 | + .replace(/import\s+['"]\.\/index\.css['"];?/g, "") |
| 99 | + .replace(/import\s+['"]\.\/App\.css['"];?/g, ""); |
| 100 | + fs.writeFileSync(mainPath, mainContent); |
| 101 | + } |
| 102 | + |
| 103 | + // 6. Install optional packages |
| 104 | + if (packages.length > 0) { |
| 105 | + run(`npm install ${packages.join(" ")}`, projectPath); |
| 106 | + } |
| 107 | + |
| 108 | + // 7. Create folder structure |
| 109 | + const folders = ["components", "pages", "hooks", "store", "utils", "assets"]; |
| 110 | + folders.forEach((folder) => { |
| 111 | + fs.mkdirSync(path.join(projectPath, "src", folder), { recursive: true }); |
| 112 | + }); |
| 113 | + |
| 114 | + // 8. Axios setup if chosen |
| 115 | + if (packages.includes("axios")) { |
| 116 | + const axiosContent = `import axios from "axios"; |
| 117 | +
|
| 118 | +export const api = axios.create({ |
| 119 | + baseURL: import.meta.env.VITE_API_URL || "http://localhost:5000", |
| 120 | + headers: { "Content-Type": "application/json" } |
| 121 | +}); |
| 122 | +`; |
| 123 | + fs.writeFileSync(path.join(projectPath, "src", "utils", "axiosInstance.js"), axiosContent); |
| 124 | + } |
| 125 | + |
| 126 | + // 9. Clean up default CSS files from Vite |
| 127 | + const appCssPath = path.join(projectPath, "src", "App.css"); |
| 128 | + if (fs.existsSync(appCssPath)) fs.unlinkSync(appCssPath); |
| 129 | + |
| 130 | + const appFile = fs.existsSync(path.join(projectPath, "src/App.jsx")) |
| 131 | + ? path.join(projectPath, "src/App.jsx") |
| 132 | + : path.join(projectPath, "src/App.tsx"); |
| 133 | + |
| 134 | + let appContent = fs.readFileSync(appFile, "utf-8"); |
| 135 | + appContent = appContent.replace(/import\s+['"]\.\/App\.css['"];?/g, ""); // remove App.css import |
| 136 | + appContent = `export default function App() { |
| 137 | + return ( |
| 138 | + <div |
| 139 | + style={{ |
| 140 | + display: "flex", |
| 141 | + flexDirection: "column", |
| 142 | + justifyContent: "center", |
| 143 | + alignItems: "center", |
| 144 | + height: "100vh", |
| 145 | + fontFamily: "sans-serif", |
| 146 | + background: "#f9fafb", |
| 147 | + color: "#111", |
| 148 | + textAlign: "center", |
| 149 | + }} |
| 150 | + > |
| 151 | + <h1 |
| 152 | + style={{ |
| 153 | + fontSize: "2.5rem", |
| 154 | + marginBottom: "0.5rem", |
| 155 | + fontWeight: 600, |
| 156 | + }} |
| 157 | + > |
| 158 | + Welcome to{" "} |
| 159 | + <span style={{ color: "#2563eb" }}>${projectName}</span> 🚀 |
| 160 | + </h1> |
| 161 | + <p style={{ fontSize: "1.1rem", color: "#555" }}> |
| 162 | + Your project is ready. Start building amazing things! |
| 163 | + </p> |
| 164 | + </div> |
| 165 | + ); |
| 166 | +} |
| 167 | +`; |
| 168 | + fs.writeFileSync(appFile, appContent); |
| 169 | + |
| 170 | + console.log("\n✅ Setup complete!"); |
| 171 | + console.log(`\nNext steps:\n cd ${projectName}\n npm run dev`); |
| 172 | +})(); |
0 commit comments