Repository navigation
Expand file tree
/
Copy pathastro-ngrok.js
More file actions
58 lines (53 loc) · 1.67 KB
/
Copy pathastro-ngrok.js
File metadata and controls
58 lines (53 loc) · 1.67 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
import { defineIntegration } from "astro";
import ngrok from "@ngrok/ngrok";
import dotenv from "dotenv";
dotenv.config();
export default function ngrokIntegration({ authtoken, port }) {
return {
name: "ngrok-integration",
hooks: {
"astro:server:start": async () => {
let ngrokListener;
try {
const ngrokEnvToken = process.env.NGROK_AUTHTOKEN;
// Authenticate ngrok with authtoken if provided
if (!!ngrokEnvToken) {
ngrokListener = await ngrok.forward({
authtoken: ngrokEnvToken,
addr: port,
});
} else {
ngrokListener = await ngrok.forward({
addr: port,
authtoken_from_env: true,
});
}
// Start ngrok with the specified port
const url = await ngrokListener.url();
console.log("NGROK Base URL", url);
console.log(`Webflow Redirect URI: ${url}/api/auth`);
} catch (err) {
console.error("Failed to start ngrok tunnel:", err);
}
const disconnectNgrok = async () => {
try {
await ngrokListener.close();
// await ngrok.disconnect(ngrokListener.url());
console.log("ngrok tunnel disconnected");
process.exit(0);
} catch (err) {
console.error("Failed to disconnect ngrok tunnel:", err);
process.exit(1);
}
};
process.on("SIGTERM", async () => {
await disconnectNgrok();
});
process.on("SIGINT", async () => {
await disconnectNgrok();
process.exit(0);
});
},
},
};
}