-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.js
218 lines (198 loc) · 6.03 KB
/
frontend.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import inquirer from "inquirer";
import { exec } from "child_process";
import fs from "fs";
import path from "path";
import { createSpinner } from "nanospinner";
function frontend() {
inquirer
.prompt([
{
type: "list",
name: "language",
message: "Do you want to use TypeScript or JavaScript?",
choices: ["TypeScript", "JavaScript"],
},
{
type: "list",
name: "style",
message: "Do you want to use CSS or SCSS?",
choices: ["CSS", "SCSS"],
},
{
type: "confirm",
name: "tailwind",
message: "Do you want to use TailwindCSS?",
default: false,
},
{
type: "input",
name: "projectName",
message: "What is the name of your project?",
validate: function (input) {
if (/^([A-Za-z\-\_\d])+$/.test(input)) return true;
else
return "Project name may only include letters, numbers, underscores, and hashes.";
},
},
])
.then((answers) => {
const { language, style, tailwind, projectName } = answers;
const template = language === "TypeScript" ? "--template typescript" : "";
console.log(
`Creating ${language} project with ${style} and TailwindCSS: ${
tailwind ? "Yes" : "No"
}`
);
const spinner = createSpinner("Creating React app...").start();
// Create React app with or without TypeScript
exec(
`npx create-react-app ${projectName} ${template}`,
(err, stdout, stderr) => {
if (err) {
spinner.error({ text: `Error creating React app: ${stderr}` });
return;
}
spinner.success({
text: `React app created successfully in ${projectName}`,
});
// Navigate to the project directory
process.chdir(projectName);
// Create components and utils folders
createFolders(["src/components", "src/utils"], spinner, () => {
// Track completion of tasks
let tasksCompleted = 0;
const totalTasks = (style === "SCSS" ? 1 : 0) + (tailwind ? 1 : 0);
// Modify the project based on user choices
if (style === "SCSS") {
setupSCSS(spinner, () => {
tasksCompleted++;
if (tasksCompleted === totalTasks) {
setupComplete(projectName);
}
});
}
if (tailwind) {
setupTailwind(spinner, () => {
tasksCompleted++;
if (tasksCompleted === totalTasks) {
setupComplete(projectName);
}
});
}
// If no additional setups, complete immediately
if (totalTasks === 0) {
setupComplete(projectName);
}
});
}
);
})
.catch((error) => {
if (error.isTtyError) {
console.log("Prompt couldn't be rendered in the current environment");
} else {
console.log("Something went wrong:", error);
}
});
}
function createFolders(folders, spinner, callback) {
folders.forEach((folder) => {
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder, { recursive: true });
}
});
spinner.success({ text: "Created components and utils folders" });
callback();
}
function setupSCSS(spinner, callback) {
spinner.update({ text: "Setting up SCSS..." });
exec("npm install sass", (err, stdout, stderr) => {
if (err) {
spinner.error({ text: `Error installing SCSS: ${stderr}` });
return;
}
spinner.success({ text: "SCSS setup completed" });
// Rename .css files to .scss
renameFiles("src", ".css", ".scss");
callback();
});
}
function setupTailwind(spinner, callback) {
spinner.update({ text: "Setting up TailwindCSS..." });
exec(
"npm install -D tailwindcss@latest postcss@latest autoprefixer@latest && npx tailwindcss init",
(err, stdout, stderr) => {
if (err) {
spinner.error({ text: `Error installing TailwindCSS: ${stderr}` });
return;
}
spinner.success({ text: "TailwindCSS setup completed" });
// Add TailwindCSS configuration to postcss.config.js and src/index.css
const tailwindConfig = `
module.exports = {
purge: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
};
`;
fs.writeFileSync("tailwind.config.js", tailwindConfig);
const postcssConfig = `
module.exports = {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
],
};
`;
fs.writeFileSync("postcss.config.js", postcssConfig);
const indexCSS = `
@tailwind base;
@tailwind components;
@tailwind utilities;
`;
fs.writeFileSync("src/index.css", indexCSS);
callback();
}
);
}
function setupComplete(projectName) {
console.log(`✔ Setup complete. Inside that directory, you can run several commands:
cd ${projectName}
npm start
Starts the development server.
npm run build
Bundles the app into static files for production.
npm test
Starts the test runner.
npm run eject
Removes this tool and copies build dependencies, configuration files
and scripts into the app directory. If you do this, you can’t go back!
We suggest that you begin by typing:
cd ${projectName}
npm start
`);
}
function renameFiles(dir, oldExt, newExt) {
fs.readdir(dir, (err, files) => {
if (err) throw err;
files.forEach((file) => {
const filePath = path.join(dir, file);
if (fs.lstatSync(filePath).isDirectory()) {
renameFiles(filePath, oldExt, newExt);
} else if (path.extname(file) === oldExt) {
const newFilePath = path.join(
dir,
path.basename(file, oldExt) + newExt
);
fs.renameSync(filePath, newFilePath);
}
});
});
}
export default frontend;