The endpoint gets automatically refreshed when files are changed, with a throttle

This commit is contained in:
Matt Aitken
2023-06-29 22:19:55 +01:00
parent 90abcb2cf7
commit c97ca350ad
3 changed files with 50 additions and 6 deletions
+1
View File
@@ -55,6 +55,7 @@
"dependencies": {
"@types/degit": "^2.8.3",
"chalk": "^5.2.0",
"chokidar": "^3.5.3",
"commander": "^9.4.1",
"degit": "^2.8.4",
"execa": "^7.0.0",
+47 -6
View File
@@ -6,6 +6,7 @@ import { resolvePath } from "../utils/parseNameAndPath.js";
import ngrok from "ngrok";
import { z } from "zod";
import { TriggerApi } from "../utils/triggerApi.js";
import chokidar from "chokidar";
export const DevCommandOptionsSchema = z.object({
port: z.coerce.number(),
@@ -13,6 +14,18 @@ export const DevCommandOptionsSchema = z.object({
tunnel: z.union([z.literal("ngrok"), z.literal("localtunnel")]),
});
const compileTimeWaitMs = 300;
const throttleTimeMs = 1000;
const formattedDate = new Intl.DateTimeFormat("en", {
year: "numeric",
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
});
export async function devCommand(path: string, anyOptions: any) {
const result = DevCommandOptionsSchema.safeParse(anyOptions);
if (!result.success) {
@@ -54,16 +67,28 @@ export async function devCommand(path: string, anyOptions: any) {
const tunnelUrl = await createTunnel(options.port);
logger.success(`🚇 Created tunnel: ${tunnelUrl}`);
logger.info(`Connecting to Trigger.dev...`);
logger.info(`🔌 Connecting to Trigger.dev...`);
//wait 200ms
// await wait(200);
//do initial refresh of the endpoint
//todo get path for API
await refreshEndpoint(apiClient, endpointId, tunnelUrl);
logger.success(`🔄 Updated your Jobs`);
//refresh function
const refresh = async () => {
//compiling takes a bit of time
await wait(compileTimeWaitMs);
const result = await refreshEndpoint(apiClient, endpointId, tunnelUrl);
if (result?.updatedAt) {
logger.success(
`🔄 Updated your Jobs ${formattedDate.format(
new Date(result.updatedAt)
)}`
);
}
};
// Watch for changes to .ts files and call triggerApi.indexEndpoint
// Watch for changes to .ts files and refresh endpoints
chokidar.watch(resolvedPath).on("all", (event, path) => {
throttle(refresh, throttleTimeMs);
});
}
async function getEndpointIdFromPackageJson(path: string) {
@@ -139,3 +164,19 @@ async function refreshEndpoint(
return response.data;
}
//wait function
async function wait(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
//throttle function
let throttleTimeout: NodeJS.Timeout | null = null;
function throttle(fn: () => any, delay: number) {
if (throttleTimeout) {
clearTimeout(throttleTimeout);
}
throttleTimeout = setTimeout(fn, delay);
}
+2
View File
@@ -637,6 +637,7 @@ importers:
'@types/node': '16'
'@types/node-fetch': ^2.6.2
chalk: ^5.2.0
chokidar: ^3.5.3
commander: ^9.4.1
degit: ^2.8.4
execa: ^7.0.0
@@ -658,6 +659,7 @@ importers:
dependencies:
'@types/degit': 2.8.3
chalk: 5.2.0
chokidar: 3.5.3
commander: 9.5.0
degit: 2.8.4
execa: 7.0.0