-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
141 lines (117 loc) · 3.38 KB
/
commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { existsSync, writeFileSync } from "fs";
import { join, parse } from "path";
import { execCommand } from "./index.js";
import ora from "ora";
export async function createProjectDirectory(
projectName,
projectType,
options
) {
const projectPath = join(process.cwd(), projectName);
switch (projectType) {
case "react":
await createReactProject(projectName, options);
break;
}
}
async function createReactProject(projectName, options) {
const { useTypeScript, useTailwind, useFramerMotion, useReactRouter } =
options;
const spinner = ora("Creating React project 🪚").start();
try {
// Create React project using Vite
await execCommand(
`npm create vite@latest ${projectName} -- --template react${
useTypeScript ? "-ts" : ""
}`
);
spinner.succeed("React project created successfully ✅ 🏁");
process.chdir(projectName);
await execCommand("npm install");
// install additional deps based on selected options
const dependencies = [];
if (useTailwind) {
dependencies.push(
"tailwindcss@latest postcss@latest autoprefixer@latest"
);
}
if (useFramerMotion) {
dependencies.push("framer-motion");
}
if (useReactRouter) {
dependencies.push("react-router-dom");
}
if (dependencies.length > 0) {
spinner.start("Installing additional dependencies 🪚");
await execCommand(`npm install ${dependencies.join(" ")}`);
spinner.succeed("Additional dependencies installed successfully ✅ 🏁");
}
// Generate Taiwind CSS configuration files
if (useTailwind) {
spinner.start("Handling Tailwind configuration 🪚");
await execCommand("npx tailwindcss init -p");
// Update tailwind.config.js
writeFileSync(
"tailwind.config.js",
`/** @type {import('tailwindcss').Config} */
export default {
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}`
);
// Update index.css
writeFileSync(
"src/index.css",
`@tailwind base;
@tailwind components;
@tailwind utilities;`
);
}
spinner.succeed("Tailwind config completed ✅ 🏁");
} catch (error) {
spinner.fail(`Error creating React project ❌: ${error.message}`);
}
}
export function generateBoilerplateCode(fileType, fileName) {
const spinner = ora(`Generating ${fileType} file 🪚`).start();
const filePath = join(process.cwd(), fileName);
try {
if (existsSync(filePath)) {
spinner.fail(`File ${fileName} already exists`);
return;
}
const componentName = parse(fileName).name;
let boilerplateCode = ``;
switch (fileType) {
case "component":
boilerplateCode = `import React from 'react';
const ${componentName} = () => {
return (
<div>
<h1>${componentName} Component</h1>
</div>
)
}
export default ${componentName};`;
break;
case "service":
boilerplateCode = `export default class ${componentName} {
constructor() {
// Initalize service
}
// Add service methods
}`;
break;
}
writeFileSync(filePath, boilerplateCode);
spinner.succeed(`Generated ${fileType} file '${fileName}' ✅ 🏁`);
} catch (error) {
spinner.fail(`Error generating ${fileType} file ❌: ${error.message}`);
}
}