@shiftapi/vite-plugin 0.0.3 → 0.0.5

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 (3) hide show
  1. package/README.md +4 -1
  2. package/dist/index.js +66 -13
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  <p align="center">
2
- <img src="https://raw.githubusercontent.com/fcjr/shiftapi/main/assets/logo.svg" alt="ShiftAPI Logo">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/fcjr/shiftapi/main/assets/logo-dark.svg">
4
+ <img src="https://raw.githubusercontent.com/fcjr/shiftapi/main/assets/logo.svg" alt="ShiftAPI Logo">
5
+ </picture>
3
6
  </p>
4
7
 
5
8
  # @shiftapi/vite-plugin
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  existsSync
8
8
  } from "fs";
9
9
  import { spawn } from "child_process";
10
+ import { createServer as createTcpServer } from "net";
10
11
 
11
12
  // src/extract.ts
12
13
  import { execFileSync } from "child_process";
@@ -17,7 +18,7 @@ function extractSpec(serverEntry, goRoot) {
17
18
  const tempDir = mkdtempSync(join(tmpdir(), "shiftapi-"));
18
19
  const specPath = join(tempDir, "openapi.json");
19
20
  try {
20
- execFileSync("go", ["run", serverEntry], {
21
+ execFileSync("go", ["run", "-tags", "shiftapidev", serverEntry], {
21
22
  cwd: goRoot,
22
23
  env: {
23
24
  ...process.env,
@@ -76,6 +77,22 @@ export { createClient };
76
77
  // src/index.ts
77
78
  var MODULE_ID = "@shiftapi/client";
78
79
  var RESOLVED_MODULE_ID = "\0" + MODULE_ID;
80
+ function isPortFree(port) {
81
+ return new Promise((resolve2) => {
82
+ const server = createTcpServer();
83
+ server.once("error", () => resolve2(false));
84
+ server.once("listening", () => {
85
+ server.close(() => resolve2(true));
86
+ });
87
+ server.listen(port);
88
+ });
89
+ }
90
+ async function findFreePort(startPort) {
91
+ for (let port = startPort; port < startPort + 20; port++) {
92
+ if (await isPortFree(port)) return port;
93
+ }
94
+ return startPort;
95
+ }
79
96
  function shiftapiPlugin(options) {
80
97
  const {
81
98
  server: serverEntry,
@@ -83,6 +100,9 @@ function shiftapiPlugin(options) {
83
100
  goRoot = process.cwd(),
84
101
  url = "http://localhost:8080"
85
102
  } = options;
103
+ const parsedUrl = new URL(url);
104
+ const basePort = parseInt(parsedUrl.port || "8080");
105
+ let goPort = basePort;
86
106
  let virtualModuleSource = "";
87
107
  let generatedDts = "";
88
108
  let cachedSpec = null;
@@ -111,7 +131,7 @@ function shiftapiPlugin(options) {
111
131
  return true;
112
132
  }
113
133
  function writeDtsFile() {
114
- const outDir = resolve("node_modules", ".shiftapi");
134
+ const outDir = resolve(projectRoot, ".shiftapi");
115
135
  if (!existsSync(outDir)) {
116
136
  mkdirSync(outDir, { recursive: true });
117
137
  }
@@ -136,7 +156,7 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
136
156
  if (!tsconfig.compilerOptions) tsconfig.compilerOptions = {};
137
157
  if (!tsconfig.compilerOptions.paths) tsconfig.compilerOptions.paths = {};
138
158
  tsconfig.compilerOptions.paths[MODULE_ID] = [
139
- "./node_modules/.shiftapi/client.d.ts"
159
+ "./.shiftapi/client.d.ts"
140
160
  ];
141
161
  const indent = raw.match(/^[ \t]+/m)?.[0] ?? " ";
142
162
  writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, indent) + "\n");
@@ -145,9 +165,14 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
145
165
  );
146
166
  }
147
167
  function startGoServer() {
148
- goProcess = spawn("go", ["run", serverEntry], {
168
+ goProcess = spawn("go", ["run", "-tags", "shiftapidev", serverEntry], {
149
169
  cwd: resolve(goRoot),
150
- stdio: ["ignore", "inherit", "inherit"]
170
+ stdio: ["ignore", "inherit", "inherit"],
171
+ detached: true,
172
+ env: {
173
+ ...process.env,
174
+ SHIFTAPI_PORT: String(goPort)
175
+ }
151
176
  });
152
177
  goProcess.on("error", (err) => {
153
178
  console.error("[shiftapi] Failed to start Go server:", err.message);
@@ -158,13 +183,23 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
158
183
  }
159
184
  goProcess = null;
160
185
  });
161
- console.log(`[shiftapi] Go server starting: go run ${serverEntry}`);
186
+ console.log(
187
+ `[shiftapi] Go server starting on port ${goPort}: go run ${serverEntry}`
188
+ );
162
189
  }
163
190
  function stopGoServer() {
164
- if (goProcess) {
165
- goProcess.kill();
166
- goProcess = null;
167
- }
191
+ const proc = goProcess;
192
+ if (!proc || !proc.pid) return Promise.resolve();
193
+ const pid = proc.pid;
194
+ goProcess = null;
195
+ return new Promise((resolve2) => {
196
+ proc.on("exit", () => resolve2());
197
+ try {
198
+ process.kill(-pid, "SIGTERM");
199
+ } catch {
200
+ resolve2();
201
+ }
202
+ });
168
203
  }
169
204
  return {
170
205
  name: "@shiftapi/vite-plugin",
@@ -172,13 +207,22 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
172
207
  projectRoot = config.root;
173
208
  patchTsConfig();
174
209
  },
175
- config() {
210
+ async config(_, env) {
211
+ if (env.command === "serve") {
212
+ goPort = await findFreePort(basePort);
213
+ if (goPort !== basePort) {
214
+ console.log(
215
+ `[shiftapi] Port ${basePort} is in use, using ${goPort}`
216
+ );
217
+ }
218
+ }
176
219
  const spec = getSpec();
177
220
  const paths = spec.paths;
178
221
  if (!paths) return;
222
+ const targetUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}:${goPort}`;
179
223
  const proxy = {};
180
224
  for (const path of Object.keys(paths)) {
181
- proxy[path] = url;
225
+ proxy[path] = targetUrl;
182
226
  }
183
227
  return {
184
228
  server: { proxy }
@@ -189,6 +233,15 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
189
233
  server.watcher.add(resolve(goRoot));
190
234
  startGoServer();
191
235
  server.httpServer?.on("close", stopGoServer);
236
+ process.on("exit", stopGoServer);
237
+ process.on("SIGINT", () => {
238
+ stopGoServer();
239
+ process.exit();
240
+ });
241
+ process.on("SIGTERM", () => {
242
+ stopGoServer();
243
+ process.exit();
244
+ });
192
245
  },
193
246
  async buildStart() {
194
247
  await regenerate();
@@ -215,7 +268,7 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
215
268
  if (debounceTimer) clearTimeout(debounceTimer);
216
269
  debounceTimer = setTimeout(async () => {
217
270
  try {
218
- stopGoServer();
271
+ await stopGoServer();
219
272
  startGoServer();
220
273
  const changed = await regenerate();
221
274
  if (changed && devServer) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shiftapi/vite-plugin",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
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",