-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite-plugin-check-provider.ts
More file actions
84 lines (72 loc) · 2.96 KB
/
vite-plugin-check-provider.ts
File metadata and controls
84 lines (72 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// biome-ignore-all lint/suspicious/noConsole: Dev plugin needs console output for user guidance
// biome-ignore-all lint/correctness/noProcessGlobal: Vite plugins need access to process.env
import type { Plugin, ViteDevServer } from "vite";
const PROVIDER_REPO = "https://github.com/pactflow/example-provider";
const DEFAULT_PROVIDER_URL = "http://localhost:8080";
async function checkProviderAvailability(
url: string,
): Promise<{ available: boolean; error?: string }> {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 2000);
const response = await fetch(`${url}/health`, {
signal: controller.signal,
});
clearTimeout(timeoutId);
return { available: response.ok };
} catch (error) {
return {
available: false,
error: error instanceof Error ? error.message : "Unknown error",
};
}
}
function printProviderInstructions(providerUrl: string): void {
console.log("\n");
console.log("╔═══════════════════════════════════════════════════════════╗");
console.log("║ ⚠️ Provider Not Running ║");
console.log("╚═══════════════════════════════════════════════════════════╝");
console.log("\n📋 This example requires a running provider service.\n");
console.log(`Expected provider at: ${providerUrl}\n`);
console.log("Quick Start:\n");
console.log(" 1. Clone the provider in a new terminal:");
console.log(` git clone ${PROVIDER_REPO}`);
console.log(" cd example-provider\n");
console.log(" 2. Install and start:");
console.log(" npm install");
console.log(" npm run dev\n");
console.log(
" 3. Refresh this page once the provider is running at port 8080\n",
);
}
export function checkProviderPlugin(): Plugin {
let server: ViteDevServer;
let hasChecked = false;
return {
name: "check-provider",
configureServer(devServer) {
server = devServer;
// Check on server start
server.httpServer?.once("listening", async () => {
if (hasChecked) return;
hasChecked = true;
const providerUrl =
process.env.VITE_API_BASE_URL || DEFAULT_PROVIDER_URL;
// Only check if a provider URL is configured
if (!providerUrl) {
console.log(
"\n⚠️ No VITE_API_BASE_URL configured. API calls will fail.\n",
);
printProviderInstructions(DEFAULT_PROVIDER_URL);
return;
}
const result = await checkProviderAvailability(providerUrl);
if (!result.available) {
printProviderInstructions(providerUrl);
} else {
console.log(`\n✅ Provider available at ${providerUrl}\n`);
}
});
},
};
}