-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.js
234 lines (199 loc) · 6.44 KB
/
create.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env node
import { promises as fs } from "fs";
import path from "path";
import { execSync } from "child_process";
import readline from "readline";
let projectName = process.argv[2];
const repoUrl = "https://github.com/litpack/create";
(async () => {
console.log(
"\x1b[34m🌟 Welcome to the Litpack Project Generator! Let's get started...\x1b[0m"
);
if (!projectName) {
projectName = await promptForProjectName();
}
projectName = await checkFolderExists(projectName);
const packageManager = await promptPackageManager();
const targetDir = path.join(process.cwd(), projectName);
try {
console.log(`Creating project "${projectName}"...`);
await fs.mkdir(targetDir, { recursive: true });
console.log("⏳ Cloning repository...");
await cloneRepo(repoUrl, targetDir);
console.log("🎉 Repository cloned successfully!");
console.log("Updating project files...");
await updatePackageJson(targetDir, projectName);
console.log("✅ Project files updated successfully!");
projectCreated(packageManager);
} catch (err) {
console.error(
`\x1b[31m❌ Error creating project directory: ${err.message}\x1b[0m`
);
process.exit(1);
}
})();
async function promptForProjectName() {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const askForName = () => {
rl.question("\x1b[32m📛 Please provide a project name: \x1b[0m", (name) => {
name = name.trim();
if (name) {
rl.close();
resolve(name);
} else {
console.log("\x1b[31m⚠️ Project name cannot be empty. Try again.\x1b[0m");
askForName();
}
});
};
askForName();
});
}
async function checkFolderExists(name) {
const targetDir = path.join(process.cwd(), name);
try {
const stats = await fs.stat(targetDir);
if (stats.isDirectory()) {
console.log(`\x1b[33m🚨 The folder "${name}" already exists.\x1b[0m`);
return await promptForNewProjectName();
}
} catch (err) {
if (err.code !== "ENOENT") {
console.error(`\x1b[31m❌ Error checking directory: ${err.message}\x1b[0m`);
process.exit(1);
}
}
return name;
}
function promptForNewProjectName() {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
"\x1b[32m🔄 Please provide a new project name: \x1b[0m",
(newName) => {
rl.close();
resolve(newName.trim());
}
);
});
}
async function promptPackageManager() {
const packageManagers = ["npm", "yarn", "pnpm", "bun"];
const choice = await promptForChoice(packageManagers);
console.log(`\x1b[36m🚀 You selected: ${choice}\x1b[0m`);
return choice;
}
async function promptForChoice(choices) {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let currentIndex = 0;
const displayChoices = () => {
console.clear();
console.log("\x1b[32mPlease choose a package manager:\x1b[0m");
choices.forEach((choice, index) => {
const isSelected = index === currentIndex ? "👉" : " ";
console.log(`${isSelected} ${choice}`);
});
};
const handleInput = (key) => {
if (key === '\u001B[B') {
currentIndex = (currentIndex + 1) % choices.length;
} else if (key === '\u001B[A') {
currentIndex = (currentIndex - 1 + choices.length) % choices.length;
} else if (key === ' ' || key === '\u000D') {
rl.close();
resolve(choices[currentIndex]);
}
displayChoices();
};
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
displayChoices();
process.stdin.on('keypress', (str, key) => {
if (key.name === 'escape') {
rl.close();
process.exit(0);
}
handleInput(key.sequence);
});
});
}
async function cloneRepo(repoUrl, targetDir) {
try {
execSync(`git clone ${repoUrl} ${targetDir}`, { stdio: "inherit" });
execSync(`git -C ${targetDir} checkout stable`, { stdio: "inherit" });
await fs.rm(path.join(targetDir, '.git'), { recursive: true, force: true });
} catch (err) {
throw new Error("Failed to clone repository: " + err.message);
}
}
async function updatePackageJson(targetDir, projectName) {
const packageJsonPath = path.join(targetDir, "package.json");
try {
const packageJson = await fs.readFile(packageJsonPath, "utf-8");
const updatedPackageJson = packageJson.replace(
/"name":\s*"(.*?)"/,
`"name": "${projectName}"`
);
await fs.writeFile(packageJsonPath, updatedPackageJson, "utf-8");
console.log(
`\x1b[32m🔧 Updated package.json with project name: ${projectName}\x1b[0m`
);
} catch (err) {
console.error(
`\x1b[31m❌ Failed to update package.json: ${err.message}\x1b[0m`
);
process.exit(1);
}
}
function projectCreated(packageManager) {
console.log(
`\x1b[32m🎉 Project "${projectName}" created successfully!\x1b[0m`
);
try {
execSync(`${packageManager} --version`, { stdio: "ignore" });
console.log(`\x1b[34m✅ ${packageManager} is installed.\x1b[0m`);
} catch {
console.log(
`\x1b[31m❌ ${packageManager} is not installed. Please install it.\x1b[0m`
);
console.log("\x1b[33mYou can install " + packageManager + " using:\x1b[0m");
if (packageManager === "pnpm") {
console.log("📦 npm install -g pnpm");
} else if (packageManager === "yarn") {
console.log("📦 npm install -g yarn");
} else if (packageManager === "bun") {
console.log("📦 npm install -g bun");
}
}
console.log(
`\x1b[32m\n🚀 To get started, navigate into your project folder:\x1b[0m`
);
console.log(`\x1b[36m📁 cd ${projectName}\x1b[0m`);
console.log(
`\x1b[32mThen, install the dependencies with:\x1b[0m`
);
console.log(`\x1b[35m🔗 ${packageManager} install\x1b[0m`);
console.log(
`\x1b[32m\n🛠️ You can run the following lifecycle scripts:\x1b[0m`
);
console.log(
`\x1b[34m1. 🧹 Clean the build directory: ${packageManager} run clean\x1b[0m`
);
console.log(
`\x1b[34m2. 🏗️ Build the project: ${packageManager} run build\x1b[0m`
);
console.log(
`\x1b[34m3. 🚦 Start the development server: ${packageManager} start\x1b[0m`
);
}