-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
293 lines (257 loc) · 7.99 KB
/
index.js
File metadata and controls
293 lines (257 loc) · 7.99 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env node
import fs from "fs-extra";
import path from "path";
import prompts from "prompts";
import chalk from "chalk";
import { spawnSync } from "child_process";
import { config, getTemplates, hasTemplates } from "./lib/config.js";
import { configWizard, manageTemplates } from "./lib/wizard.js";
import {
detectPackageManager,
checkAiReleaseSupport,
getUniqueProjectName,
} from "./lib/utils.js";
async function init() {
// Check for --config flag
if (process.argv.includes("--config")) {
await manageTemplates(config);
return;
}
// Parse installation flags
const shouldInstall = process.argv.includes("--install");
const shouldSkipInstall = process.argv.includes("--no-install");
// Default behavior: skip installation if no flag is specified
const autoInstall = shouldInstall && !shouldSkipInstall;
console.log(chalk.bold.cyan("\n🚀 UI Scaffold CLI (create-ui-app)\n"));
// Check if running in interactive terminal
if (!process.stdin.isTTY) {
console.log(
chalk.yellow(
"⚠️ Warning: Non-interactive terminal detected. Using default values.\n"
)
);
}
// 1. Check if templates exist
if (!hasTemplates()) {
const { runWizard } = await prompts({
type: "confirm",
name: "runWizard",
message: "No templates found. Run configuration wizard?",
initial: true,
});
if (runWizard) {
await configWizard(config);
} else {
console.log(
chalk.yellow(
"\n⚠️ Operation cancelled. Run with --config to add templates later.\n"
)
);
process.exit(0);
}
}
// 2. Get Templates
const templates = getTemplates();
if (!templates) {
process.exit(0);
}
// 3. Prompt User
const response = await prompts(
[
{
type: "text",
name: "projectName",
message: "What is the project name?",
initial: "my-app",
validate: (value) =>
value.trim().length > 0 ? true : "Project name is required",
},
{
type: "select",
name: "template",
message: "Which template would you like to use?",
choices: templates,
initial: 0,
},
],
{
onCancel: () => {
console.log(chalk.yellow("\n⚠️ Operation cancelled."));
process.exit(0);
},
}
);
if (!response.projectName || !response.template) {
console.log(chalk.yellow("\n⚠️ Operation cancelled."));
process.exit(0);
}
let { projectName, template } = response;
// Resolve naming conflicts automatically
const uniqueName = getUniqueProjectName(projectName, process.cwd());
if (uniqueName !== projectName) {
console.log(
chalk.yellow(
`\n⚠️ Directory '${projectName}' already exists. Creating project in '${uniqueName}' instead.\n`
)
);
projectName = uniqueName;
}
const targetDir = path.join(process.cwd(), projectName);
// 4. Clone Repository
console.log(
chalk.dim(
`\nDownloading template from ${template.repo}#${template.branch}...`
)
);
try {
// Use git clone directly to avoid 'rm' issues on Windows and handle private repos better
const gitArgs = [
"clone",
"--depth",
"1",
"--branch",
template.branch,
template.repo,
targetDir,
];
const { status } = spawnSync("git", gitArgs, { stdio: "inherit" });
if (status !== 0) {
throw new Error(`Git clone failed with status ${status}`);
}
} catch (err) {
console.error(chalk.red(`\n❌ Error cloning repository: ${err.message}`));
if (err.message.includes("Host key verification failed")) {
console.log(
chalk.yellow("\n💡 Tip: Your SSH key is not authenticated with GitHub.")
);
console.log(
chalk.gray(" Run this command to fix it: ssh -T git@github.com")
);
console.log(
chalk.gray(
" Or update the template to use an HTTPS URL via 'npm run config'"
)
);
} else {
console.log(
chalk.yellow(
"Tip: Ensure you have SSH access to the repository if it is private."
)
);
}
process.exit(1);
}
// 5. Post-processing
process.chdir(targetDir);
try {
// Clean git history
await fs.remove(".git");
// Init new git repo
spawnSync("git", ["init"], { stdio: "ignore" });
// Handle .env
if (fs.existsSync(".env.example")) {
await fs.copy(".env.example", ".env");
console.log(chalk.green("✔ Created .env from .env.example"));
}
// Automatic dependency installation based on flags
const packageManager = detectPackageManager();
let didInstall = false;
if (autoInstall && packageManager) {
console.log(
chalk.dim(`\nInstalling dependencies with ${packageManager}...\n`)
);
const installResult = spawnSync(packageManager, ["install"], {
stdio: "inherit",
cwd: targetDir,
shell: true,
});
if (installResult.status === 0) {
didInstall = true;
console.log(chalk.green("\n✔ Dependencies installed successfully"));
} else {
console.log(
chalk.yellow(
"\n⚠️ Installation failed. You may need to run the install command manually."
)
);
}
}
// Check for AI release support
const hasAiSupport = checkAiReleaseSupport(targetDir);
// Read template info
const templateInfoPath = path.join(targetDir, ".template-info.json");
if (fs.existsSync(templateInfoPath)) {
const templateInfo = JSON.parse(
fs.readFileSync(templateInfoPath, "utf-8")
);
console.log(
chalk.bold.green(
`\n✅ ${templateInfo.name} (${templateInfo.variant}) ready!`
)
);
console.log(chalk.gray(` ${templateInfo.description}\n`));
if (templateInfo.features && templateInfo.features.length > 0) {
console.log(chalk.cyan("📦 Features:"));
templateInfo.features.forEach((f) =>
console.log(chalk.gray(` • ${f}`))
);
console.log("");
}
if (templateInfo.postInstall && templateInfo.postInstall.steps) {
console.log(chalk.cyan("📋 Next steps:"));
console.log(chalk.gray(` 1. cd ${projectName}`));
// Filter out install step if already installed
const steps = didInstall
? templateInfo.postInstall.steps.filter(
(step) =>
!step.toLowerCase().includes("npm install") &&
!step.toLowerCase().includes("yarn install") &&
!step.toLowerCase().includes("pnpm install") &&
!step.toLowerCase().includes("bun install")
)
: templateInfo.postInstall.steps;
steps.forEach((step, i) => {
console.log(chalk.gray(` ${i + 2}. ${step}`));
});
console.log("");
}
// AI release support notification
if (hasAiSupport) {
console.log(chalk.cyan("🤖 AI Release Support Detected"));
console.log(
chalk.gray(
" To use 'npm run project:release', please set your GEMINI_API_KEY in .env"
)
);
console.log("");
}
} else {
// Fallback
console.log(chalk.bold.green(`\n✅ Project ready in ./${projectName}\n`));
console.log(chalk.cyan("Next steps:"));
console.log(chalk.gray(` cd ${projectName}`));
if (!didInstall) {
console.log(chalk.gray(" npm install"));
}
console.log(chalk.gray(" npm run dev\n"));
// AI release support notification
if (hasAiSupport) {
console.log(chalk.cyan("🤖 AI Release Support Detected"));
console.log(
chalk.gray(
" To use 'npm run project:release', please set your GEMINI_API_KEY in .env"
)
);
console.log("");
}
}
} catch (err) {
console.error(
chalk.red(`\n❌ Error during post-processing: ${err.message}`)
);
}
}
init().catch((err) => {
console.error(chalk.red(err));
process.exit(1);
});