Skip to content

Commit

Permalink
feat(create-cloudflare): prompt instead of terminating if the argumen…
Browse files Browse the repository at this point in the history
…t is invalid
  • Loading branch information
edmundhung committed Oct 22, 2024
1 parent f9e9134 commit dad68bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
41 changes: 32 additions & 9 deletions packages/cli/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export type BasePromptConfig = {
renderers?: Partial<ReturnType<typeof getRenderers>>;
// Whether to throw an error if the prompt is crashed or cancelled
throwOnError?: boolean;
// Prompt instead of terminating if the default value is invalid (i.e. `acceptDefault` is true with invalid `defaultValue`)
promptOnDefaultInvalid?: boolean;
};

export type TextPromptConfig = BasePromptConfig & {
Expand Down Expand Up @@ -140,8 +142,24 @@ export const inputPrompt = async <T = string>(
| SelectRefreshablePrompt;

// Looks up the needed renderer by the current state ('initial', 'submitted', etc.)
const dispatchRender = (props: RenderProps, p: Prompt): string | void => {
const renderedLines = renderers[props.state](props, p);
const dispatchRender = (
props: RenderProps,
p: Prompt,
initialValue: Arg
): string | void => {
let state = props.state;

if (state === "initial" && promptConfig.acceptDefault) {
const error = promptConfig.validate?.(initialValue);

if (error) {
state = "error";
props.error = error;
props.value = initialValue;
}
}

const renderedLines = renderers[state](props, p);
return renderedLines.join("\n");
};

Expand All @@ -157,7 +175,7 @@ export const inputPrompt = async <T = string>(
options: promptConfig.options.filter((o) => !o.hidden),
initialValue,
render() {
return dispatchRender(this, prompt);
return dispatchRender(this, prompt, initialValue);
},
});
} else if (promptConfig.type === "confirm") {
Expand All @@ -173,7 +191,7 @@ export const inputPrompt = async <T = string>(
active: promptConfig.activeText || "",
inactive: promptConfig.inactiveText || "",
render() {
return dispatchRender(this, prompt);
return dispatchRender(this, prompt, initialValue);
},
});
} else if (promptConfig.type == "multiselect") {
Expand All @@ -193,7 +211,7 @@ export const inputPrompt = async <T = string>(
options: promptConfig.options,
initialValues: initialValues,
render() {
return dispatchRender(this, prompt);
return dispatchRender(this, prompt, initialValues);
},
});
} else if (promptConfig.type === "list") {
Expand All @@ -209,23 +227,28 @@ export const inputPrompt = async <T = string>(
promptConfig.onRefresh ?? (() => Promise.resolve(promptConfig.options)),
initialValue,
render() {
return dispatchRender(this, prompt);
return dispatchRender(this, prompt, initialValue);
},
});
} else {
const initialValue =
promptConfig.initialValue ?? String(promptConfig.defaultValue ?? "");

if (promptConfig.acceptDefault) {
return acceptDefault<T>(promptConfig, renderers, initialValue as T);
if (
!promptConfig.promptOnDefaultInvalid ||
!promptConfig.validate?.(initialValue as Arg)
) {
return acceptDefault<T>(promptConfig, renderers, initialValue as T);
}
}

prompt = new TextPrompt({
...promptConfig,
initialValue: promptConfig.initialValue,
initialValue: promptConfig.initialValue ?? initialValue,
defaultValue: String(promptConfig.defaultValue ?? ""),
render() {
return dispatchRender(this, prompt);
return dispatchRender(this, prompt, initialValue);
},
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/create-cloudflare/src/helpers/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ export const processArgument = async <Key extends keyof C3Args>(
acceptDefault: promptConfig.acceptDefault ?? value !== undefined,
defaultValue: value ?? promptConfig.defaultValue,
throwOnError: true,
promptOnDefaultInvalid: true,
});

// Update value in args before returning the result
Expand Down

0 comments on commit dad68bb

Please sign in to comment.