Skip to content

Commit 460f703

Browse files
authoredMay 16, 2023
[C3] Printing dummy questions for values provided via command line (#3237)

File tree

4 files changed

+86
-45
lines changed

4 files changed

+86
-45
lines changed
 

‎packages/create-cloudflare/src/cli.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ const parseArgs = async (argv: string[]) => {
3838
.option("framework", { type: "string" })
3939
.option("deploy", { type: "boolean" })
4040
.option("ts", { type: "boolean" })
41+
.option("open", {
42+
type: "boolean",
43+
default: true,
44+
description:
45+
"opens your browser after your deployment, set --no-open to disable",
46+
})
4147
.help().argv;
4248

4349
const [name] = args._;
@@ -56,7 +62,8 @@ const parseArgs = async (argv: string[]) => {
5662
const validateName = async (args: Partial<PagesGeneratorArgs>) => {
5763
const haikunator = new Haikunator();
5864

59-
args.projectName ||= await textInput({
65+
args.projectName = await textInput({
66+
initialValue: args.projectName,
6067
question: `Where do you want to create your application?`,
6168
helpText: "also used as application name",
6269
renderSubmitted: (value: string) => {
@@ -76,12 +83,13 @@ const validateType = async (args: PagesGeneratorArgs) => {
7683
.filter(([_, { hidden }]) => !hidden)
7784
.map(([value, { label }]) => ({ value, label }));
7885

79-
args.type ||= await selectInput({
86+
args.type = await selectInput({
8087
question: "What type of application do you want to create?",
8188
options: templateOptions,
8289
renderSubmitted: (option: Option) => {
8390
return `${brandColor("type")} ${dim(option.label)}`;
8491
},
92+
initialValue: args.type,
8593
});
8694

8795
if (!args.type || !Object.keys(templateMap).includes(args.type)) {

‎packages/create-cloudflare/src/common.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,17 @@ export const setupProjectDirectory = (args: PagesGeneratorArgs) => {
4949
};
5050

5151
export const offerToDeploy = async (ctx: PagesGeneratorContext) => {
52-
if (ctx.args.deploy === false) return;
53-
5452
startSection(`Deploy with Cloudflare`, `Step 3 of 3`);
5553

56-
ctx.args.deploy ||= await confirmInput({
54+
ctx.args.deploy = await confirmInput({
5755
question: "Do you want to deploy your application?",
5856
renderSubmitted: (value: boolean) =>
5957
`${brandColor(value ? `yes` : `no`)} ${dim(
6058
`deploying via \`${npm} run ${
6159
ctx.framework?.config.deployCommand ?? "deploy"
6260
}\``
6361
)}`,
62+
initialValue: ctx.args.deploy,
6463
});
6564

6665
if (!ctx.args.deploy) return;

‎packages/create-cloudflare/src/helpers/interactive.ts

+59-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TextPrompt, SelectPrompt, ConfirmPrompt } from "@clack/core";
22
import { isCancel } from "@clack/prompts";
33
import logUpdate from "log-update";
4-
import { shapes, cancel, space, status, newline } from "./cli";
4+
import { shapes, cancel, space, status, newline, logRaw } from "./cli";
55
import { blue, dim, gray, brandColor, bold } from "./colors";
66

77
const grayBar = gray(shapes.bar);
@@ -14,10 +14,12 @@ export type TextOptions = {
1414
defaultValue: string;
1515
helpText?: string;
1616
validate?: (value: string) => string | void;
17+
initialValue?: string;
1718
};
1819

1920
export const textInput = async (opts: TextOptions) => {
20-
const { renderSubmitted, question, defaultValue, validate } = opts;
21+
const { renderSubmitted, question, defaultValue, validate, initialValue } =
22+
opts;
2123
const helpText = opts.helpText || ``;
2224

2325
const prompt = new TextPrompt({
@@ -52,14 +54,21 @@ export const textInput = async (opts: TextOptions) => {
5254
},
5355
});
5456

55-
const value = await prompt.prompt();
56-
57-
if (isCancel(value)) {
58-
cancel("Operation cancelled.");
59-
process.exit(0);
57+
let value: string;
58+
if (initialValue) {
59+
logRaw(`${leftT} ${question}`);
60+
logRaw(`${grayBar} ${renderSubmitted(initialValue)}\n${grayBar}`);
61+
value = initialValue;
62+
} else {
63+
value = (await prompt.prompt()) as string;
64+
65+
if (isCancel(value)) {
66+
cancel("Operation cancelled.");
67+
process.exit(0);
68+
}
6069
}
6170

62-
return value as string;
71+
return value;
6372
};
6473

6574
export type Option = {
@@ -72,10 +81,11 @@ type SelectOptions = {
7281
renderSubmitted: (option: Option) => string;
7382
options: Option[];
7483
helpText?: string;
84+
initialValue?: string;
7585
};
7686

7787
export const selectInput = async (opts: SelectOptions) => {
78-
const { question, options, renderSubmitted } = opts;
88+
const { question, options, renderSubmitted, initialValue } = opts;
7989
const helpText = opts.helpText || ``;
8090

8191
const prompt = new SelectPrompt({
@@ -111,11 +121,24 @@ export const selectInput = async (opts: SelectOptions) => {
111121
},
112122
});
113123

114-
const value = await prompt.prompt();
115-
116-
if (isCancel(value)) {
117-
cancel("Operation cancelled.");
118-
process.exit(0);
124+
let value: string;
125+
if (initialValue) {
126+
logRaw(`${leftT} ${question}`);
127+
logRaw(
128+
`${grayBar} ${renderSubmitted({
129+
label: initialValue,
130+
value: initialValue,
131+
})}`
132+
);
133+
logRaw(`${grayBar}`);
134+
value = initialValue;
135+
} else {
136+
value = (await prompt.prompt()) as string;
137+
138+
if (isCancel(value)) {
139+
cancel("Operation cancelled.");
140+
process.exit(0);
141+
}
119142
}
120143

121144
return value as string;
@@ -128,11 +151,18 @@ type ConfirmOptions = {
128151
activeText?: string;
129152
inactiveText?: string;
130153
helpText?: string;
154+
initialValue?: boolean;
131155
};
132156

133157
export const confirmInput = async (opts: ConfirmOptions) => {
134-
const { activeText, inactiveText, question, renderSubmitted, defaultValue } =
135-
opts;
158+
const {
159+
activeText,
160+
inactiveText,
161+
question,
162+
renderSubmitted,
163+
defaultValue,
164+
initialValue,
165+
} = opts;
136166
const helpText = opts.helpText || `(y/n)`;
137167

138168
const active = activeText || "Yes";
@@ -163,11 +193,20 @@ export const confirmInput = async (opts: ConfirmOptions) => {
163193
},
164194
});
165195

166-
const value = Boolean(await prompt.prompt());
196+
let value: boolean;
197+
198+
if (initialValue !== undefined) {
199+
logRaw(`${leftT} ${question}`);
200+
logRaw(`${grayBar} ${renderSubmitted(initialValue)}`);
201+
logRaw(`${grayBar}`);
202+
value = initialValue;
203+
} else {
204+
value = Boolean(await prompt.prompt());
167205

168-
if (isCancel(value)) {
169-
cancel("Operation cancelled.");
170-
process.exit(0);
206+
if (isCancel(value)) {
207+
cancel("Operation cancelled.");
208+
process.exit(0);
209+
}
171210
}
172211

173212
return value;

‎packages/create-cloudflare/src/pages.ts

+15-20
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,21 @@ export const runPagesGenerator = async (args: PagesGeneratorArgs) => {
7070
};
7171

7272
const getFrameworkSelection = async (args: PagesGeneratorArgs) => {
73-
let framework: string;
74-
75-
if (args.framework) {
76-
framework = args.framework;
77-
} else {
78-
const frameworkOptions = Object.entries(FrameworkMap).map(
79-
([key, { displayName }]) => ({
80-
label: displayName,
81-
value: key,
82-
})
83-
);
84-
85-
framework = await selectInput({
86-
question: "Which development framework do you want to use?",
87-
options: frameworkOptions,
88-
renderSubmitted: (option: Option) => {
89-
return `${brandColor("framework")} ${dim(option.label)}`;
90-
},
91-
});
92-
}
73+
const frameworkOptions = Object.entries(FrameworkMap).map(
74+
([key, { displayName }]) => ({
75+
label: displayName,
76+
value: key,
77+
})
78+
);
79+
80+
const framework = await selectInput({
81+
question: "Which development framework do you want to use?",
82+
options: frameworkOptions,
83+
renderSubmitted: (option: Option) => {
84+
return `${brandColor("framework")} ${dim(option.label)}`;
85+
},
86+
initialValue: args.framework,
87+
});
9388

9489
// Validate answers
9590
framework || crash("A framework must be selected to continue.");

0 commit comments

Comments
 (0)
Please sign in to comment.