create-vanilla-vite 1.2.1 → 1.2.3
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 +26 -0
- package/README.md +160 -22
- package/bin/create-template.mjs +109 -18
- package/package.json +21 -3
- package/scripts/add-dependency.mjs +35 -8
- package/scripts/dev.mjs +123 -54
- package/scripts/install-info.mjs +47 -14
- package/scripts/run-app.mjs +15 -3
- package/scripts/toggle-feature.mjs +407 -0
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
const root = fileURLToPath(new URL("../", import.meta.url));
|
|
7
|
+
|
|
8
|
+
const ADD_DEPENDENCY_TEMPLATE = `import { readFile } from "node:fs/promises";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import { emitKeypressEvents } from "node:readline";
|
|
12
|
+
import { stdin, stdout } from "node:process";
|
|
13
|
+
import spawn from "cross-spawn";
|
|
14
|
+
|
|
15
|
+
const root = fileURLToPath(new URL("../", import.meta.url));
|
|
16
|
+
const project = JSON.parse(await readFile(join(root, "package.json"), "utf8"));
|
|
17
|
+
const packages = process.argv.slice(2);
|
|
18
|
+
const packageManager = process.env.npm_config_user_agent?.startsWith("pnpm/")
|
|
19
|
+
? "pnpm"
|
|
20
|
+
: "npm";
|
|
21
|
+
|
|
22
|
+
if (packages.length === 0) {
|
|
23
|
+
console.log(\`Syncing workspace dependencies with \${packageManager} install...\`);
|
|
24
|
+
const child = spawn(packageManager, ["install"], {
|
|
25
|
+
cwd: root,
|
|
26
|
+
stdio: "inherit",
|
|
27
|
+
env: { ...process.env, VANILLA_VITE_ALLOW_INSTALL: "1" },
|
|
28
|
+
});
|
|
29
|
+
child.once("error", (error) => {
|
|
30
|
+
console.error(\`Unable to run \${packageManager}: \${error.message}\`);
|
|
31
|
+
process.exitCode = 1;
|
|
32
|
+
});
|
|
33
|
+
child.once("exit", (code, signal) => {
|
|
34
|
+
process.exitCode =
|
|
35
|
+
code ??
|
|
36
|
+
(signal === "SIGINT"
|
|
37
|
+
? 130
|
|
38
|
+
: signal === "SIGTERM"
|
|
39
|
+
? 143
|
|
40
|
+
: signal
|
|
41
|
+
? 1
|
|
42
|
+
: 0);
|
|
43
|
+
});
|
|
44
|
+
await new Promise((resolve) => child.once("close", resolve));
|
|
45
|
+
process.exit(process.exitCode ?? 0);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!stdin.isTTY) {
|
|
49
|
+
console.error("Choose an install location in an interactive terminal.");
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const targets = [
|
|
54
|
+
{ label: \`Project root (\${project.name})\`, workspace: null },
|
|
55
|
+
...project.workspaces.map((workspace) => ({
|
|
56
|
+
label: workspace,
|
|
57
|
+
workspace,
|
|
58
|
+
})),
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
const defaultIndex = Math.max(
|
|
62
|
+
0,
|
|
63
|
+
targets.findIndex((target) => target.workspace?.endsWith("-client")),
|
|
64
|
+
);
|
|
65
|
+
let selectedIndex = defaultIndex;
|
|
66
|
+
emitKeypressEvents(stdin);
|
|
67
|
+
|
|
68
|
+
const selectedTarget = await new Promise((resolveSelection) => {
|
|
69
|
+
function render() {
|
|
70
|
+
const question = "Where should the package be installed?";
|
|
71
|
+
const prompt = stdout.isTTY && process.env.NO_COLOR === undefined
|
|
72
|
+
? \`\\x1b[1;36m\${question}\\x1b[0m\`
|
|
73
|
+
: question;
|
|
74
|
+
stdout.write(\`\\r\\x1b[2K\${prompt}\\n\`);
|
|
75
|
+
targets.forEach((target, index) => {
|
|
76
|
+
stdout.write(\` \${index === selectedIndex ? ">" : " "} \${target.label}\\n\`);
|
|
77
|
+
});
|
|
78
|
+
stdout.write(" Use ↑/↓ to select, Enter to confirm, or Esc to keep the default.\\n");
|
|
79
|
+
stdout.write(\`\\x1b[\${targets.length + 2}A\`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function finish(target) {
|
|
83
|
+
stdin.off("keypress", onKeypress);
|
|
84
|
+
stdin.setRawMode(false);
|
|
85
|
+
stdin.pause();
|
|
86
|
+
stdout.write(\`\\x1b[\${targets.length + 2}B\\n\`);
|
|
87
|
+
resolveSelection(target);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function onKeypress(_input, key) {
|
|
91
|
+
if (key?.name === "up" || key?.name === "down") {
|
|
92
|
+
selectedIndex =
|
|
93
|
+
(selectedIndex + (key.name === "down" ? 1 : targets.length - 1)) %
|
|
94
|
+
targets.length;
|
|
95
|
+
render();
|
|
96
|
+
} else if (key?.name === "return") finish(targets[selectedIndex]);
|
|
97
|
+
else if (key?.name === "escape") finish(targets[defaultIndex]);
|
|
98
|
+
else if (key?.ctrl && key.name === "c") {
|
|
99
|
+
stdin.off("keypress", onKeypress);
|
|
100
|
+
stdin.setRawMode(false);
|
|
101
|
+
stdin.pause();
|
|
102
|
+
stdout.write("\\n");
|
|
103
|
+
resolveSelection(null);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
stdin.setRawMode(true);
|
|
108
|
+
stdin.resume();
|
|
109
|
+
stdin.on("keypress", onKeypress);
|
|
110
|
+
render();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if (!selectedTarget) {
|
|
114
|
+
process.exit(130);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const { workspace } = selectedTarget;
|
|
118
|
+
const commandArgs =
|
|
119
|
+
packageManager === "pnpm"
|
|
120
|
+
? workspace
|
|
121
|
+
? ["--filter", workspace, "add", ...packages]
|
|
122
|
+
: ["add", "--workspace-root", ...packages]
|
|
123
|
+
: workspace
|
|
124
|
+
? ["install", ...packages, "--workspace", workspace]
|
|
125
|
+
: ["install", ...packages];
|
|
126
|
+
const child = spawn(packageManager, commandArgs, {
|
|
127
|
+
cwd: root,
|
|
128
|
+
stdio: "inherit",
|
|
129
|
+
env: { ...process.env, VANILLA_VITE_ALLOW_INSTALL: "1" },
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
child.once("error", (error) => {
|
|
133
|
+
console.error(\`Unable to run \${packageManager}: \${error.message}\`);
|
|
134
|
+
process.exitCode = 1;
|
|
135
|
+
});
|
|
136
|
+
child.once("exit", (code, signal) => {
|
|
137
|
+
process.exitCode =
|
|
138
|
+
code ??
|
|
139
|
+
(signal === "SIGINT"
|
|
140
|
+
? 130
|
|
141
|
+
: signal === "SIGTERM"
|
|
142
|
+
? 143
|
|
143
|
+
: signal
|
|
144
|
+
? 1
|
|
145
|
+
: 0);
|
|
146
|
+
});
|
|
147
|
+
`;
|
|
148
|
+
|
|
149
|
+
export const FEATURES = {
|
|
150
|
+
adD: {
|
|
151
|
+
id: "adD",
|
|
152
|
+
name: "adD",
|
|
153
|
+
description: "Workspace dependency selector and direct install guard",
|
|
154
|
+
aliases: ["add", "add-dependency", "install", "i", "guard", "install-mode"],
|
|
155
|
+
isEnabled(project) {
|
|
156
|
+
return (project.vanillaVite?.installMode ?? "adD") !== "plain";
|
|
157
|
+
},
|
|
158
|
+
getStatus(project) {
|
|
159
|
+
const mode = project.vanillaVite?.installMode ?? "adD";
|
|
160
|
+
const hasScript = Boolean(project.scripts?.adD);
|
|
161
|
+
return {
|
|
162
|
+
enabled: mode !== "plain",
|
|
163
|
+
mode,
|
|
164
|
+
hasScript,
|
|
165
|
+
};
|
|
166
|
+
},
|
|
167
|
+
async turnOn({ rootDir, project, subOption, packageManager }) {
|
|
168
|
+
const targetMode = subOption === "both" || subOption === "--both" ? "both" : "adD";
|
|
169
|
+
project.vanillaVite = project.vanillaVite ?? {};
|
|
170
|
+
project.vanillaVite.installMode = targetMode;
|
|
171
|
+
|
|
172
|
+
project.scripts = project.scripts ?? {};
|
|
173
|
+
project.scripts.adD = "node scripts/add-dependency.mjs";
|
|
174
|
+
project.scripts["add-dependency"] = "node scripts/add-dependency.mjs";
|
|
175
|
+
|
|
176
|
+
const scriptsDir = join(rootDir, "scripts");
|
|
177
|
+
await mkdir(scriptsDir, { recursive: true });
|
|
178
|
+
const scriptPath = join(scriptsDir, "add-dependency.mjs");
|
|
179
|
+
if (!existsSync(scriptPath)) {
|
|
180
|
+
await writeFile(scriptPath, ADD_DEPENDENCY_TEMPLATE, "utf8");
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
await writeFile(
|
|
184
|
+
join(rootDir, "package.json"),
|
|
185
|
+
`${JSON.stringify(project, null, 2)}\n`,
|
|
186
|
+
"utf8",
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
const isPnpm = packageManager === "pnpm";
|
|
190
|
+
const addCmd = isPnpm ? "pnpm adD <package>" : "npm run adD -- <package>";
|
|
191
|
+
const addDepCmd = isPnpm
|
|
192
|
+
? "pnpm add-dependency <package>"
|
|
193
|
+
: "npm run add-dependency -- <package>";
|
|
194
|
+
const turnOffCmd = isPnpm ? "pnpm tO adD" : "npm run tO -- adD";
|
|
195
|
+
|
|
196
|
+
console.log(`[feature] Feature 'adD' turned ON (mode: ${targetMode}).`);
|
|
197
|
+
console.log(` - Scripts enabled: ${addCmd}, ${addDepCmd}`);
|
|
198
|
+
if (targetMode === "both") {
|
|
199
|
+
console.log(" - Direct installs also permitted alongside adD.");
|
|
200
|
+
} else {
|
|
201
|
+
console.log(" - Direct root installs are guarded to preserve monorepo workspace structure.");
|
|
202
|
+
}
|
|
203
|
+
console.log(" - Development runner banner '>>> ADD DEPENDENCIES LATER <<<' is active.");
|
|
204
|
+
console.log(` To turn off: ${turnOffCmd}\n`);
|
|
205
|
+
},
|
|
206
|
+
async turnOff({ rootDir, project, packageManager }) {
|
|
207
|
+
project.vanillaVite = project.vanillaVite ?? {};
|
|
208
|
+
project.vanillaVite.installMode = "plain";
|
|
209
|
+
|
|
210
|
+
if (project.scripts) {
|
|
211
|
+
delete project.scripts.adD;
|
|
212
|
+
delete project.scripts["add-dependency"];
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
await writeFile(
|
|
216
|
+
join(rootDir, "package.json"),
|
|
217
|
+
`${JSON.stringify(project, null, 2)}\n`,
|
|
218
|
+
"utf8",
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const isPnpm = packageManager === "pnpm";
|
|
222
|
+
const directInstall = isPnpm ? "pnpm add <package>" : "npm install <package>";
|
|
223
|
+
const turnOnCmd = isPnpm ? "pnpm tN adD" : "npm run tN -- adD";
|
|
224
|
+
|
|
225
|
+
console.log("[feature] Feature 'adD' turned OFF (mode: plain).");
|
|
226
|
+
console.log(` - Direct installs enabled: ${directInstall}`);
|
|
227
|
+
console.log(" - Removed 'adD' and 'add-dependency' scripts.");
|
|
228
|
+
console.log(" - Development runner banner '>>> ADD DEPENDENCIES LATER <<<' is hidden.");
|
|
229
|
+
console.log(` To turn on: ${turnOnCmd}\n`);
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
export function resolveFeature(featureInput) {
|
|
235
|
+
if (!featureInput) return null;
|
|
236
|
+
const normalized = featureInput.trim().toLowerCase();
|
|
237
|
+
for (const feature of Object.values(FEATURES)) {
|
|
238
|
+
if (feature.id.toLowerCase() === normalized) return feature;
|
|
239
|
+
if (feature.aliases.some((alias) => alias.toLowerCase() === normalized)) {
|
|
240
|
+
return feature;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const ACTION_ON_ALIASES = new Set(["on", "turn-on", "trun-on", "tn", "enable", "1", "true"]);
|
|
247
|
+
const ACTION_OFF_ALIASES = new Set(["off", "turn-off", "trun-off", "to", "disable", "0", "false"]);
|
|
248
|
+
const ACTION_STATUS_ALIASES = new Set(["status", "list", "info", "-s", "--status", "-l", "--list"]);
|
|
249
|
+
const ACTION_TOGGLE_ALIASES = new Set(["toggle", "t"]);
|
|
250
|
+
|
|
251
|
+
export function parseArgs(rawArgs) {
|
|
252
|
+
if (!rawArgs || rawArgs.length === 0) {
|
|
253
|
+
return { action: "status", featureName: null, subOption: null };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const normalizeFeature = (input) => {
|
|
257
|
+
if (!input) return "adD";
|
|
258
|
+
const matched = resolveFeature(input);
|
|
259
|
+
return matched ? matched.id : input;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const first = rawArgs[0].toLowerCase();
|
|
263
|
+
|
|
264
|
+
if (ACTION_STATUS_ALIASES.has(first)) {
|
|
265
|
+
return {
|
|
266
|
+
action: "status",
|
|
267
|
+
featureName: rawArgs[1] ? normalizeFeature(rawArgs[1]) : null,
|
|
268
|
+
subOption: rawArgs[2] ?? null,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (ACTION_ON_ALIASES.has(first)) {
|
|
273
|
+
return {
|
|
274
|
+
action: "on",
|
|
275
|
+
featureName: normalizeFeature(rawArgs[1]),
|
|
276
|
+
subOption: rawArgs[2] ?? null,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (ACTION_OFF_ALIASES.has(first)) {
|
|
281
|
+
return {
|
|
282
|
+
action: "off",
|
|
283
|
+
featureName: normalizeFeature(rawArgs[1]),
|
|
284
|
+
subOption: rawArgs[2] ?? null,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (ACTION_TOGGLE_ALIASES.has(first)) {
|
|
289
|
+
return {
|
|
290
|
+
action: "toggle",
|
|
291
|
+
featureName: normalizeFeature(rawArgs[1]),
|
|
292
|
+
subOption: rawArgs[2] ?? null,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Check if first argument is a feature identifier or alias
|
|
297
|
+
const matchedFeature = resolveFeature(first);
|
|
298
|
+
if (matchedFeature) {
|
|
299
|
+
const second = rawArgs[1]?.toLowerCase();
|
|
300
|
+
if (!second || ACTION_TOGGLE_ALIASES.has(second)) {
|
|
301
|
+
return { action: "toggle", featureName: matchedFeature.id, subOption: rawArgs[2] ?? null };
|
|
302
|
+
}
|
|
303
|
+
if (ACTION_ON_ALIASES.has(second)) {
|
|
304
|
+
return { action: "on", featureName: matchedFeature.id, subOption: rawArgs[2] ?? null };
|
|
305
|
+
}
|
|
306
|
+
if (ACTION_OFF_ALIASES.has(second)) {
|
|
307
|
+
return { action: "off", featureName: matchedFeature.id, subOption: rawArgs[2] ?? null };
|
|
308
|
+
}
|
|
309
|
+
if (ACTION_STATUS_ALIASES.has(second)) {
|
|
310
|
+
return { action: "status", featureName: matchedFeature.id, subOption: rawArgs[2] ?? null };
|
|
311
|
+
}
|
|
312
|
+
return { action: second, featureName: matchedFeature.id, subOption: rawArgs[2] ?? null };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return {
|
|
316
|
+
action: rawArgs[0],
|
|
317
|
+
featureName: rawArgs[1] ? normalizeFeature(rawArgs[1]) : null,
|
|
318
|
+
subOption: rawArgs[2] ?? null,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export async function toggleFeature({
|
|
323
|
+
rootDir = root,
|
|
324
|
+
action,
|
|
325
|
+
featureName,
|
|
326
|
+
subOption,
|
|
327
|
+
packageManager = process.env.npm_config_user_agent?.startsWith("pnpm/") ? "pnpm" : "npm",
|
|
328
|
+
}) {
|
|
329
|
+
const packagePath = join(rootDir, "package.json");
|
|
330
|
+
let project;
|
|
331
|
+
try {
|
|
332
|
+
project = JSON.parse(await readFile(packagePath, "utf8"));
|
|
333
|
+
} catch (error) {
|
|
334
|
+
console.error(`Unable to read package.json at ${packagePath}: ${error.message}`);
|
|
335
|
+
process.exitCode = 1;
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (action === "status") {
|
|
340
|
+
printStatus(project, packageManager);
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const targetName = featureName ?? "adD";
|
|
345
|
+
const feature = resolveFeature(targetName);
|
|
346
|
+
if (!feature) {
|
|
347
|
+
console.error(`Unknown feature: '${targetName}'.`);
|
|
348
|
+
console.error("Available features:");
|
|
349
|
+
for (const f of Object.values(FEATURES)) {
|
|
350
|
+
console.error(` - ${f.name} (aliases: ${f.aliases.join(", ")})`);
|
|
351
|
+
}
|
|
352
|
+
process.exitCode = 1;
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
let finalAction = action;
|
|
357
|
+
if (finalAction === "toggle") {
|
|
358
|
+
finalAction = feature.isEnabled(project) ? "off" : "on";
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (finalAction === "on") {
|
|
362
|
+
await feature.turnOn({ rootDir, project, subOption, packageManager });
|
|
363
|
+
} else if (finalAction === "off") {
|
|
364
|
+
await feature.turnOff({ rootDir, project, packageManager });
|
|
365
|
+
} else {
|
|
366
|
+
console.error(`Invalid action '${action}'. Use 'on' or 'off'.`);
|
|
367
|
+
process.exitCode = 1;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function printStatus(project, packageManager) {
|
|
372
|
+
const isPnpm = packageManager === "pnpm";
|
|
373
|
+
console.log("Vanilla Vite Project Features:");
|
|
374
|
+
for (const feature of Object.values(FEATURES)) {
|
|
375
|
+
const status = feature.getStatus(project);
|
|
376
|
+
const stateStr = status.enabled ? "ENABLED" : "DISABLED";
|
|
377
|
+
console.log(`\n [${feature.name}] - ${stateStr}`);
|
|
378
|
+
console.log(` Description: ${feature.description}`);
|
|
379
|
+
console.log(` Mode: ${status.mode}`);
|
|
380
|
+
console.log(` Aliases: ${feature.aliases.join(", ")}`);
|
|
381
|
+
if (isPnpm) {
|
|
382
|
+
console.log(` Turn on: pnpm tN ${feature.name}`);
|
|
383
|
+
console.log(` Turn off: pnpm tO ${feature.name}`);
|
|
384
|
+
} else {
|
|
385
|
+
console.log(` Turn on: npm run tN -- ${feature.name}`);
|
|
386
|
+
console.log(` Turn off: npm run tO -- ${feature.name}`);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
console.log("");
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export async function main() {
|
|
393
|
+
const parsed = parseArgs(process.argv.slice(2));
|
|
394
|
+
await toggleFeature({
|
|
395
|
+
rootDir: root,
|
|
396
|
+
action: parsed.action,
|
|
397
|
+
featureName: parsed.featureName,
|
|
398
|
+
subOption: parsed.subOption,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
|
|
403
|
+
main().catch((error) => {
|
|
404
|
+
console.error(`Feature toggle error: ${error.message}`);
|
|
405
|
+
process.exit(1);
|
|
406
|
+
});
|
|
407
|
+
}
|