@shiftapi/vite-plugin 0.0.6 → 0.0.7

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.
Files changed (2) hide show
  1. package/dist/index.js +93 -31
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -91,6 +91,9 @@ async function findFreePort(startPort) {
91
91
  for (let port = startPort; port < startPort + 20; port++) {
92
92
  if (await isPortFree(port)) return port;
93
93
  }
94
+ console.warn(
95
+ `[shiftapi] No free port found in range ${startPort}-${startPort + 19}, falling back to ${startPort}`
96
+ );
94
97
  return startPort;
95
98
  }
96
99
  function shiftapiPlugin(options) {
@@ -151,7 +154,15 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
151
154
  const tsconfigPath = resolve(projectRoot, "tsconfig.json");
152
155
  if (!existsSync(tsconfigPath)) return;
153
156
  const raw = readFileSync2(tsconfigPath, "utf-8");
154
- const tsconfig = JSON.parse(raw);
157
+ let tsconfig;
158
+ try {
159
+ tsconfig = JSON.parse(raw);
160
+ } catch (err) {
161
+ console.warn(
162
+ `[shiftapi] Failed to parse tsconfig.json: ${err instanceof Error ? err.message : String(err)}`
163
+ );
164
+ return;
165
+ }
155
166
  if (tsconfig?.compilerOptions?.paths?.[MODULE_ID]) return;
156
167
  if (!tsconfig.compilerOptions) tsconfig.compilerOptions = {};
157
168
  if (!tsconfig.compilerOptions.paths) tsconfig.compilerOptions.paths = {};
@@ -165,27 +176,41 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
165
176
  );
166
177
  }
