-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-interactive.ts
More file actions
223 lines (193 loc) · 7.27 KB
/
run-interactive.ts
File metadata and controls
223 lines (193 loc) · 7.27 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
import os from "node:os";
import { existsSync, readdirSync } from "node:fs";
import { relative, resolve } from "node:path";
import { confirm, intro, isCancel, note, outro, select, spinner, text } from "@clack/prompts";
import { bye } from "../console.ts";
import { getRandomJoke } from "../commands/joke/jokes.ts";
import { installDeps } from "../integrations/update-app.ts";
import { loadAppStarters } from "../integrations/load-app-starters.ts";
import { name as detectedPm } from "panam/pm";
import { backgroundInstallDeps } from "./background-install.ts";
import { createApp } from "./create-app.ts";
import { initGitRepo } from "./git-init.ts";
/**
* Run the create-qwik interactive CLI.
*
* Presents a 6-step prompt flow:
* 1. Intro + 500ms pause
* 2. Project directory selection
* 3. Starter template selection
* 4. Package manager selection
* 5. Install dependencies confirmation (with joke while waiting)
* 6. Git init confirmation
*
* Background dependency install begins immediately after Prompt 2 to
* overlap with the remaining user prompts.
*/
export async function runCreateInteractiveCli(): Promise<void> {
// Load starters upfront
const starters = await loadAppStarters();
const baseApp = starters.find((s) => s.id === "base");
const selectableStarters = starters.filter((s) => s.id !== "base");
if (!baseApp) {
throw new Error("base starter not found in stubs/apps/");
}
// Step 1: Intro + 500ms pause (spec-required delay)
intro("Let's create a Qwik app");
await new Promise<void>((r) => setTimeout(r, 500));
// We track bgInstall across prompts so cancel handlers can abort it
let bgInstall: ReturnType<typeof backgroundInstallDeps> | null = null;
try {
// -------------------------------------------------------------------------
// Prompt 2: Project directory
// -------------------------------------------------------------------------
const dirAnswer = await text({
message: "Where would you like to create your new project?",
placeholder: "./qwik-app",
defaultValue: "./qwik-app",
});
if (isCancel(dirAnswer)) {
bye();
}
const dirRaw = (dirAnswer as string) || "./qwik-app";
// Resolve path: support ~/... or relative to cwd
let resolvedOutDir: string;
if (dirRaw.startsWith("~/")) {
resolvedOutDir = resolve(os.homedir(), dirRaw.slice(2));
} else {
resolvedOutDir = resolve(process.cwd(), dirRaw);
}
// Start background install immediately (runs concurrently with remaining prompts)
bgInstall = backgroundInstallDeps(baseApp.dir, resolvedOutDir);
// Guard: if outDir already exists and is non-empty, abort and error
if (existsSync(resolvedOutDir)) {
const entries = readdirSync(resolvedOutDir);
if (entries.length > 0) {
await bgInstall.abort();
throw new Error(`Directory already exists and is not empty: ${resolvedOutDir}`);
}
}
// -------------------------------------------------------------------------
// Prompt 3: Starter selection
// -------------------------------------------------------------------------
const starterAnswer = await select({
message: "Select a starter",
options: selectableStarters.map((s) => ({
value: s.id,
label: s.name,
hint: s.id,
})),
});
if (isCancel(starterAnswer)) {
await bgInstall.abort();
bye();
}
const selectedStarter = starterAnswer as string;
// -------------------------------------------------------------------------
// Prompt 4: Package manager selection
// -------------------------------------------------------------------------
const pmAnswer = await select({
message: "Which package manager do you prefer?",
options: (["npm", "pnpm", "yarn", "bun"] as const).map((pm) => {
const opt: { value: string; label: string; hint?: string } = { value: pm, label: pm };
if (pm === detectedPm) {
opt.hint = "(detected)";
}
return opt;
}),
initialValue: detectedPm,
});
if (isCancel(pmAnswer)) {
await bgInstall.abort();
bye();
}
const selectedPm = pmAnswer as string;
// -------------------------------------------------------------------------
// Prompt 5: Install dependencies
// -------------------------------------------------------------------------
const installAnswer = await confirm({
message: "Would you like to install dependencies?",
});
if (isCancel(installAnswer)) {
await bgInstall.abort();
bye();
}
const shouldInstall = installAnswer as boolean;
// If user wants deps and background install is still running, display a joke
// while we wait for it to finish
if (shouldInstall && bgInstall.success === undefined) {
const [setup, punchline] = getRandomJoke();
note(`${setup}\n\n${punchline}`, "While you wait...");
const s = spinner();
s.start("Installing dependencies");
// Wait for the background install result (complete() is called later)
await new Promise<void>((r) => {
const check = () => {
if (bgInstall!.success !== undefined) {
r();
} else {
setTimeout(check, 100);
}
};
check();
});
s.stop("Dependencies install ready");
}
// -------------------------------------------------------------------------
// Prompt 6: Git init
// -------------------------------------------------------------------------
const gitAnswer = await confirm({
message: "Initialize a new git repository?",
});
if (isCancel(gitAnswer)) {
await bgInstall.abort();
bye();
}
const shouldGitInit = gitAnswer as boolean;
// -------------------------------------------------------------------------
// Create the project
// -------------------------------------------------------------------------
await createApp({
appId: selectedStarter,
outDir: resolvedOutDir,
pkgManager: selectedPm,
});
// Handle dependency installation
if (shouldInstall) {
if (bgInstall.success !== false) {
// Background install succeeded (or is still running) — move results in
const ok = await bgInstall.complete(resolvedOutDir);
if (!ok) {
// Background install failed — fall back to running install synchronously
await installDeps(resolvedOutDir);
}
} else {
// Background install already failed — run deps synchronously
await installDeps(resolvedOutDir);
}
} else {
// User declined deps — abort background install and clean up
await bgInstall.abort();
}
// Initialize git repository (non-fatal)
if (shouldGitInit) {
initGitRepo(resolvedOutDir);
}
// Print next steps
const relDir = relative(process.cwd(), resolvedOutDir);
const nextSteps = [
`cd ${relDir}`,
...(!shouldInstall ? [`${selectedPm} install`] : []),
`${selectedPm} run dev`,
].join("\n");
note(nextSteps, "Next steps");
outro("Happy coding!");
process.exit(0);
} catch (e: unknown) {
// On any error, abort background install to avoid orphan tmp dirs
if (bgInstall) {
await bgInstall.abort().catch(() => {});
}
throw e;
}
}