Better handle input for the --trigger-url option
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@trigger.dev/cli": patch
|
||||
---
|
||||
|
||||
Better handle input for the --trigger-url option
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user