Skip to content

Commit 0e8a572

Browse files
committed
Add createViteProject function to initialize Vite React project with Tailwind CSS
1 parent 80d5ba8 commit 0e8a572

File tree

1 file changed

+141
-1
lines changed

1 file changed

+141
-1
lines changed

commands/createFrontend.js

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,146 @@ ${chalk.yellow('npx shadcn@latest add <component-name>')}
235235
);
236236
}
237237

238+
function createViteProject(projectName) {
239+
checkNodeVersion();
240+
const rootDir = path.join(process.cwd(), projectName);
241+
242+
// Step 1: Create Vite Project
243+
console.log('📦 Creating Vite React project...');
244+
try {
245+
execSync(`npm create vite@latest ${projectName} -- --template react-swc`, {
246+
stdio: 'inherit',
247+
});
248+
console.log('✅ Vite project created successfully.');
249+
} catch (error) {
250+
console.error(`❌ Failed to create Vite project: ${error.message}`);
251+
process.exit(1);
252+
}
253+
254+
// Change to project directory
255+
process.chdir(rootDir);
256+
257+
// Step 2: Install dependencies
258+
console.log('📦 Installing dependencies...');
259+
try {
260+
execSync('npm install', { stdio: 'inherit' });
261+
console.log('✅ Dependencies installed successfully.');
262+
} catch (error) {
263+
console.error(`❌ Failed to install dependencies: ${error.message}`);
264+
process.exit(1);
265+
}
266+
267+
// Step 3: Install Tailwind CSS
268+
console.log('📦 Installing Tailwind CSS...');
269+
try {
270+
execSync('npm install tailwindcss @tailwindcss/vite', { stdio: 'inherit' });
271+
console.log('✅ Tailwind CSS installed successfully.');
272+
} catch (error) {
273+
console.error(`❌ Failed to install Tailwind CSS: ${error.message}`);
274+
process.exit(1);
275+
}
276+
277+
// Step 4: Update index.css
278+
const indexCss = `@import "tailwindcss";`;
279+
try {
280+
fs.writeFileSync(path.join(rootDir, 'src', 'index.css'), indexCss);
281+
console.log('✅ index.css updated.');
282+
} catch (error) {
283+
console.error(`❌ Failed to update index.css: ${error.message}`);
284+
process.exit(1);
285+
}
286+
287+
// Step 5: Update vite.config.js
288+
const viteConfig = `import { defineConfig } from 'vite'
289+
import react from '@vitejs/plugin-react-swc'
290+
import tailwindcss from '@tailwindcss/vite'
291+
292+
// https://vite.dev/config/
293+
export default defineConfig({
294+
plugins: [react(), tailwindcss()],
295+
})`;
296+
297+
try {
298+
fs.writeFileSync(path.join(rootDir, 'vite.config.js'), viteConfig);
299+
console.log('✅ vite.config.js updated.');
300+
} catch (error) {
301+
console.error(`❌ Failed to update vite.config.js: ${error.message}`);
302+
process.exit(1);
303+
}
304+
305+
// Step 6: Create jsconfig.json
306+
const jsConfig = {
307+
compilerOptions: {
308+
baseUrl: '.',
309+
paths: {
310+
'@/*': ['./src/*'],
311+
},
312+
},
313+
};
314+
315+
try {
316+
fs.writeFileSync(
317+
path.join(rootDir, 'jsconfig.json'),
318+
JSON.stringify(jsConfig, null, 2)
319+
);
320+
console.log('✅ jsconfig.json created.');
321+
} catch (error) {
322+
console.error(`❌ Failed to create jsconfig.json: ${error.message}`);
323+
process.exit(1);
324+
}
325+
326+
// Step 7: Install @types/node
327+
console.log('📦 Installing @types/node...');
328+
try {
329+
execSync('npm install -D @types/node', { stdio: 'inherit' });
330+
console.log('✅ @types/node installed successfully.');
331+
} catch (error) {
332+
console.error(`❌ Failed to install @types/node: ${error.message}`);
333+
process.exit(1);
334+
}
335+
336+
// Step 8: Update vite.config.js with path alias
337+
const updatedViteConfig = `import { defineConfig } from 'vite'
338+
import react from '@vitejs/plugin-react-swc'
339+
import tailwindcss from '@tailwindcss/vite'
340+
import path from 'path'
341+
342+
// https://vite.dev/config/
343+
export default defineConfig({
344+
plugins: [react(), tailwindcss()],
345+
resolve: {
346+
alias: {
347+
"@": path.resolve(__dirname, "./src"),
348+
},
349+
},
350+
})`;
351+
352+
try {
353+
fs.writeFileSync(path.join(rootDir, 'vite.config.js'), updatedViteConfig);
354+
console.log('✅ vite.config.js updated with path alias.');
355+
} catch (error) {
356+
console.error(`❌ Failed to update vite.config.js: ${error.message}`);
357+
process.exit(1);
358+
}
359+
360+
// Final success message
361+
console.log(
362+
`\n🎉 React project "${projectName}" with Tailwind CSS created successfully!`
363+
);
364+
365+
console.log(
366+
`${chalk.green.bold('To get started:')}
367+
${chalk.blue(`cd ${projectName}`)}
368+
${chalk.yellow('npm run dev')}
369+
370+
${chalk.magenta.bold('Available commands:')}
371+
${chalk.yellow('npm run dev')} - Start development server
372+
${chalk.yellow('npm run build')} - Build for production
373+
${chalk.yellow('npm run preview')} - Preview production build
374+
`
375+
);
376+
}
377+
238378
export default function createFrontend(program) {
239379
program
240380
.command('create-frontend <projectName>')
@@ -259,7 +399,7 @@ export default function createFrontend(program) {
259399
if (options.shadcn) {
260400
createShadcnProject(projectName);
261401
} else if (options.vite) {
262-
//createViteProject(projectName);
402+
createViteProject(projectName);
263403
}
264404
});
265405
}

0 commit comments

Comments
 (0)