167
178
  function startGoServer() {
168
- goProcess = spawn("go", ["run", "-tags", "shiftapidev", serverEntry], {
169
- cwd: resolve(goRoot),
170
- stdio: ["ignore", "inherit", "inherit"],
171
- detached: true,
172
- env: {
173
- ...process.env,
174
- SHIFTAPI_PORT: String(goPort)
175
- }
176
- });
177
- goProcess.on("error", (err) => {
178
- console.error("[shiftapi] Failed to start Go server:", err.message);
179
- });
180
- goProcess.on("exit", (code) => {
181
- if (code !== null && code !== 0) {
182
- console.error(`[shiftapi] Go server exited with code ${code}`);
183
- }
184
- goProcess = null;
179
+ return new Promise((resolveStart, rejectStart) => {
180
+ const proc = spawn("go", ["run", "-tags", "shiftapidev", serverEntry], {
181
+ cwd: resolve(goRoot),
182
+ stdio: ["ignore", "inherit", "inherit"],
183
+ detached: true,
184
+ env: {
185
+ ...process.env,
186
+ SHIFTAPI_PORT: String(goPort)
187
+ }
188
+ });
189
+ goProcess = proc;
190
+ let settled = false;
191
+ proc.on("error", (err) => {
192
+ console.error("[shiftapi] Failed to start Go server:", err.message);
193
+ if (!settled) {
194
+ settled = true;
195
+ rejectStart(err);
196
+ }
197
+ });
198
+ proc.on("exit", (code) => {
199
+ if (code !== null && code !== 0) {
200
+ console.error(`[shiftapi] Go server exited with code ${code}`);
201
+ }
202
+ goProcess = null;
203
+ });
204
+ proc.on("spawn", () => {
205
+ if (!settled) {
206
+ settled = true;
207
+ resolveStart();
208
+ }
209
+ });
210
+ console.log(
211
+ `[shiftapi] Go server starting on port ${goPort}: go run ${serverEntry}`
212
+ );
185
213
  });
186
- console.log(
187
- `[shiftapi] Go server starting on port ${goPort}: go run ${serverEntry}`
188
- );
189
214
  }
190
215
  function stopGoServer() {
191
216
  const proc = goProcess;
@@ -193,10 +218,21 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
193
218
  const pid = proc.pid;
194
219
  goProcess = null;
195
220
  return new Promise((resolve2) => {
196
- proc.on("exit", () => resolve2());
221
+ const timeout = setTimeout(() => {
222
+ try {
223
+ process.kill(-pid, "SIGKILL");
224
+ } catch {
225
+ }
226
+ resolve2();
227
+ }, 5e3);
228
+ proc.on("exit", () => {
229
+ clearTimeout(timeout);
230
+ resolve2();
231
+ });
197
232
  try {
198
233
  process.kill(-pid, "SIGTERM");
199
234
  } catch {
235
+ clearTimeout(timeout);
200
236
  resolve2();
201
237
  }
202
238
  });
@@ -218,7 +254,10 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
218
254
  }
219
255
  const spec = getSpec();
220
256
  const paths = spec.paths;
221
- if (!paths) return;
257
+ if (!paths) {
258
+ console.warn("[shiftapi] No paths found in OpenAPI spec. Proxy will not be configured.");
259
+ return;
260
+ }
222
261
  const targetUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}:${goPort}`;
223
262
  const proxy = {};
224
263
  for (const path of Object.keys(paths)) {
@@ -231,15 +270,38 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
231
270
  configureServer(server) {
232
271
  devServer = server;
233
272
  server.watcher.add(resolve(goRoot));
234
- startGoServer();
235
- server.httpServer?.on("close", stopGoServer);
236
- process.on("exit", stopGoServer);
237
- process.on("SIGINT", () => {
238
- stopGoServer();
273
+ startGoServer().catch((err) => {
274
+ console.error("[shiftapi] Go server failed to start:", err);
275
+ });
276
+ function clearDebounce() {
277
+ if (debounceTimer) {
278
+ clearTimeout(debounceTimer);
279
+ debounceTimer = null;
280
+ }
281
+ }
282
+ server.httpServer?.on("close", () => {
283
+ clearDebounce();
284
+ stopGoServer().catch((err) => {
285
+ console.error("[shiftapi] Failed to stop Go server on close:", err);
286
+ });
287
+ });
288
+ process.on("exit", () => {
289
+ clearDebounce();
290
+ if (goProcess?.pid) {
291
+ try {
292
+ process.kill(-goProcess.pid, "SIGTERM");
293
+ } catch {
294
+ }
295
+ }
296
+ });
297
+ process.on("SIGINT", async () => {
298
+ clearDebounce();
299
+ await stopGoServer();
239
300
  process.exit();
240
301
  });
241
- process.on("SIGTERM", () => {
242
- stopGoServer();
302
+ process.on("SIGTERM", async () => {
303
+ clearDebounce();
304
+ await stopGoServer();
243
305
  process.exit();
244
306
  });
245
307
  },
@@ -269,7 +331,7 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
269
331
  debounceTimer = setTimeout(async () => {
270
332
  try {
271
333
  await stopGoServer();
272
- startGoServer();
334
+ await startGoServer();
273
335
  const changed = await regenerate();
274
336
  if (changed && devServer) {
275
337
  writeDtsFile();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shiftapi/vite-plugin",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Vite plugin for fully-typed TypeScript clients from shiftapi Go servers",
5
5
  "author": "Frank Chiarulli Jr. <frank@frankchiarulli.com>",
6
6
  "license": "MIT",
@@ -30,10 +30,10 @@
30
30
  "virtual.d.ts"
31
31
  ],
32
32
  "peerDependencies": {
33
- "vite": "^5.0.0 || ^6.0.0"
33
+ "vite": "^6.0.0"
34
34
  },
35
35
  "dependencies": {
36
- "openapi-fetch": "^0.12.0 || ^0.13.0",
36
+ "openapi-fetch": "^0.13.0",
37
37
  "openapi-typescript": "^7.0.0"
38
38
  },
39
39
  "devDependencies": {