Better handle input for the --trigger-url option

This commit is contained in:
Eric Allam
2023-06-30 11:26:42 +01:00
parent e4cca81124
commit ac371664af
2 changed files with 47 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/cli": patch
---
Better handle input for the --trigger-url option
+42 -1
View File
@@ -28,7 +28,8 @@ program
)
.option(
"-t, --trigger-url <trigger-url>",
"The URL of the Trigger.dev instance to use."
"The URL of the Trigger.dev instance to use.",
createUrlValidator("--trigger-url")
)
.version(getVersion(), "-v, --version", "Display the version number")
.action(async (options) => {
@@ -91,11 +92,22 @@ export const promptTriggerUrl = async (): Promise<string> => {
type: "input",
name: "triggerUrl",
message: "Enter the URL of your self-hosted Trigger.dev instance",
filter: (input) => {
return tryToCreateValidUrlFromValue(input);
},
validate: (input) => {
if (!input) {
return "Please enter the URL of your self-hosted Trigger.dev instance";
}
const possibleUrl = tryToCreateValidUrlFromValue(input);
try {
new URL(possibleUrl);
} catch (e) {
return "Please enter a valid URL";
}
return true;
},
});
@@ -199,3 +211,32 @@ function slugify(value: string): string {
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-|-$/g, "");
}
// Create a URL validator for a specific flag name
// If the input is a string like test-cloud.trigger.dev, we should automatically add https://
// If the input is a string like localhost:3030, we should automatically add http://
function createUrlValidator(flagName: string): (input: string) => string {
return (input: string) => {
try {
const possibleUrl = tryToCreateValidUrlFromValue(input);
new URL(possibleUrl);
return possibleUrl;
} catch (e) {
throw new Error(`Please enter a valid URL for the ${flagName} flag`);
}
};
}
function tryToCreateValidUrlFromValue(input: string): string {
let possibleUrl = input;
if (!input.startsWith("http://") && !input.startsWith("https://")) {
possibleUrl = input.includes("localhost")
? `http://${input}`
: `https://${input}`;
}
return possibleUrl;
}