@shiftapi/vite-plugin 0.0.1 → 0.0.4
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/LICENSE +21 -0
- package/dist/index.js +66 -13
- package/package.json +18 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Frank Chiarulli Jr.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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(
|
|
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
|
-
"
|
|
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(
|
|
186
|
+
console.log(
|
|
187
|
+
`[shiftapi] Go server starting on port ${goPort}: go run ${serverEntry}`
|
|
188
|
+
);
|
|
162
189
|
}
|
|
163
190
|
function stopGoServer() {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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] =
|
|
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,7 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shiftapi/vite-plugin",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Vite plugin for fully-typed TypeScript clients from shiftapi Go servers",
|
|
5
|
+
"author": "Frank Chiarulli Jr. <frank@frankchiarulli.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/fcjr/shiftapi",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/fcjr/shiftapi",
|
|
11
|
+
"directory": "packages/vite-plugin"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/fcjr/shiftapi/issues"
|
|
15
|
+
},
|
|
5
16
|
"type": "module",
|
|
6
17
|
"main": "dist/index.js",
|
|
7
18
|
"types": "dist/index.d.ts",
|
|
@@ -18,11 +29,6 @@
|
|
|
18
29
|
"dist",
|
|
19
30
|
"virtual.d.ts"
|
|
20
31
|
],
|
|
21
|
-
"scripts": {
|
|
22
|
-
"build": "tsup src/index.ts --format esm --dts",
|
|
23
|
-
"dev": "tsup src/index.ts --format esm --dts --watch",
|
|
24
|
-
"test": "vitest run"
|
|
25
|
-
},
|
|
26
32
|
"peerDependencies": {
|
|
27
33
|
"vite": "^5.0.0 || ^6.0.0"
|
|
28
34
|
},
|
|
@@ -37,5 +43,10 @@
|
|
|
37
43
|
"typescript": "^5.5.0",
|
|
38
44
|
"vite": "^6.0.0",
|
|
39
45
|
"vitest": "^2.0.0"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
49
|
+
"dev": "tsup src/index.ts --format esm --dts --watch",
|
|
50
|
+
"test": "vitest run"
|
|
40
51
|
}
|
|
41
|
-
}
|
|
52
|
+
}
|