-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.ts
More file actions
409 lines (366 loc) · 13.5 KB
/
init.ts
File metadata and controls
409 lines (366 loc) · 13.5 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* @module
*
* This module contains the logic for the `init` command, which interactive
* initializes a new project from a template.
*/
import { blue, bold, cyan, green, magenta, red, yellow } from "@std/fmt/colors";
import { resolve } from "@std/path";
import { exists } from "@std/fs";
import { startSpinner } from "./lib/ui.ts";
import { promptInput, promptSelect } from "./lib/prompt.ts";
import { degitRepoToDir } from "./lib/degit.ts";
import { replacePlaceholdersInRepo } from "./lib/replace.ts";
import { isValidProjectName, slugifyProjectName } from "./lib/validate.ts";
import { generateLogo } from "./lib/ai.ts";
export async function initCommand() {
console.log(magenta(bold(`
_____ _
/ ____| | |
| | _ __ _ _ _ __ ___ | |__ __ __ _ __ __ _ _ __
| | | '__| | | | '_ \\ / __| '_ \\\\ \\ /\\ / / '__/ _\` | '_ \\
| |____| | | |_| | | | | (__| | | |\\ V V /| | | (_| | |_) |
\\_____|_| \\__,_|_| |_|\\___|_| |_| \\_/\\_/ |_| \\__,_| .__/
| |
|_|
`)));
console.log(cyan(bold(" Welcome to the crunchwrap app CLI! 🌯 Let's build something awesome.\n")));
// ------------------------
// Step 1: Collect Project Information
// ------------------------
const projectnameInput = promptInput("📂 Project name", "the-example-app", {
required: true,
validate: (v) => {
if (!isValidProjectName(v)) {
return red(" ❌ Use letters, numbers, and dashes only.");
}
return null;
},
});
const projectname = projectnameInput.toLowerCase();
const gitRepoInput = promptInput(
"🌐 Git repository URL or 'user/repo' (optional)",
"",
);
let gitRepo = gitRepoInput.trim();
// Handle shorthand user/repo format for GitHub
if (gitRepo && !gitRepo.includes("://") && !gitRepo.includes("@")) {
if (gitRepo.includes("/")) {
const parts = gitRepo.split("/");
const user = parts[0].trim();
const repo = parts[1].trim();
gitRepo = `git@github.com:${user}/${repo}.git`;
console.log(cyan(` 🚀 Expanded to: ${gitRepo}`));
}
}
let gitVisibility = "public";
if (gitRepo) {
gitVisibility = await promptSelect("🔒 Repository visibility", [
{ label: "🔓 Public", value: "public" },
{ label: "🔒 Private", value: "private" },
]);
}
const shortname = promptInput("📝 Short name (optional)", "Example");
const domainname = promptInput("🌍 Domain name (optional)", "example.com");
const title = promptInput("👑 Title (optional)", "The Example Page");
const description = promptInput(
"📖 Description (optional)",
"Find examples every day, anywhere, for example.",
);
const email = promptInput(
"📧 Email contact (optional)",
"hello@example.com",
);
const phone = promptInput("📞 Phone number (optional)", "+46 7182387123");
// Logo generation prompt
console.log(cyan(bold("\n🎨 AI Logo Generation")));
const logoPrompt = promptInput(
"✨ Enter a prompt for your logo (optional, leave empty to skip)",
"",
);
let geminiApiKey = Deno.env.get("GEMINI_API_KEY") || "";
if (logoPrompt && !geminiApiKey) {
geminiApiKey = promptInput("🔑 Enter your Google Gemini API key", "", {
required: true,
});
}
const meta = {
projectname: projectname.trim(),
shortname: shortname.trim(),
domainname: domainname.trim(),
title: title.trim(),
description: description.trim(),
email: email.trim(),
phone: phone.trim(),
slug: slugifyProjectName(projectname),
};
console.log("");
// ------------------------
// Step 2: Select Template
// ------------------------
const templates: {
label: string;
value: { key: string; url: string | null };
}[] = [
{
label: "🟢 Nuxt Tailwind",
value: {
key: "nuxt4-tw-template",
url: "https://github.com/crunchwrap89/nuxt4-tw-template",
},
},
{
label: "🔥 Nuxt Tailwind (Firebase)",
value: {
key: "nuxt4-tw-fb-template",
url: "https://github.com/crunchwrap89/nuxt4-tw-fb-template",
},
},
{ label: "🏗️ TBD", value: { key: "tbd-c", url: null } },
];
const template = await promptSelect(
"✨ Select which template to use:",
templates,
);
if (!template.url) {
console.log(yellow("\n🚧 That template is not available yet. Exiting."));
return;
}
// ------------------------
// Step 3: Generate Project
// ------------------------
const destDir = resolve(Deno.cwd(), meta.projectname);
if (await exists(destDir)) {
console.log(red(`\n💥 Error: Folder already exists: ${destDir}`));
console.log(
yellow(
"💡 Please choose a different project name or remove the existing folder.\n",
),
);
Deno.exit(1);
}
const spinner = startSpinner("🚀 Generating repository...");
try {
// 1. Download template using degit
await degitRepoToDir(template.url, destDir);
// 2. Replace placeholders with project metadata
await replacePlaceholdersInRepo(destDir, meta);
spinner.stop(true);
// ------------------------
// Step 4: Firebase Init
// ------------------------
if (template.key.includes("-fb-")) {
console.log(cyan("\n🔥 Running firebase init..."));
try {
const firebaseCmd = new Deno.Command("firebase", {
args: ["init"],
cwd: destDir,
stdin: "inherit",
stdout: "inherit",
stderr: "piped",
});
const process = firebaseCmd.spawn();
const { success, stderr } = await process.output();
if (!success) {
const errorMsg = new TextDecoder().decode(stderr);
if (errorMsg.includes("firestore")) {
console.log(
yellow(
"\n⚠️ It looks like Firestore is not yet enabled in your Firebase project.",
),
);
console.log(
yellow("🔗 Please go to the Firebase Console to create it:"),
);
console.log(
blue(bold(" https://console.firebase.google.com/project/_/firestore")),
);
} else {
console.log(
yellow("\n⚠️ Firebase initialization was not completed successfully."),
);
if (errorMsg.trim()) {
console.log(red(errorMsg));
}
}
}
} catch (_err) {
console.log(
yellow(
"⚠️ Could not run 'firebase init'. Make sure you have firebase-tools installed.",
),
);
}
}
// ------------------------
// Step 5: Git Initialization and Publishing
// ------------------------
if (gitRepo) {
console.log(cyan("\n🛠️ Initializing local git repository..."));
try {
const runGit = async (args: string[]) => {
const command = new Deno.Command("git", {
args,
cwd: destDir,
stdout: "inherit",
stderr: "inherit",
});
const { success } = await command.output();
if (!success) {
throw new Error(`Git command failed: git ${args.join(" ")}`);
}
};
await runGit(["init"]);
await runGit(["add", "."]);
await runGit(["commit", "-m", "Initial commit from crunchwrap CLI"]);
await runGit(["branch", "-M", "main"]);
// If it's a GitHub repo, try to create it if it doesn't exist
if (gitRepo.includes("github.com")) {
try {
// Extract repo name from URL
// Handle SSH: git@github.com:user/repo.git
// Handle HTTPS: https://github.com/user/repo
let repoPath = "";
if (gitRepo.includes("github.com:")) {
// SSH format: git@github.com:user/repo.git
repoPath = gitRepo.split("github.com:")[1]
?.replace(/\.git$/, "")
?.trim();
} else if (gitRepo.includes("github.com/")) {
// HTTPS format: https://github.com/user/repo
repoPath = gitRepo.split("github.com/")[1]
?.replace(/\.git$/, "")
?.trim();
}
if (repoPath) {
// Ensure we don't have leading/trailing slashes
repoPath = repoPath.replace(/^\/+|\/+$/g, "");
console.log(cyan(`\n🐙 Ensuring GitHub repository exists: ${repoPath}`));
// First, check if gh is installed and authenticated
const authCheck = new Deno.Command("gh", {
args: ["auth", "status"],
stdout: "piped",
stderr: "piped",
});
const authResult = await authCheck.output();
if (!authResult.success) {
console.log(red("\n❌ GitHub CLI (gh) is required but not authenticated."));
console.log(
yellow(
" Please run 'gh auth login' to authenticate and try again.",
),
);
Deno.exit(1);
}
// Ensure git is configured to use gh for credentials
try {
const setupGit = new Deno.Command("gh", {
args: ["auth", "setup-git"],
stdout: "null",
stderr: "null",
});
await setupGit.output();
} catch (_e) {
// Ignore failures here
}
const visibilityFlag = gitVisibility === "private"
? "--private"
: "--public";
// Check if repo already exists using 'gh repo view'
const viewCommand = new Deno.Command("gh", {
args: ["repo", "view", repoPath],
cwd: destDir,
stdout: "piped",
stderr: "piped",
});
const viewResult = await viewCommand.output();
if (!viewResult.success) {
// If it doesn't exist, create it and push
console.log(cyan(`🚀 Creating ${gitVisibility} repository: ${repoPath}...`));
// Use --source=. to link the current directory to the new repo
// --remote=origin to set up the remote
// --push to push the initial commit
const ghCommand = new Deno.Command("gh", {
args: [
"repo",
"create",
repoPath,
visibilityFlag,
"--source=.",
"--remote=origin",
"--push",
],
cwd: destDir,
stdout: "inherit",
stderr: "inherit",
});
const { success } = await ghCommand.output();
if (!success) {
throw new Error(`Failed to create GitHub repository via 'gh' CLI.`);
}
console.log(green("✅ GitHub repository created and pushed successfully."));
} else {
console.log(
yellow("ℹ️ GitHub repository already exists. Linking and pushing..."),
);
// Link manually
const remoteCommand = new Deno.Command("git", {
args: ["remote", "add", "origin", gitRepo],
cwd: destDir,
stdout: "null",
stderr: "null",
});
await remoteCommand.output();
// Push manually
await runGit(["push", "-u", "origin", "main"]);
console.log(green(`✅ Pushed to existing repository: ${gitRepo}`));
}
}
} catch (err) {
console.log(yellow("\n⚠️ Failed to publish to GitHub."));
if (err instanceof Error) {
console.log(red(` ${err.message}`));
}
console.log(
yellow(
" Please ensure you have correct permissions and 'gh' is set up correctly.",
),
);
}
} else {
console.log(
yellow(
"\n⚠️ Only GitHub is supported for automated publishing via the 'gh' CLI.",
),
);
console.log(yellow(" Skipping remote publishing."));
}
} catch (err) {
console.log(yellow("\n⚠️ Failed to initialize git repository."));
if (err instanceof Error) {
console.log(red(` ${err.message}`));
}
}
}
// ------------------------
// Step 4: AI Logo Generation (if prompt and key provided)
// ------------------------
if (logoPrompt && geminiApiKey) {
console.log("");
await generateLogo(logoPrompt, geminiApiKey, destDir);
}
console.log(
green(bold(`\n🎉 Project successfully generated in: ${meta.projectname}`)),
);
console.log(green("🚀 Next steps:"));
console.log(cyan(` cd ${meta.projectname}`));
console.log(cyan(" yarn install"));
console.log(cyan(" yarn dev\n"));
console.log(magenta(bold("Happy coding! 🌮🌯✨\n")));
} catch (err) {
spinner.stop(false);
console.error(red("\n💥 Failed to generate project."));
console.error(red(err instanceof Error ? err.message : String(err)));
Deno.exit(1);
}
}