@voltx/cli 0.3.9 → 0.4.1

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.
@@ -0,0 +1,213 @@
1
+ // src/dev.ts
2
+ import { spawn } from "child_process";
3
+ import { resolve, join } from "path";
4
+ import { existsSync, writeFileSync, unlinkSync } from "fs";
5
+ import { loadEnv } from "@voltx/core";
6
+ async function runDev(options = {}) {
7
+ const cwd = process.cwd();
8
+ const {
9
+ port,
10
+ entry = findEntryPoint(cwd),
11
+ clearScreen = true
12
+ } = options;
13
+ if (!entry) {
14
+ console.error("[voltx] Could not find entry point. Expected server.ts or src/index.ts");
15
+ process.exit(1);
16
+ }
17
+ const entryPath = resolve(cwd, entry);
18
+ if (!existsSync(entryPath)) {
19
+ console.error(`[voltx] Entry file not found: ${entry}`);
20
+ process.exit(1);
21
+ }
22
+ loadEnv("development", cwd);
23
+ const hasViteConfig = existsSync(resolve(cwd, "vite.config.ts"));
24
+ const hasServerEntry = existsSync(resolve(cwd, "server.ts"));
25
+ const isFullStack = hasViteConfig || hasServerEntry;
26
+ if (isFullStack) {
27
+ await startViteDevServer(cwd, port, entry, options);
28
+ } else {
29
+ await startTsxWatch(cwd, port, entry, options);
30
+ }
31
+ }
32
+ async function startViteDevServer(cwd, port, entry, options) {
33
+ const resolvedPort = port ?? (Number(process.env.PORT) || 3e3);
34
+ printDevBanner(entry, resolvedPort, true);
35
+ const userViteConfig = resolve(cwd, "vite.config.ts");
36
+ const hasUserViteConfig = existsSync(userViteConfig);
37
+ let tempConfigPath = null;
38
+ if (!hasUserViteConfig) {
39
+ tempConfigPath = resolve(cwd, "vite.config.voltx.ts");
40
+ const viteConfigContent = generateViteConfig(entry, resolvedPort);
41
+ writeFileSync(tempConfigPath, viteConfigContent, "utf-8");
42
+ }
43
+ const env = {
44
+ ...process.env,
45
+ NODE_ENV: "development"
46
+ };
47
+ if (port) {
48
+ env.PORT = String(port);
49
+ }
50
+ const viteBin = findBin(cwd, "vite");
51
+ const viteArgs = ["dev"];
52
+ if (tempConfigPath) {
53
+ viteArgs.push("--config", tempConfigPath);
54
+ }
55
+ viteArgs.push("--port", String(resolvedPort));
56
+ let child;
57
+ if (viteBin) {
58
+ child = spawn(viteBin, viteArgs, { cwd, env, stdio: "inherit" });
59
+ } else {
60
+ child = spawn("npx", ["vite", ...viteArgs], { cwd, env, stdio: "inherit" });
61
+ }
62
+ const cleanup = () => {
63
+ if (tempConfigPath && existsSync(tempConfigPath)) {
64
+ try {
65
+ unlinkSync(tempConfigPath);
66
+ } catch {
67
+ }
68
+ }
69
+ };
70
+ const signals = ["SIGINT", "SIGTERM"];
71
+ for (const signal of signals) {
72
+ process.on(signal, () => {
73
+ cleanup();
74
+ child.kill(signal);
75
+ });
76
+ }
77
+ child.on("error", (err) => {
78
+ cleanup();
79
+ if (err.code === "ENOENT") {
80
+ console.error("[voltx] vite not found. Install it with: npm install -D vite @hono/vite-dev-server");
81
+ } else {
82
+ console.error("[voltx] Dev server error:", err.message);
83
+ }
84
+ process.exit(1);
85
+ });
86
+ child.on("exit", (code) => {
87
+ cleanup();
88
+ process.exit(code ?? 0);
89
+ });
90
+ }
91
+ function generateViteConfig(entry, port) {
92
+ return `// Auto-generated by VoltX \u2014 do not commit this file
93
+ import { defineConfig } from "vite";
94
+ import devServer from "@hono/vite-dev-server";
95
+ import react from "@vitejs/plugin-react";
96
+ import tailwindcss from "@tailwindcss/vite";
97
+
98
+ export default defineConfig({
99
+ resolve: {
100
+ alias: {
101
+ "@": "/src",
102
+ },
103
+ },
104
+ server: {
105
+ port: ${port},
106
+ },
107
+ plugins: [
108
+ react(),
109
+ tailwindcss(),
110
+ devServer({
111
+ entry: "${entry}",
112
+ exclude: [
113
+ /.*\\.tsx?($|\\?)/,
114
+ /.*\\.(s?css|less)($|\\?)/,
115
+ /.*\\.(svg|png|jpg|jpeg|gif|webp|ico)($|\\?)/,
116
+ /^\\/@.+$/,
117
+ /^\\/favicon\\.svg$/,
118
+ /^\\/node_modules\\/.*/,
119
+ ],
120
+ injectClientScript: false,
121
+ }),
122
+ ],
123
+ });
124
+ `;
125
+ }
126
+ async function startTsxWatch(cwd, port, entry, options) {
127
+ const resolvedPort = port ?? (Number(process.env.PORT) || 3e3);
128
+ printDevBanner(entry, resolvedPort, false);
129
+ const env = {
130
+ ...process.env,
131
+ NODE_ENV: "development"
132
+ };
133
+ if (port) {
134
+ env.PORT = String(port);
135
+ }
136
+ const tsxArgs = ["watch"];
137
+ if (options.clearScreen ?? true) {
138
+ tsxArgs.push("--clear-screen=false");
139
+ }
140
+ tsxArgs.push("--ignore=node_modules", "--ignore=dist", "--ignore=.turbo");
141
+ tsxArgs.push(entry);
142
+ const tsxBin = findBin(cwd, "tsx");
143
+ let child;
144
+ if (tsxBin) {
145
+ child = spawn(tsxBin, tsxArgs, { cwd, env, stdio: "inherit" });
146
+ } else {
147
+ child = spawn("npx", ["tsx", ...tsxArgs], { cwd, env, stdio: "inherit" });
148
+ }
149
+ const signals = ["SIGINT", "SIGTERM"];
150
+ for (const signal of signals) {
151
+ process.on(signal, () => {
152
+ child.kill(signal);
153
+ });
154
+ }
155
+ child.on("error", (err) => {
156
+ if (err.code === "ENOENT") {
157
+ console.error("[voltx] tsx not found. Install it with: npm install -D tsx");
158
+ } else {
159
+ console.error("[voltx] Dev server error:", err.message);
160
+ }
161
+ process.exit(1);
162
+ });
163
+ child.on("exit", (code) => {
164
+ process.exit(code ?? 0);
165
+ });
166
+ }
167
+ function findEntryPoint(cwd) {
168
+ const candidates = [
169
+ "server.ts",
170
+ "server.js",
171
+ "src/index.ts",
172
+ "src/index.js",
173
+ "src/index.mts",
174
+ "src/main.ts",
175
+ "src/main.js",
176
+ "index.ts",
177
+ "index.js"
178
+ ];
179
+ for (const candidate of candidates) {
180
+ if (existsSync(join(cwd, candidate))) {
181
+ return candidate;
182
+ }
183
+ }
184
+ return null;
185
+ }
186
+ function findBin(cwd, name) {
187
+ const paths = [
188
+ join(cwd, "node_modules", ".bin", name),
189
+ join(cwd, "..", "node_modules", ".bin", name),
190
+ join(cwd, "..", "..", "node_modules", ".bin", name)
191
+ ];
192
+ for (const p of paths) {
193
+ if (existsSync(p)) return p;
194
+ }
195
+ return null;
196
+ }
197
+ function printDevBanner(entry, port, fullStack) {
198
+ console.log("");
199
+ console.log(" \u26A1 VoltX Dev Server");
200
+ console.log(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500");
201
+ console.log(` Entry: ${entry}`);
202
+ console.log(` Port: ${port}`);
203
+ console.log(` Mode: ${fullStack ? "full-stack (Vite + Hono)" : "API-only (tsx watch)"}`);
204
+ if (fullStack) {
205
+ console.log(` HMR: enabled (frontend + backend)`);
206
+ }
207
+ console.log(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500");
208
+ console.log("");
209
+ }
210
+
211
+ export {
212
+ runDev
213
+ };