-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanViteSetup.js
140 lines (120 loc) · 3.92 KB
/
cleanViteSetup.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
#!/usr/bin/env node
import path from 'path';
import fs from 'fs';
import readline from 'readline';
import chalk from 'chalk';
import { execSync } from 'child_process';
// Define paths for main files and folders
const paths = {
appJsx: path.join(process.cwd(), 'src', 'App.jsx'),
appTsx: path.join(process.cwd(), 'src', 'App.tsx'),
appCss: path.join(process.cwd(), 'src', 'App.css'),
assetsFolder: path.join(process.cwd(), 'src', 'assets'),
indexCss: path.join(process.cwd(), 'src', 'index.css'),
tailwindConfig: path.join(process.cwd(), 'tailwind.config.js'),
};
// Templates
const vanillaAppTemplate = `
export default function App() {
return (
<h1>Hello world!</h1>
);
}
`;
const tailwindAppTemplate = `
export default function App() {
return (
<h1 className="text-3xl font-bold underline">
Hello world!
</h1>
);
}
`;
const tailwindCssContent = `
@tailwind base;
@tailwind components;
@tailwind utilities;
`;
const tailwindConfigContent = `
/** @type {import('tailwindcss').Config} */
export default {
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
`;
// Utility functions
const fileExists = (filePath) => fs.existsSync(filePath);
const deleteFile = (filePath) => {
if (fileExists(filePath)) {
fs.unlinkSync(filePath);
console.log(chalk.red(`Deleted: ${filePath}`));
}
};
const deleteFolder = (folderPath) => {
if (fileExists(folderPath)) {
// fs.rmdirSync(folderPath, { recursive: true });
fs.rmSync(folderPath, { recursive: true, force: true });
console.log(chalk.red(`Deleted folder: ${folderPath}`));
}
};
const setupTailwind = () => {
console.log(chalk.blue("Setting up Tailwind CSS..."));
try {
execSync("npm install -D tailwindcss postcss autoprefixer", { stdio: 'inherit' });
execSync("npx tailwindcss init -p", { stdio: 'inherit' });
fs.writeFileSync(paths.tailwindConfig, tailwindConfigContent.trim(), 'utf-8');
fs.writeFileSync(paths.indexCss, tailwindCssContent.trim(), 'utf-8');
console.log(chalk.green("Tailwind CSS setup complete."));
} catch (error) {
console.error("Error during Tailwind setup:", error.message);
}
};
// Display a selection prompt
const promptUser = (question) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => rl.question(question, (answer) => {
rl.close();
resolve(answer.toLowerCase());
}));
};
const displayOptions = async () => {
console.log(chalk.blue("\nChoose an option for CSS setup:"));
console.log(`1. ${chalk.cyan("Vanilla CSS")}`);
console.log(`2. ${chalk.green("Tailwind CSS")}\n`);
const choice = await promptUser("Select (1 or 2): ");
return choice === '2' ? 'tailwind' : 'vanilla';
};
const runCleanup = async () => {
const confirm = await promptUser("Ok to proceed? (y)");
if (confirm !== 'y') {
console.log(chalk.yellow("Cleanup canceled."));
return;
}
const cssChoice = await displayOptions();
// Proceed with cleanup
deleteFile(paths.appCss);
deleteFolder(paths.assetsFolder);
const appPath = fileExists(paths.appJsx) ? paths.appJsx : paths.appTsx;
if (appPath) {
const appTemplate = (cssChoice === 'tailwind') ? tailwindAppTemplate : vanillaAppTemplate;
fs.writeFileSync(appPath, appTemplate.trim(), 'utf-8');
console.log(chalk.blue(`App component reset with ${(cssChoice === 'tailwind') ? "Tailwind" : "vanilla"} template.`));
}
if (cssChoice === 'tailwind') {
setupTailwind();
} else {
fs.writeFileSync(paths.indexCss, '', 'utf-8');
console.log(chalk.green("index.css reset to empty for vanilla CSS."));
}
console.log(chalk.green("Cleanup complete."));
};
runCleanup().catch((error) => console.error("Error during cleanup process:", error.message));