create-vanilla-vite 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/README.md +82 -11
- package/bin/create-template.mjs +801 -159
- package/package.json +6 -2
- package/scripts/add-dependency.mjs +7 -3
- package/scripts/dev.mjs +512 -19
- package/scripts/install-info.mjs +54 -9
- package/scripts/run-app.mjs +4 -9
package/bin/create-template.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
4
5
|
import spawn from "cross-spawn";
|
|
5
6
|
import { emitKeypressEvents } from "node:readline";
|
|
6
7
|
import { createInterface } from "node:readline/promises";
|
|
@@ -9,8 +10,114 @@ import { dirname, join, resolve } from "node:path";
|
|
|
9
10
|
import { fileURLToPath } from "node:url";
|
|
10
11
|
|
|
11
12
|
const args = process.argv.slice(2);
|
|
12
|
-
const
|
|
13
|
-
|
|
13
|
+
const allowedOptions = [
|
|
14
|
+
"--yes",
|
|
15
|
+
"--default",
|
|
16
|
+
"--host",
|
|
17
|
+
"--tailwind",
|
|
18
|
+
"--no-tailwind",
|
|
19
|
+
"--axios",
|
|
20
|
+
"--no-axios",
|
|
21
|
+
"--router",
|
|
22
|
+
"--no-router",
|
|
23
|
+
"--icons",
|
|
24
|
+
"--backend",
|
|
25
|
+
"--ts",
|
|
26
|
+
"--js",
|
|
27
|
+
"--structure",
|
|
28
|
+
"--pm",
|
|
29
|
+
"--template",
|
|
30
|
+
"--no-start",
|
|
31
|
+
"--no-update-check",
|
|
32
|
+
"--no-git",
|
|
33
|
+
];
|
|
34
|
+
const valueOptions = new Set(["--icons", "--backend", "--structure", "--pm", "--template"]);
|
|
35
|
+
const optionNames = new Set(allowedOptions);
|
|
36
|
+
const positionalArgs = [];
|
|
37
|
+
const optionErrors = [];
|
|
38
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
39
|
+
const argument = args[index];
|
|
40
|
+
if (!argument.startsWith("--")) {
|
|
41
|
+
positionalArgs.push(argument);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const separator = argument.indexOf("=");
|
|
45
|
+
const name = separator === -1 ? argument : argument.slice(0, separator);
|
|
46
|
+
if (!optionNames.has(name)) {
|
|
47
|
+
optionErrors.push(`Unknown option: ${name}`);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (valueOptions.has(name)) {
|
|
51
|
+
if (separator === -1 || !argument.slice(separator + 1)) {
|
|
52
|
+
optionErrors.push(`${name} requires a value in the form ${name}=<value>`);
|
|
53
|
+
if (separator === -1 && args[index + 1] && !args[index + 1].startsWith("--")) {
|
|
54
|
+
index += 1;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
} else if (separator !== -1) {
|
|
58
|
+
optionErrors.push(`${name} does not accept a value`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const readOption = (name) =>
|
|
62
|
+
args.find((argument) => argument.startsWith(`--${name}=`))?.slice(name.length + 3);
|
|
63
|
+
const hasOption = (name) => args.includes(`--${name}`);
|
|
64
|
+
const yes = hasOption("yes");
|
|
65
|
+
let activeChild;
|
|
66
|
+
let creationCancelled = false;
|
|
67
|
+
const useDefaultStructure =
|
|
68
|
+
hasOption("default") || process.env.npm_config_default === "true";
|
|
69
|
+
const exposeClient = hasOption("host");
|
|
70
|
+
const noTailwind = hasOption("no-tailwind");
|
|
71
|
+
const noAxios = hasOption("no-axios");
|
|
72
|
+
const noStart = hasOption("no-start");
|
|
73
|
+
const skipUpdateCheck =
|
|
74
|
+
hasOption("no-update-check") || yes || process.env.CI !== undefined;
|
|
75
|
+
const backendOption = readOption("backend");
|
|
76
|
+
const languageOption = hasOption("ts") ? "ts" : hasOption("js") ? "js" : null;
|
|
77
|
+
const structureOption = readOption("structure");
|
|
78
|
+
const requestedPackageManager = readOption("pm");
|
|
79
|
+
const templateOption = readOption("template");
|
|
80
|
+
const iconsOption = readOption("icons");
|
|
81
|
+
if (backendOption !== undefined && !["none", "basic", "express"].includes(backendOption)) {
|
|
82
|
+
optionErrors.push("--backend must be none, basic, or express");
|
|
83
|
+
}
|
|
84
|
+
if (hasOption("ts") && hasOption("js")) {
|
|
85
|
+
optionErrors.push("Use only one of --ts or --js");
|
|
86
|
+
}
|
|
87
|
+
if (hasOption("router") && hasOption("no-router")) {
|
|
88
|
+
optionErrors.push("Use only one of --router or --no-router");
|
|
89
|
+
}
|
|
90
|
+
if (structureOption !== undefined && !["simple", "module"].includes(structureOption)) {
|
|
91
|
+
optionErrors.push("--structure must be simple or module");
|
|
92
|
+
}
|
|
93
|
+
if (
|
|
94
|
+
requestedPackageManager !== undefined &&
|
|
95
|
+
!["npm", "pnpm"].includes(requestedPackageManager)
|
|
96
|
+
) {
|
|
97
|
+
optionErrors.push("--pm must be npm or pnpm");
|
|
98
|
+
}
|
|
99
|
+
if (noTailwind && hasOption("tailwind")) {
|
|
100
|
+
optionErrors.push("Use only one of --tailwind or --no-tailwind");
|
|
101
|
+
}
|
|
102
|
+
if (noAxios && hasOption("axios")) {
|
|
103
|
+
optionErrors.push("Use only one of --axios or --no-axios");
|
|
104
|
+
}
|
|
105
|
+
if (
|
|
106
|
+
iconsOption !== undefined &&
|
|
107
|
+
!["react-icons", "animateicons", "none"].includes(iconsOption)
|
|
108
|
+
) {
|
|
109
|
+
optionErrors.push("--icons must be react-icons, animateicons, or none");
|
|
110
|
+
}
|
|
111
|
+
if (args.some((argument) => argument.startsWith("--template=")) && !templateOption) {
|
|
112
|
+
optionErrors.push("--template must include a template name");
|
|
113
|
+
}
|
|
114
|
+
if (optionErrors.length) {
|
|
115
|
+
console.error(
|
|
116
|
+
`${optionErrors.join("\n")}\nAllowed options: ${allowedOptions.join(", ")}`,
|
|
117
|
+
);
|
|
118
|
+
process.exit(1);
|
|
119
|
+
}
|
|
120
|
+
let projectName = positionalArgs[0];
|
|
14
121
|
const validProjectName = (name) => /^[a-z0-9][a-z0-9_-]*$/.test(name);
|
|
15
122
|
|
|
16
123
|
if (!projectName && stdin.isTTY) {
|
|
@@ -35,10 +142,11 @@ if (!projectName || !validProjectName(projectName)) {
|
|
|
35
142
|
process.exit(1);
|
|
36
143
|
}
|
|
37
144
|
|
|
38
|
-
const packageManager =
|
|
39
|
-
|
|
40
|
-
: "npm";
|
|
145
|
+
const packageManager =
|
|
146
|
+
requestedPackageManager ??
|
|
147
|
+
(process.env.npm_config_user_agent?.startsWith("pnpm/") ? "pnpm" : "npm");
|
|
41
148
|
const root = resolve(process.cwd(), projectName);
|
|
149
|
+
let rootCreated = false;
|
|
42
150
|
const clientFolder = `${projectName}-client`;
|
|
43
151
|
const serverFolder = `${projectName}-server`;
|
|
44
152
|
const packageRoot = fileURLToPath(new URL("../", import.meta.url));
|
|
@@ -46,8 +154,94 @@ const scriptRoot = fileURLToPath(new URL("../scripts/", import.meta.url));
|
|
|
46
154
|
const cliPackage = JSON.parse(
|
|
47
155
|
await readFile(join(packageRoot, "package.json"), "utf8"),
|
|
48
156
|
);
|
|
157
|
+
const delay = (milliseconds) =>
|
|
158
|
+
new Promise((resolveDelay) => setTimeout(resolveDelay, milliseconds));
|
|
159
|
+
const removeIncompleteProject = async () => {
|
|
160
|
+
process.off("SIGINT", handleCreationSignal);
|
|
161
|
+
process.off("SIGTERM", handleCreationSignal);
|
|
162
|
+
if (!rootCreated) return;
|
|
163
|
+
rootCreated = false;
|
|
164
|
+
for (let attempt = 0; attempt < 6; attempt += 1) {
|
|
165
|
+
try {
|
|
166
|
+
await rm(root, { recursive: true, force: true });
|
|
167
|
+
return;
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (!["EBUSY", "ENOTEMPTY"].includes(error.code) || attempt === 5) {
|
|
170
|
+
console.error(`Unable to remove the incomplete project directory: ${error.message}`);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
await delay(100 * (attempt + 1));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
const handleCreationSignal = async () => {
|
|
178
|
+
if (creationCancelled) return;
|
|
179
|
+
creationCancelled = true;
|
|
180
|
+
const child = activeChild;
|
|
181
|
+
if (child && child.exitCode === null && child.signalCode === null) {
|
|
182
|
+
child.kill("SIGTERM");
|
|
183
|
+
await Promise.race([
|
|
184
|
+
new Promise((resolveClose) => child.once("close", resolveClose)),
|
|
185
|
+
delay(3000),
|
|
186
|
+
]);
|
|
187
|
+
if (child.exitCode === null && child.signalCode === null) {
|
|
188
|
+
child.kill("SIGKILL");
|
|
189
|
+
await new Promise((resolveClose) => child.once("close", resolveClose));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
await removeIncompleteProject();
|
|
193
|
+
process.exit(130);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
function parseSemver(version) {
|
|
197
|
+
const match =
|
|
198
|
+
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*))*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/.exec(version);
|
|
199
|
+
if (!match) throw new Error(`Invalid semantic version: ${version}`);
|
|
200
|
+
const prerelease = match[4]?.split(".") ?? [];
|
|
201
|
+
return {
|
|
202
|
+
major: BigInt(match[1]),
|
|
203
|
+
minor: BigInt(match[2]),
|
|
204
|
+
patch: BigInt(match[3]),
|
|
205
|
+
prerelease,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function compareSemver(left, right) {
|
|
210
|
+
const a = parseSemver(left);
|
|
211
|
+
const b = parseSemver(right);
|
|
212
|
+
for (const component of ["major", "minor", "patch"]) {
|
|
213
|
+
if (a[component] !== b[component]) {
|
|
214
|
+
return a[component] < b[component] ? -1 : 1;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (!a.prerelease.length || !b.prerelease.length) {
|
|
218
|
+
return a.prerelease.length === b.prerelease.length
|
|
219
|
+
? 0
|
|
220
|
+
: a.prerelease.length
|
|
221
|
+
? -1
|
|
222
|
+
: 1;
|
|
223
|
+
}
|
|
224
|
+
const length = Math.max(a.prerelease.length, b.prerelease.length);
|
|
225
|
+
for (let index = 0; index < length; index += 1) {
|
|
226
|
+
const leftId = a.prerelease[index];
|
|
227
|
+
const rightId = b.prerelease[index];
|
|
228
|
+
if (leftId === undefined || rightId === undefined) {
|
|
229
|
+
return leftId === rightId ? 0 : leftId === undefined ? -1 : 1;
|
|
230
|
+
}
|
|
231
|
+
if (leftId === rightId) continue;
|
|
232
|
+
const leftNumeric = /^\d+$/.test(leftId);
|
|
233
|
+
const rightNumeric = /^\d+$/.test(rightId);
|
|
234
|
+
if (leftNumeric && rightNumeric) {
|
|
235
|
+
return BigInt(leftId) < BigInt(rightId) ? -1 : 1;
|
|
236
|
+
}
|
|
237
|
+
if (leftNumeric !== rightNumeric) return leftNumeric ? -1 : 1;
|
|
238
|
+
return leftId < rightId ? -1 : 1;
|
|
239
|
+
}
|
|
240
|
+
return 0;
|
|
241
|
+
}
|
|
49
242
|
|
|
50
243
|
async function updateIfOutdated() {
|
|
244
|
+
if (skipUpdateCheck) return;
|
|
51
245
|
const { version: installedVersion } = cliPackage;
|
|
52
246
|
const registry = (process.env.npm_config_registry || "https://registry.npmjs.org")
|
|
53
247
|
.replace(/\/$/, "");
|
|
@@ -67,7 +261,12 @@ async function updateIfOutdated() {
|
|
|
67
261
|
return;
|
|
68
262
|
}
|
|
69
263
|
|
|
70
|
-
|
|
264
|
+
try {
|
|
265
|
+
if (compareSemver(latestVersion, installedVersion) <= 0) return;
|
|
266
|
+
} catch (error) {
|
|
267
|
+
console.warn(`\nCould not compare CLI versions: ${error.message}`);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
71
270
|
if (!stdin.isTTY) {
|
|
72
271
|
console.warn(
|
|
73
272
|
`\nA newer version of create-vanilla-vite is available (${latestVersion}; current: ${installedVersion}). Run ${packageManager} create vanilla-vite@latest to use it.`,
|
|
@@ -84,16 +283,21 @@ async function updateIfOutdated() {
|
|
|
84
283
|
]);
|
|
85
284
|
if (!useLatest) return;
|
|
86
285
|
|
|
286
|
+
const relaunchArgs = [
|
|
287
|
+
...args.filter((arg) => arg !== "--no-update-check"),
|
|
288
|
+
...(args.some((arg) => !arg.startsWith("--")) ? [] : [projectName]),
|
|
289
|
+
"--no-update-check",
|
|
290
|
+
];
|
|
87
291
|
const updateArgs =
|
|
88
292
|
packageManager === "pnpm"
|
|
89
|
-
? ["dlx", "create-vanilla-vite@latest", ...
|
|
293
|
+
? ["dlx", "create-vanilla-vite@latest", ...relaunchArgs]
|
|
90
294
|
: [
|
|
91
295
|
"exec",
|
|
92
296
|
"--yes",
|
|
93
297
|
"--package=create-vanilla-vite@latest",
|
|
94
298
|
"--",
|
|
95
299
|
"create-vanilla-vite",
|
|
96
|
-
...
|
|
300
|
+
...relaunchArgs,
|
|
97
301
|
];
|
|
98
302
|
try {
|
|
99
303
|
await run(packageManager, updateArgs, process.cwd());
|
|
@@ -107,8 +311,13 @@ async function updateIfOutdated() {
|
|
|
107
311
|
function run(command, commandArgs, cwd, env = process.env) {
|
|
108
312
|
return new Promise((resolveRun, rejectRun) => {
|
|
109
313
|
const child = spawn(command, commandArgs, { cwd, stdio: "inherit", env });
|
|
110
|
-
child
|
|
314
|
+
activeChild = child;
|
|
315
|
+
child.once("error", (error) => {
|
|
316
|
+
if (activeChild === child) activeChild = undefined;
|
|
317
|
+
rejectRun(error);
|
|
318
|
+
});
|
|
111
319
|
child.once("close", (code, signal) => {
|
|
320
|
+
if (activeChild === child) activeChild = undefined;
|
|
112
321
|
if (code === 0) resolveRun();
|
|
113
322
|
else {
|
|
114
323
|
rejectRun(
|
|
@@ -152,8 +361,14 @@ async function selectOption(question, options, defaultIndex = 0, color = "cyan")
|
|
|
152
361
|
options.length;
|
|
153
362
|
render();
|
|
154
363
|
} else if (key?.name === "return") finish(options[selected].value);
|
|
155
|
-
else if (key?.name === "escape"
|
|
364
|
+
else if (key?.name === "escape") {
|
|
156
365
|
finish(options[defaultIndex].value);
|
|
366
|
+
} else if (key?.ctrl && key.name === "c") {
|
|
367
|
+
stdin.off("keypress", onKeypress);
|
|
368
|
+
stdin.setRawMode(false);
|
|
369
|
+
stdin.pause();
|
|
370
|
+
stdout.write(`\x1b[${options.length + 2}B\n`);
|
|
371
|
+
void handleCreationSignal();
|
|
157
372
|
}
|
|
158
373
|
}
|
|
159
374
|
|
|
@@ -164,12 +379,27 @@ async function selectOption(question, options, defaultIndex = 0, color = "cyan")
|
|
|
164
379
|
});
|
|
165
380
|
}
|
|
166
381
|
|
|
382
|
+
function chooseOption(question, options, defaultIndex = 0, color = "cyan") {
|
|
383
|
+
return yes
|
|
384
|
+
? Promise.resolve(options[defaultIndex].value)
|
|
385
|
+
: selectOption(question, options, defaultIndex, color);
|
|
386
|
+
}
|
|
387
|
+
|
|
167
388
|
async function configureTailwind(clientRoot) {
|
|
168
|
-
|
|
389
|
+
let viteConfigName = (await readdir(clientRoot)).find((file) =>
|
|
169
390
|
/^vite\.config\.(?:[cm]?[jt]s)$/.test(file),
|
|
170
391
|
);
|
|
171
392
|
if (!viteConfigName) {
|
|
172
|
-
|
|
393
|
+
const isTypeScript = (await readdir(clientRoot)).includes("tsconfig.json");
|
|
394
|
+
viteConfigName = `vite.config.${isTypeScript ? "ts" : "js"}`;
|
|
395
|
+
await writeFile(
|
|
396
|
+
join(clientRoot, viteConfigName),
|
|
397
|
+
'import { defineConfig } from "vite";\n' +
|
|
398
|
+
'import tailwindcss from "@tailwindcss/vite";\n\n' +
|
|
399
|
+
"export default defineConfig({\n" +
|
|
400
|
+
" plugins: [tailwindcss()],\n" +
|
|
401
|
+
"});\n",
|
|
402
|
+
);
|
|
173
403
|
}
|
|
174
404
|
|
|
175
405
|
const viteConfigPath = join(clientRoot, viteConfigName);
|
|
@@ -208,9 +438,12 @@ async function configureTailwind(clientRoot) {
|
|
|
208
438
|
throw new Error("Could not find the frontend entry file to configure Tailwind CSS.");
|
|
209
439
|
}
|
|
210
440
|
const entry = await readFile(entryPath, "utf8");
|
|
211
|
-
|
|
441
|
+
let cssImport = entry.match(/import\s+["']([^"']+\.css)["']/);
|
|
212
442
|
if (!cssImport) {
|
|
213
|
-
|
|
443
|
+
const cssPath = join(dirname(entryPath), "style.css");
|
|
444
|
+
await writeFile(cssPath, '@import "tailwindcss";\n');
|
|
445
|
+
await writeFile(entryPath, `import "./style.css";\n${entry}`);
|
|
446
|
+
cssImport = ['import "./style.css";', "./style.css"];
|
|
214
447
|
}
|
|
215
448
|
const cssPath = resolve(dirname(entryPath), cssImport[1]);
|
|
216
449
|
const css = await readFile(cssPath, "utf8");
|
|
@@ -219,6 +452,38 @@ async function configureTailwind(clientRoot) {
|
|
|
219
452
|
}
|
|
220
453
|
}
|
|
221
454
|
|
|
455
|
+
async function configureStrictPort(clientRoot) {
|
|
456
|
+
let configName = (await readdir(clientRoot)).find((file) =>
|
|
457
|
+
/^vite\.config\.(?:[cm]?[jt]s)$/.test(file),
|
|
458
|
+
);
|
|
459
|
+
if (!configName) {
|
|
460
|
+
const isTypeScript = (await readdir(clientRoot)).includes("tsconfig.json");
|
|
461
|
+
configName = `vite.config.${isTypeScript ? "ts" : "js"}`;
|
|
462
|
+
await writeFile(
|
|
463
|
+
join(clientRoot, configName),
|
|
464
|
+
'import { defineConfig } from "vite";\n\nexport default defineConfig({});\n',
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const configPath = join(clientRoot, configName);
|
|
469
|
+
let config = await readFile(configPath, "utf8");
|
|
470
|
+
if (/\bstrictPort\s*:/.test(config)) {
|
|
471
|
+
config = config.replace(/\bstrictPort\s*:\s*(?:true|false)/, "strictPort: true");
|
|
472
|
+
} else if (/\bserver\s*:\s*\{/.test(config)) {
|
|
473
|
+
config = config.replace(/\bserver\s*:\s*\{/, "server: { strictPort: true,");
|
|
474
|
+
} else {
|
|
475
|
+
const configObject = /(\bdefineConfig\s*\(\s*\{|\bexport\s+default\s*\{)/;
|
|
476
|
+
if (!configObject.test(config)) {
|
|
477
|
+
console.warn(
|
|
478
|
+
`Could not configure strictPort in ${configName}; the Vite config shape was not recognized.`,
|
|
479
|
+
);
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
config = config.replace(configObject, "$1\n server: { strictPort: true },");
|
|
483
|
+
}
|
|
484
|
+
await writeFile(configPath, config);
|
|
485
|
+
}
|
|
486
|
+
|
|
222
487
|
async function scaffoldReactRouter(clientRoot, useTailwind) {
|
|
223
488
|
const sourceRoot = join(clientRoot, "src");
|
|
224
489
|
const sourceFiles = await readdir(sourceRoot);
|
|
@@ -255,7 +520,7 @@ async function scaffoldReactRouter(clientRoot, useTailwind) {
|
|
|
255
520
|
navActive: "bg-[#e7eee8] text-[#244b39]",
|
|
256
521
|
page: "flex-1",
|
|
257
522
|
footer: "py-6 border-t border-[#dfe6df] text-[13px] text-[#78857d]",
|
|
258
|
-
home: "py-[72px] sm:
|
|
523
|
+
home: "py-[72px] sm:pt-[clamp(72px,12vw,132px)] sm:pb-[84px]",
|
|
259
524
|
eyebrow: "text-xs font-bold tracking-[0.12em] text-[#557663]",
|
|
260
525
|
title: "max-w-[680px] my-[18px] text-[clamp(40px,7vw,68px)] font-semibold leading-[1.05] tracking-[-0.055em]",
|
|
261
526
|
authTitle: "max-w-[680px] my-[18px] text-[clamp(38px,7vw,52px)] font-semibold leading-[1.05] tracking-[-0.055em]",
|
|
@@ -338,6 +603,7 @@ const MainRouter = createBrowserRouter([
|
|
|
338
603
|
},
|
|
339
604
|
]);
|
|
340
605
|
|
|
606
|
+
// eslint-disable-next-line react-refresh/only-export-components
|
|
341
607
|
export default MainRouter;
|
|
342
608
|
`,
|
|
343
609
|
),
|
|
@@ -380,7 +646,7 @@ export default App;
|
|
|
380
646
|
|
|
381
647
|
function Home() {
|
|
382
648
|
return (
|
|
383
|
-
<section className="${styles.
|
|
649
|
+
<section className="${styles.home}">
|
|
384
650
|
<p className="${styles.eyebrow}">YOUR PROJECT STARTS HERE</p>
|
|
385
651
|
<h1 className="${styles.title}">A faster start for your next project.</h1>
|
|
386
652
|
<p className="${styles.copy}">
|
|
@@ -478,11 +744,11 @@ a {
|
|
|
478
744
|
}
|
|
479
745
|
`,
|
|
480
746
|
),
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
747
|
+
...(!useTailwind
|
|
748
|
+
? [
|
|
749
|
+
writeFile(
|
|
750
|
+
join(sourceRoot, "App.css"),
|
|
751
|
+
`.app-shell {
|
|
486
752
|
min-height: 100vh;
|
|
487
753
|
display: flex;
|
|
488
754
|
flex-direction: column;
|
|
@@ -728,7 +994,9 @@ a {
|
|
|
728
994
|
}
|
|
729
995
|
}
|
|
730
996
|
`,
|
|
731
|
-
|
|
997
|
+
),
|
|
998
|
+
]
|
|
999
|
+
: []),
|
|
732
1000
|
writeFile(
|
|
733
1001
|
entryPath,
|
|
734
1002
|
`import { StrictMode } from "react";
|
|
@@ -749,46 +1017,218 @@ createRoot(document.getElementById("root")${typed ? "!" : ""}).render(
|
|
|
749
1017
|
return true;
|
|
750
1018
|
}
|
|
751
1019
|
|
|
1020
|
+
async function scaffoldBackendStatus(clientRoot) {
|
|
1021
|
+
const indexPath = join(clientRoot, "index.html");
|
|
1022
|
+
const html = await readFile(indexPath, "utf8");
|
|
1023
|
+
if (!/<\/body\s*>/i.test(html)) {
|
|
1024
|
+
throw new Error("Could not find the closing body tag for the backend status control.");
|
|
1025
|
+
}
|
|
1026
|
+
await writeFile(
|
|
1027
|
+
indexPath,
|
|
1028
|
+
html.replace(
|
|
1029
|
+
/<\/body\s*>/i,
|
|
1030
|
+
' <script type="module" src="/backend-status.js"></script>\n</body>',
|
|
1031
|
+
),
|
|
1032
|
+
);
|
|
1033
|
+
await writeFile(
|
|
1034
|
+
join(clientRoot, "backend-status.js"),
|
|
1035
|
+
`if (import.meta.env.DEV) {
|
|
1036
|
+
import("./backend-status.css");
|
|
1037
|
+
const apiUrl = (import.meta.env.VITE_API_URL || "http://localhost:4000").replace(/\\/+$/, "");
|
|
1038
|
+
const panel = document.createElement("aside");
|
|
1039
|
+
panel.className = "backend-status";
|
|
1040
|
+
panel.setAttribute("aria-label", "Backend connection");
|
|
1041
|
+
panel.innerHTML = \`
|
|
1042
|
+
<span class="backend-status__dot" aria-hidden="true"></span>
|
|
1043
|
+
<div class="backend-status__content">
|
|
1044
|
+
<strong class="backend-status__title">Backend</strong>
|
|
1045
|
+
<span class="backend-status__message" role="status" aria-live="polite">Not checked yet</span>
|
|
1046
|
+
</div>
|
|
1047
|
+
<button class="backend-status__button" type="button">Check connection</button>
|
|
1048
|
+
\`;
|
|
1049
|
+
|
|
1050
|
+
const message = panel.querySelector(".backend-status__message");
|
|
1051
|
+
const button = panel.querySelector(".backend-status__button");
|
|
1052
|
+
button.addEventListener("click", async () => {
|
|
1053
|
+
button.disabled = true;
|
|
1054
|
+
panel.dataset.state = "checking";
|
|
1055
|
+
message.textContent = "Checking…";
|
|
1056
|
+
const controller = new AbortController();
|
|
1057
|
+
const timeout = setTimeout(() => controller.abort(), 5000);
|
|
1058
|
+
try {
|
|
1059
|
+
const response = await fetch(\`\${apiUrl}/api/health\`, {
|
|
1060
|
+
headers: { Accept: "application/json" },
|
|
1061
|
+
signal: controller.signal,
|
|
1062
|
+
});
|
|
1063
|
+
const result = await response.json();
|
|
1064
|
+
if (!response.ok || result.status !== "ok") {
|
|
1065
|
+
throw new Error("Backend health check returned an unexpected response.");
|
|
1066
|
+
}
|
|
1067
|
+
panel.dataset.state = "connected";
|
|
1068
|
+
message.textContent = "Connected";
|
|
1069
|
+
} catch {
|
|
1070
|
+
panel.dataset.state = "disconnected";
|
|
1071
|
+
message.textContent = "Unavailable — check the server and network";
|
|
1072
|
+
} finally {
|
|
1073
|
+
clearTimeout(timeout);
|
|
1074
|
+
button.disabled = false;
|
|
1075
|
+
}
|
|
1076
|
+
});
|
|
1077
|
+
|
|
1078
|
+
document.body.append(panel);
|
|
1079
|
+
}
|
|
1080
|
+
`,
|
|
1081
|
+
);
|
|
1082
|
+
await writeFile(
|
|
1083
|
+
join(clientRoot, "backend-status.css"),
|
|
1084
|
+
`.backend-status {
|
|
1085
|
+
position: fixed;
|
|
1086
|
+
z-index: 1000;
|
|
1087
|
+
right: 18px;
|
|
1088
|
+
bottom: 18px;
|
|
1089
|
+
display: grid;
|
|
1090
|
+
grid-template-columns: 9px minmax(0, 1fr) auto;
|
|
1091
|
+
align-items: center;
|
|
1092
|
+
gap: 12px;
|
|
1093
|
+
width: min(390px, calc(100vw - 36px));
|
|
1094
|
+
padding: 14px;
|
|
1095
|
+
border: 1px solid #dfe6df;
|
|
1096
|
+
border-radius: 12px;
|
|
1097
|
+
background: #fbfcfa;
|
|
1098
|
+
color: #17231f;
|
|
1099
|
+
box-shadow: 0 8px 28px rgb(23 35 31 / 12%);
|
|
1100
|
+
font: 13px/1.4 Inter, ui-sans-serif, system-ui, sans-serif;
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
.backend-status__dot {
|
|
1104
|
+
width: 9px;
|
|
1105
|
+
height: 9px;
|
|
1106
|
+
border-radius: 50%;
|
|
1107
|
+
background: #9aa59e;
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
.backend-status[data-state="checking"] .backend-status__dot {
|
|
1111
|
+
background: #c28a30;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
.backend-status[data-state="connected"] .backend-status__dot {
|
|
1115
|
+
background: #31835b;
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
.backend-status[data-state="disconnected"] .backend-status__dot {
|
|
1119
|
+
background: #bd4d43;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
.backend-status__content {
|
|
1123
|
+
display: grid;
|
|
1124
|
+
gap: 2px;
|
|
1125
|
+
min-width: 0;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
.backend-status__title {
|
|
1129
|
+
font-size: 13px;
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
.backend-status__message {
|
|
1133
|
+
color: #627068;
|
|
1134
|
+
font-size: 12px;
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
.backend-status__button {
|
|
1138
|
+
min-height: 36px;
|
|
1139
|
+
padding: 0 12px;
|
|
1140
|
+
border: 0;
|
|
1141
|
+
border-radius: 8px;
|
|
1142
|
+
background: #315d4a;
|
|
1143
|
+
color: #fff;
|
|
1144
|
+
font: inherit;
|
|
1145
|
+
font-weight: 600;
|
|
1146
|
+
cursor: pointer;
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
.backend-status__button:hover:not(:disabled) {
|
|
1150
|
+
background: #244b39;
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
.backend-status__button:focus-visible {
|
|
1154
|
+
outline: 3px solid #9db9a5;
|
|
1155
|
+
outline-offset: 2px;
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
.backend-status__button:disabled {
|
|
1159
|
+
cursor: wait;
|
|
1160
|
+
opacity: 0.7;
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
@media (max-width: 420px) {
|
|
1164
|
+
.backend-status {
|
|
1165
|
+
right: 12px;
|
|
1166
|
+
bottom: 12px;
|
|
1167
|
+
width: calc(100vw - 24px);
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
`,
|
|
1171
|
+
);
|
|
1172
|
+
}
|
|
1173
|
+
|
|
752
1174
|
async function configureFrontend(clientRoot, clientPackage) {
|
|
753
|
-
const tailwind =
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
1175
|
+
const tailwind = noTailwind
|
|
1176
|
+
? false
|
|
1177
|
+
: hasOption("tailwind")
|
|
1178
|
+
? true
|
|
1179
|
+
: await chooseOption("Would you like to install Tailwind CSS?", [
|
|
1180
|
+
{ label: "Yes — add Tailwind CSS v4", value: true },
|
|
1181
|
+
{ label: "No", value: false },
|
|
1182
|
+
]);
|
|
1183
|
+
const axios = hasOption("axios")
|
|
1184
|
+
? true
|
|
1185
|
+
: noAxios
|
|
1186
|
+
? false
|
|
1187
|
+
: await chooseOption("Would you like to install Axios?", [
|
|
1188
|
+
{ label: "Yes — add Axios", value: true },
|
|
1189
|
+
{ label: "No", value: false },
|
|
1190
|
+
]);
|
|
761
1191
|
const isReact = Boolean(
|
|
762
1192
|
clientPackage.dependencies?.react || clientPackage.devDependencies?.react,
|
|
763
1193
|
);
|
|
764
1194
|
const installRouter =
|
|
765
1195
|
isReact &&
|
|
766
|
-
(
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
]
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
1196
|
+
(hasOption("router")
|
|
1197
|
+
? true
|
|
1198
|
+
: hasOption("no-router")
|
|
1199
|
+
? false
|
|
1200
|
+
: await chooseOption("Would you like to install React Router?", [
|
|
1201
|
+
{ label: "Yes — add Home and Login pages with navigation", value: true },
|
|
1202
|
+
{ label: "No", value: false },
|
|
1203
|
+
]));
|
|
1204
|
+
const iconPackages = {
|
|
1205
|
+
"react-icons": ["react-icons", "^5.7.0"],
|
|
1206
|
+
animateicons: ["@animateicons/react", "^0.13.0"],
|
|
1207
|
+
};
|
|
1208
|
+
const icons =
|
|
1209
|
+
!isReact || iconsOption === "none"
|
|
1210
|
+
? null
|
|
1211
|
+
: iconsOption
|
|
1212
|
+
? iconPackages[iconsOption]
|
|
1213
|
+
: (await chooseOption("Would you like to install an icon library?", [
|
|
1214
|
+
{ label: "Yes", value: true },
|
|
1215
|
+
{ label: "No", value: false },
|
|
1216
|
+
]))
|
|
1217
|
+
? await chooseOption("Which React icon library would you like to install?", [
|
|
1218
|
+
{ label: "React Icons (default)", value: iconPackages["react-icons"] },
|
|
1219
|
+
{ label: "@animateicons/react", value: iconPackages.animateicons },
|
|
1220
|
+
])
|
|
1221
|
+
: null;
|
|
782
1222
|
|
|
783
1223
|
if (tailwind) {
|
|
784
1224
|
clientPackage.devDependencies ??= {};
|
|
785
|
-
clientPackage.devDependencies["@tailwindcss/vite"] = "^4.3.
|
|
786
|
-
clientPackage.devDependencies.tailwindcss = "^4.3.
|
|
1225
|
+
clientPackage.devDependencies["@tailwindcss/vite"] = "^4.3.5";
|
|
1226
|
+
clientPackage.devDependencies.tailwindcss = "^4.3.5";
|
|
787
1227
|
await configureTailwind(clientRoot);
|
|
788
1228
|
}
|
|
789
1229
|
if (axios) {
|
|
790
1230
|
clientPackage.dependencies ??= {};
|
|
791
|
-
clientPackage.dependencies.axios = "^1.21.
|
|
1231
|
+
clientPackage.dependencies.axios = "^1.21.1";
|
|
792
1232
|
}
|
|
793
1233
|
if (icons) {
|
|
794
1234
|
clientPackage.dependencies ??= {};
|
|
@@ -799,6 +1239,7 @@ async function configureFrontend(clientRoot, clientPackage) {
|
|
|
799
1239
|
clientPackage.dependencies["react-router"] = "^7";
|
|
800
1240
|
await scaffoldReactRouter(clientRoot, tailwind);
|
|
801
1241
|
}
|
|
1242
|
+
await configureStrictPort(clientRoot);
|
|
802
1243
|
}
|
|
803
1244
|
|
|
804
1245
|
async function copyScript(name) {
|
|
@@ -806,111 +1247,206 @@ async function copyScript(name) {
|
|
|
806
1247
|
await writeFile(join(root, "scripts", name), source);
|
|
807
1248
|
}
|
|
808
1249
|
|
|
1250
|
+
function expressErrorHandlerParameters(language) {
|
|
1251
|
+
return language === "ts"
|
|
1252
|
+
? `error: unknown,
|
|
1253
|
+
_request: import("express").Request,
|
|
1254
|
+
response: import("express").Response,
|
|
1255
|
+
_next: import("express").NextFunction`
|
|
1256
|
+
: "error, _request, response, _next";
|
|
1257
|
+
}
|
|
1258
|
+
|
|
809
1259
|
function backendFiles(language, framework) {
|
|
810
1260
|
const extension = language;
|
|
811
1261
|
const typeAnnotation = language === "ts" ? ": number" : "";
|
|
812
|
-
const
|
|
813
|
-
?
|
|
814
|
-
|
|
815
|
-
: "request: import('node:http').IncomingMessage"
|
|
816
|
-
: "request";
|
|
817
|
-
const response = language === "ts"
|
|
818
|
-
? framework === "express"
|
|
819
|
-
? "response: import('express').Response"
|
|
820
|
-
: "response: import('node:http').ServerResponse"
|
|
821
|
-
: "response";
|
|
822
|
-
const app =
|
|
823
|
-
framework === "express"
|
|
824
|
-
? `import express from "express";
|
|
1262
|
+
const app = framework === "express"
|
|
1263
|
+
? `import cors from "cors";
|
|
1264
|
+
import express from "express";
|
|
825
1265
|
|
|
826
1266
|
export function createApp() {
|
|
827
1267
|
const app = express();
|
|
1268
|
+
const origins = (process.env.CORS_ORIGIN ?? "*")
|
|
1269
|
+
.split(",")
|
|
1270
|
+
.map((origin) => origin.trim());
|
|
1271
|
+
app.use((request, response, next) => {
|
|
1272
|
+
const origin = request.headers.origin;
|
|
1273
|
+
if (origin && !origins.includes("*") && origins.includes(origin)) {
|
|
1274
|
+
response.vary("Origin");
|
|
1275
|
+
}
|
|
1276
|
+
next();
|
|
1277
|
+
});
|
|
1278
|
+
app.use(cors({ origin: origins.includes("*") ? "*" : origins }));
|
|
828
1279
|
app.use(express.json());
|
|
829
1280
|
app.get("/", (_request, response) => response.json({ message: "Backend is running" }));
|
|
830
1281
|
app.get("/api/health", (_request, response) => response.json({ status: "ok" }));
|
|
1282
|
+
app.use((${expressErrorHandlerParameters(language)}) => {
|
|
1283
|
+
console.error(error);
|
|
1284
|
+
response.status(500).json({ error: "Internal Server Error" });
|
|
1285
|
+
});
|
|
831
1286
|
return app;
|
|
832
1287
|
}
|
|
833
1288
|
`
|
|
834
1289
|
: `import { createServer } from "node:http";
|
|
1290
|
+
import { handleRequest } from "./basicHandler.${extension}";
|
|
835
1291
|
|
|
836
1292
|
export function createApp() {
|
|
837
|
-
return createServer(
|
|
838
|
-
response.writeHead(200, { "content-type": "application/json" });
|
|
839
|
-
response.end(JSON.stringify({ message: "Backend is running" }));
|
|
840
|
-
});
|
|
1293
|
+
return createServer(handleRequest);
|
|
841
1294
|
}
|
|
842
1295
|
`;
|
|
843
|
-
const
|
|
1296
|
+
const basicHandler =
|
|
1297
|
+
framework === "basic"
|
|
1298
|
+
? `export function handleRequest(
|
|
1299
|
+
request${language === "ts" ? ": import('node:http').IncomingMessage" : ""},
|
|
1300
|
+
response${language === "ts" ? ": import('node:http').ServerResponse" : ""},
|
|
1301
|
+
) {
|
|
1302
|
+
const origins = (process.env.CORS_ORIGIN ?? "*")
|
|
1303
|
+
.split(",")
|
|
1304
|
+
.map((origin) => origin.trim());
|
|
1305
|
+
const requestOrigin = request.headers.origin;
|
|
1306
|
+
const allowedOrigin = origins.includes("*")
|
|
1307
|
+
? "*"
|
|
1308
|
+
: typeof requestOrigin === "string" && origins.includes(requestOrigin)
|
|
1309
|
+
? requestOrigin
|
|
1310
|
+
: undefined;
|
|
1311
|
+
const corsHeaders = {
|
|
1312
|
+
...(allowedOrigin ? { "access-control-allow-origin": allowedOrigin } : {}),
|
|
1313
|
+
...(allowedOrigin && allowedOrigin !== "*" ? { vary: "Origin" } : {}),
|
|
1314
|
+
"access-control-allow-methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
|
|
1315
|
+
"access-control-allow-headers": "Content-Type, Authorization",
|
|
1316
|
+
};
|
|
1317
|
+
|
|
1318
|
+
try {
|
|
1319
|
+
if (request.method === "OPTIONS") {
|
|
1320
|
+
response.writeHead(204, corsHeaders);
|
|
1321
|
+
response.end();
|
|
1322
|
+
return;
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
const path = new URL(request.url ?? "/", "http://localhost").pathname;
|
|
1326
|
+
const payload =
|
|
1327
|
+
request.method === "GET" && path === "/"
|
|
1328
|
+
? { status: 200, body: { message: "Backend is running" } }
|
|
1329
|
+
: request.method === "GET" && path === "/api/health"
|
|
1330
|
+
? { status: 200, body: { status: "ok" } }
|
|
1331
|
+
: { status: 404, body: { message: "Not Found" } };
|
|
1332
|
+
|
|
1333
|
+
response.writeHead(payload.status, {
|
|
1334
|
+
...corsHeaders,
|
|
1335
|
+
"content-type": "application/json",
|
|
1336
|
+
});
|
|
1337
|
+
response.end(JSON.stringify(payload.body));
|
|
1338
|
+
} catch (error) {
|
|
1339
|
+
console.error(error);
|
|
1340
|
+
response.writeHead(500, {
|
|
1341
|
+
...corsHeaders,
|
|
1342
|
+
"content-type": "application/json",
|
|
1343
|
+
});
|
|
1344
|
+
response.end(JSON.stringify({ error: "Internal Server Error" }));
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
`
|
|
1348
|
+
: null;
|
|
1349
|
+
const server = `import "dotenv/config";
|
|
1350
|
+
import { createApp } from "./app.${extension}";
|
|
844
1351
|
|
|
845
1352
|
const port${typeAnnotation} = Number(process.env.PORT ?? 4000);
|
|
1353
|
+
const host = process.env.HOST ?? "localhost";
|
|
846
1354
|
const app = createApp();
|
|
847
|
-
app.listen(port, () => {
|
|
848
|
-
console.log(\`Backend listening at http
|
|
1355
|
+
const server = app.listen(port, host, () => {
|
|
1356
|
+
console.log(\`Backend listening at http://\${host}:\${port}\`);
|
|
849
1357
|
});
|
|
1358
|
+
for (const signal of ["SIGINT", "SIGTERM"]) {
|
|
1359
|
+
process.once(signal, () => {
|
|
1360
|
+
server.close((error) => {
|
|
1361
|
+
if (error) {
|
|
1362
|
+
console.error("Unable to close the backend server:", error);
|
|
1363
|
+
process.exitCode = 1;
|
|
1364
|
+
}
|
|
1365
|
+
});
|
|
1366
|
+
});
|
|
1367
|
+
}
|
|
850
1368
|
`;
|
|
851
|
-
return { app, server };
|
|
1369
|
+
return { app, server, basicHandler };
|
|
852
1370
|
}
|
|
853
1371
|
|
|
854
1372
|
async function createBackend(serverRoot) {
|
|
855
|
-
const language =
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
1373
|
+
const language =
|
|
1374
|
+
languageOption ??
|
|
1375
|
+
(await chooseOption("Select a language for the backend:", [
|
|
1376
|
+
{ label: "JavaScript", value: "js" },
|
|
1377
|
+
{ label: "TypeScript", value: "ts" },
|
|
1378
|
+
], 0, "yellow"));
|
|
1379
|
+
const framework =
|
|
1380
|
+
backendOption && backendOption !== "none"
|
|
1381
|
+
? backendOption
|
|
1382
|
+
: await chooseOption("Select a backend framework:", [
|
|
1383
|
+
{ label: "Basic Node.js — no additional framework", value: "basic" },
|
|
1384
|
+
{ label: "Express — install Express, CORS, and dotenv (default)", value: "express" },
|
|
1385
|
+
], 1, "yellow");
|
|
1386
|
+
const structure = structureOption
|
|
1387
|
+
? structureOption
|
|
1388
|
+
: useDefaultStructure
|
|
1389
|
+
? "simple"
|
|
1390
|
+
: await chooseOption("Select a backend folder structure:", [
|
|
1391
|
+
{ label: "Simple — standard folders", value: "simple" },
|
|
1392
|
+
{ label: "Module-based — organized by feature (default)", value: "module" },
|
|
1393
|
+
], 1, "yellow");
|
|
869
1394
|
const typed = language === "ts";
|
|
870
|
-
let { app, server } = backendFiles(language, framework);
|
|
1395
|
+
let { app, server, basicHandler } = backendFiles(language, framework);
|
|
871
1396
|
const sourceRoot = join(serverRoot, "src");
|
|
872
1397
|
const folders =
|
|
873
1398
|
structure === "simple"
|
|
874
1399
|
? ["routes", "controllers", "middleware", "config"]
|
|
875
1400
|
: ["module", "config", "middleware", "utils"];
|
|
876
1401
|
for (const folder of folders) {
|
|
877
|
-
|
|
1402
|
+
const folderPath = join(sourceRoot, folder);
|
|
1403
|
+
await mkdir(folderPath, { recursive: true });
|
|
1404
|
+
await writeFile(join(folderPath, ".gitkeep"), "");
|
|
878
1405
|
}
|
|
879
1406
|
if (structure === "module") {
|
|
880
|
-
const request = typed
|
|
881
|
-
? framework === "express"
|
|
882
|
-
? "request: import('express').Request"
|
|
883
|
-
: "request: import('node:http').IncomingMessage"
|
|
884
|
-
: "request";
|
|
885
|
-
const response = typed
|
|
886
|
-
? framework === "express"
|
|
887
|
-
? "response: import('express').Response"
|
|
888
|
-
: "response: import('node:http').ServerResponse"
|
|
889
|
-
: "response";
|
|
890
1407
|
const router =
|
|
891
1408
|
framework === "express"
|
|
892
1409
|
? `import { Router } from "express";
|
|
893
1410
|
|
|
894
1411
|
export const mainRouter = Router();
|
|
1412
|
+
mainRouter.get("/", (_request, response) => {
|
|
1413
|
+
response.json({ message: "Backend is running" });
|
|
1414
|
+
});
|
|
895
1415
|
mainRouter.get("/api/health", (_request, response) => {
|
|
896
1416
|
response.json({ status: "ok" });
|
|
897
1417
|
});
|
|
898
1418
|
`
|
|
899
|
-
: `export
|
|
900
|
-
response.writeHead(200, { "content-type": "application/json" });
|
|
901
|
-
response.end(JSON.stringify({ message: "Backend is running" }));
|
|
902
|
-
}
|
|
1419
|
+
: `export { handleRequest } from "./basicHandler.${language}";
|
|
903
1420
|
`;
|
|
904
1421
|
await writeFile(join(sourceRoot, `mainRouter.${language}`), router);
|
|
905
1422
|
app =
|
|
906
1423
|
framework === "express"
|
|
907
|
-
? `import
|
|
1424
|
+
? `import cors from "cors";
|
|
1425
|
+
import express from "express";
|
|
908
1426
|
import { mainRouter } from "./mainRouter.${language}";
|
|
909
1427
|
|
|
910
1428
|
export function createApp() {
|
|
911
1429
|
const app = express();
|
|
1430
|
+
const origins = (process.env.CORS_ORIGIN ?? "*")
|
|
1431
|
+
.split(",")
|
|
1432
|
+
.map((origin) => origin.trim());
|
|
1433
|
+
app.use((request, response, next) => {
|
|
1434
|
+
const origin = request.headers.origin;
|
|
1435
|
+
if (origin && !origins.includes("*") && origins.includes(origin)) {
|
|
1436
|
+
response.vary("Origin");
|
|
1437
|
+
}
|
|
1438
|
+
next();
|
|
1439
|
+
});
|
|
1440
|
+
app.use(cors({ origin: origins.includes("*") ? "*" : origins }));
|
|
912
1441
|
app.use(express.json());
|
|
913
1442
|
app.use(mainRouter);
|
|
1443
|
+
app.use((_request, response) => {
|
|
1444
|
+
response.status(404).json({ error: "Not Found" });
|
|
1445
|
+
});
|
|
1446
|
+
app.use((${expressErrorHandlerParameters(language)}) => {
|
|
1447
|
+
console.error(error);
|
|
1448
|
+
response.status(500).json({ error: "Internal Server Error" });
|
|
1449
|
+
});
|
|
914
1450
|
return app;
|
|
915
1451
|
}
|
|
916
1452
|
`
|
|
@@ -922,13 +1458,29 @@ export function createApp() {
|
|
|
922
1458
|
}
|
|
923
1459
|
`;
|
|
924
1460
|
}
|
|
1461
|
+
if (basicHandler) {
|
|
1462
|
+
await writeFile(join(sourceRoot, `basicHandler.${language}`), basicHandler);
|
|
1463
|
+
}
|
|
925
1464
|
await writeFile(join(sourceRoot, `app.${language}`), app);
|
|
926
1465
|
await writeFile(join(sourceRoot, `server.${language}`), server);
|
|
927
1466
|
|
|
928
1467
|
const scripts = typed
|
|
929
|
-
?
|
|
930
|
-
|
|
931
|
-
|
|
1468
|
+
? framework === "basic"
|
|
1469
|
+
? {
|
|
1470
|
+
dev: `tsx watch src/server.${language}`,
|
|
1471
|
+
start: `tsx src/server.${language}`,
|
|
1472
|
+
}
|
|
1473
|
+
: { dev: `tsx watch src/server.${language}`, start: `tsx src/server.${language}` }
|
|
1474
|
+
: framework === "basic"
|
|
1475
|
+
? {
|
|
1476
|
+
dev: `node --watch src/server.${language}`,
|
|
1477
|
+
start: `node src/server.${language}`,
|
|
1478
|
+
}
|
|
1479
|
+
: { dev: `node --watch src/server.${language}`, start: `node src/server.${language}` };
|
|
1480
|
+
const dependencies =
|
|
1481
|
+
framework === "express"
|
|
1482
|
+
? { express: "^5", cors: "^2.8.5", dotenv: "^16" }
|
|
1483
|
+
: { dotenv: "^16" };
|
|
932
1484
|
const packageJson = {
|
|
933
1485
|
name: serverFolder,
|
|
934
1486
|
version: "1.0.0",
|
|
@@ -939,7 +1491,9 @@ export function createApp() {
|
|
|
939
1491
|
? {
|
|
940
1492
|
dependencies: { ...dependencies, tsx: "^4" },
|
|
941
1493
|
devDependencies: {
|
|
942
|
-
...(framework === "express"
|
|
1494
|
+
...(framework === "express"
|
|
1495
|
+
? { "@types/cors": "^2.8.19", "@types/express": "^5" }
|
|
1496
|
+
: {}),
|
|
943
1497
|
"@types/node": "^22",
|
|
944
1498
|
typescript: "^5",
|
|
945
1499
|
},
|
|
@@ -973,27 +1527,48 @@ export function createApp() {
|
|
|
973
1527
|
}
|
|
974
1528
|
await writeFile(
|
|
975
1529
|
join(serverRoot, ".env"),
|
|
976
|
-
"# Local backend environment variables\nPORT=4000\n",
|
|
1530
|
+
"# Local backend environment variables\nPORT=4000\nHOST=localhost\nCORS_ORIGIN=http://localhost:5173,http://127.0.0.1:5173\n",
|
|
977
1531
|
{ flag: "wx" },
|
|
978
1532
|
);
|
|
1533
|
+
await writeFile(
|
|
1534
|
+
join(serverRoot, ".env.example"),
|
|
1535
|
+
"# Backend environment variables\nPORT=4000\nHOST=localhost\nCORS_ORIGIN=http://localhost:5173,http://127.0.0.1:5173\n",
|
|
1536
|
+
);
|
|
979
1537
|
}
|
|
980
1538
|
|
|
981
1539
|
await updateIfOutdated();
|
|
982
1540
|
|
|
983
1541
|
try {
|
|
984
1542
|
await mkdir(root);
|
|
1543
|
+
rootCreated = true;
|
|
1544
|
+
process.on("SIGINT", handleCreationSignal);
|
|
1545
|
+
process.on("SIGTERM", handleCreationSignal);
|
|
985
1546
|
} catch (error) {
|
|
986
1547
|
if (error.code === "EEXIST") console.error(`The destination already exists: ${root}`);
|
|
987
1548
|
else console.error(`Unable to create project directory: ${error.message}`);
|
|
988
1549
|
process.exit(1);
|
|
989
1550
|
}
|
|
990
1551
|
|
|
1552
|
+
let installFailed = false;
|
|
991
1553
|
try {
|
|
992
1554
|
console.log(`\nCreating the frontend in ${clientFolder} with create-vite...\n`);
|
|
993
1555
|
const viteArgs =
|
|
994
1556
|
packageManager === "pnpm"
|
|
995
|
-
? [
|
|
996
|
-
|
|
1557
|
+
? [
|
|
1558
|
+
"create",
|
|
1559
|
+
"vite@latest",
|
|
1560
|
+
clientFolder,
|
|
1561
|
+
"--no-immediate",
|
|
1562
|
+
...(templateOption ? ["--template", templateOption] : []),
|
|
1563
|
+
]
|
|
1564
|
+
: [
|
|
1565
|
+
"create",
|
|
1566
|
+
"vite@latest",
|
|
1567
|
+
"--",
|
|
1568
|
+
clientFolder,
|
|
1569
|
+
"--no-immediate",
|
|
1570
|
+
...(templateOption ? ["--template", templateOption] : []),
|
|
1571
|
+
];
|
|
997
1572
|
await run(packageManager, viteArgs, root);
|
|
998
1573
|
|
|
999
1574
|
const clientRoot = join(root, clientFolder);
|
|
@@ -1006,9 +1581,6 @@ try {
|
|
|
1006
1581
|
const clientPackage = JSON.parse(await readFile(clientPackagePath, "utf8"));
|
|
1007
1582
|
clientPackage.scripts ??= {};
|
|
1008
1583
|
clientPackage.scripts.start ??= clientPackage.scripts.dev;
|
|
1009
|
-
if (clientPackage.scripts.dev && !clientPackage.scripts.dev.includes("--host")) {
|
|
1010
|
-
clientPackage.scripts.dev += " --host";
|
|
1011
|
-
}
|
|
1012
1584
|
await configureFrontend(clientRoot, clientPackage);
|
|
1013
1585
|
await writeFile(clientPackagePath, `${JSON.stringify(clientPackage, null, 2)}\n`);
|
|
1014
1586
|
await writeFile(
|
|
@@ -1016,18 +1588,26 @@ try {
|
|
|
1016
1588
|
"# Local frontend environment variables\nVITE_API_URL=http://localhost:4000\n",
|
|
1017
1589
|
{ flag: "wx" },
|
|
1018
1590
|
);
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
"
|
|
1022
|
-
[
|
|
1023
|
-
{ label: "Yes — include a backend workspace", value: true },
|
|
1024
|
-
{ label: "No — create a frontend-only project", value: false },
|
|
1025
|
-
],
|
|
1026
|
-
0,
|
|
1027
|
-
"yellow",
|
|
1591
|
+
await writeFile(
|
|
1592
|
+
join(clientRoot, ".env.example"),
|
|
1593
|
+
"# Frontend environment variables\nVITE_API_URL=http://localhost:4000\n",
|
|
1028
1594
|
);
|
|
1595
|
+
|
|
1596
|
+
const includeServer =
|
|
1597
|
+
backendOption !== undefined
|
|
1598
|
+
? backendOption !== "none"
|
|
1599
|
+
: await chooseOption(
|
|
1600
|
+
"Would you like to add a separate backend server?",
|
|
1601
|
+
[
|
|
1602
|
+
{ label: "Yes — include a backend workspace", value: true },
|
|
1603
|
+
{ label: "No — create a frontend-only project", value: false },
|
|
1604
|
+
],
|
|
1605
|
+
0,
|
|
1606
|
+
"yellow",
|
|
1607
|
+
);
|
|
1029
1608
|
const workspaces = [clientFolder];
|
|
1030
1609
|
if (includeServer) {
|
|
1610
|
+
await scaffoldBackendStatus(clientRoot);
|
|
1031
1611
|
await createBackend(join(root, serverFolder));
|
|
1032
1612
|
workspaces.push(serverFolder);
|
|
1033
1613
|
}
|
|
@@ -1040,10 +1620,20 @@ try {
|
|
|
1040
1620
|
name: projectName,
|
|
1041
1621
|
version: "1.0.0",
|
|
1042
1622
|
private: true,
|
|
1623
|
+
engines: { node: ">=20.6" },
|
|
1043
1624
|
workspaces,
|
|
1044
1625
|
scripts: {
|
|
1045
1626
|
preinstall: "node scripts/install-info.mjs",
|
|
1046
|
-
|
|
1627
|
+
postinstall: "node scripts/install-info.mjs --complete",
|
|
1628
|
+
dev: `node scripts/dev.mjs${exposeClient ? " --host" : ""}`,
|
|
1629
|
+
build:
|
|
1630
|
+
packageManager === "pnpm"
|
|
1631
|
+
? "pnpm -r --if-present run build"
|
|
1632
|
+
: "npm run build --workspaces --if-present",
|
|
1633
|
+
lint:
|
|
1634
|
+
packageManager === "pnpm"
|
|
1635
|
+
? "pnpm -r --if-present run lint"
|
|
1636
|
+
: "npm run lint --workspaces --if-present",
|
|
1047
1637
|
client: "node scripts/run-app.mjs client",
|
|
1048
1638
|
adD: "node scripts/add-dependency.mjs",
|
|
1049
1639
|
"add-dependency": "node scripts/add-dependency.mjs",
|
|
@@ -1055,19 +1645,23 @@ try {
|
|
|
1055
1645
|
},
|
|
1056
1646
|
};
|
|
1057
1647
|
await writeFile(join(root, "package.json"), `${JSON.stringify(rootPackage, null, 2)}\n`);
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1648
|
+
if (packageManager === "pnpm") {
|
|
1649
|
+
await writeFile(
|
|
1650
|
+
join(root, "pnpm-workspace.yaml"),
|
|
1651
|
+
[
|
|
1652
|
+
"packages:",
|
|
1653
|
+
...workspaces.map((workspace) => ` - ${workspace}`),
|
|
1654
|
+
"",
|
|
1655
|
+
"onlyBuiltDependencies: [esbuild]",
|
|
1656
|
+
"",
|
|
1657
|
+
"allowBuilds:",
|
|
1658
|
+
" esbuild: true",
|
|
1659
|
+
" sharp: false",
|
|
1660
|
+
" unrs-resolver: false",
|
|
1661
|
+
"",
|
|
1662
|
+
].join("\n"),
|
|
1663
|
+
);
|
|
1664
|
+
}
|
|
1071
1665
|
await writeFile(
|
|
1072
1666
|
join(root, ".gitignore"),
|
|
1073
1667
|
[
|
|
@@ -1086,37 +1680,85 @@ try {
|
|
|
1086
1680
|
"npm-debug.log*",
|
|
1087
1681
|
"pnpm-debug.log*",
|
|
1088
1682
|
"yarn-debug.log*",
|
|
1683
|
+
".dev.log",
|
|
1684
|
+
".dev.lock",
|
|
1685
|
+
".install-state",
|
|
1089
1686
|
"",
|
|
1090
1687
|
].join("\n"),
|
|
1091
1688
|
);
|
|
1092
1689
|
|
|
1093
1690
|
console.log(`\nInstalling dependencies for ${workspaces.join(" and ")}...`);
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1691
|
+
try {
|
|
1692
|
+
await run(packageManager, ["install"], root, {
|
|
1693
|
+
...process.env,
|
|
1694
|
+
VANILLA_VITE_ALLOW_INSTALL: "1",
|
|
1695
|
+
});
|
|
1696
|
+
} catch (error) {
|
|
1697
|
+
installFailed = true;
|
|
1698
|
+
await rm(join(root, ".install-state"), { force: true });
|
|
1699
|
+
console.error(
|
|
1700
|
+
`Dependency installation failed: ${error.message}\nRetry with:\n cd "${projectName}" && ${packageManager} install`,
|
|
1701
|
+
);
|
|
1702
|
+
}
|
|
1703
|
+
rootCreated = false;
|
|
1704
|
+
process.off("SIGINT", handleCreationSignal);
|
|
1705
|
+
process.off("SIGTERM", handleCreationSignal);
|
|
1706
|
+
if (!hasOption("no-git")) {
|
|
1707
|
+
const repositoryCheck = spawnSync(
|
|
1708
|
+
"git",
|
|
1709
|
+
["rev-parse", "--is-inside-work-tree"],
|
|
1710
|
+
{ cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] },
|
|
1711
|
+
);
|
|
1712
|
+
if (repositoryCheck.status === 0 && repositoryCheck.stdout.trim() === "true") {
|
|
1713
|
+
console.log("Skipping Git initialization because this project is already inside a Git work tree.");
|
|
1714
|
+
} else {
|
|
1715
|
+
const initializeGit = await chooseOption(
|
|
1716
|
+
"Would you like to initialize Git and create an initial commit?",
|
|
1717
|
+
[
|
|
1718
|
+
{ label: "Yes — initialize Git and commit the project", value: true },
|
|
1719
|
+
{ label: "No", value: false },
|
|
1720
|
+
],
|
|
1721
|
+
0,
|
|
1722
|
+
);
|
|
1723
|
+
if (initializeGit) {
|
|
1724
|
+
try {
|
|
1725
|
+
await run("git", ["init"], root);
|
|
1726
|
+
await run("git", ["add", "-A"], root);
|
|
1727
|
+
await run("git", ["commit", "-m", "Initial commit"], root);
|
|
1728
|
+
} catch (error) {
|
|
1729
|
+
console.error(`Unable to create the initial Git commit: ${error.message}`);
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1111
1734
|
console.log(
|
|
1112
|
-
`\nProject created successfully: ${projectName}/\
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1735
|
+
`\nProject created successfully: ${projectName}/\n${
|
|
1736
|
+
installFailed
|
|
1737
|
+
? "Install dependencies with the retry command above.\n"
|
|
1738
|
+
: noStart
|
|
1739
|
+
? "Skipping the development server.\n"
|
|
1740
|
+
: "Starting the development server...\n"
|
|
1741
|
+
}`,
|
|
1118
1742
|
);
|
|
1119
1743
|
} catch (error) {
|
|
1120
|
-
|
|
1121
|
-
|
|
1744
|
+
if (!creationCancelled) {
|
|
1745
|
+
await removeIncompleteProject();
|
|
1746
|
+
console.error(`\nProject creation failed: ${error.message}`);
|
|
1747
|
+
process.exit(1);
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
if (!creationCancelled && !noStart && !installFailed) {
|
|
1752
|
+
try {
|
|
1753
|
+
await run(
|
|
1754
|
+
packageManager,
|
|
1755
|
+
packageManager === "pnpm" ? ["dev"] : ["run", "dev"],
|
|
1756
|
+
root,
|
|
1757
|
+
);
|
|
1758
|
+
} catch (error) {
|
|
1759
|
+
console.error(
|
|
1760
|
+
`\nProject created successfully, but the development server could not be started: ${error.message}`,
|
|
1761
|
+
);
|
|
1762
|
+
process.exitCode = 1;
|
|
1763
|
+
}
|
|
1122
1764
|
}
|