Skip to content

Commit dc7f62a

Browse files
committed
feat: add yes flag as automatic answer to all prompts and run non-interactively.
- flag can be used with `--yes` or `--y`
1 parent 78acd24 commit dc7f62a

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

.changeset/olive-maps-promise.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
feat: add `--yes` with alias `--y` flag as automatic answer to all prompts and run `wrangler init` non-interactively.
6+
generated during setup:
7+
8+
- package.json
9+
- TypeScript, which includes tsconfig.json & @cloudflare/workers-types
10+
- Template "hello world" Worker at src/index.ts

packages/wrangler/src/__tests__/index.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,26 @@ describe("wrangler", () => {
716716
expect(tsconfigJson.compilerOptions).toEqual({});
717717
});
718718

719+
it("should initialize with no interactive prompts if `--yes` is used", async () => {
720+
await runWrangler("init --yes");
721+
722+
expect(fs.existsSync("./src/index.js")).toBe(false);
723+
expect(fs.existsSync("./src/index.ts")).toBe(true);
724+
expect(fs.existsSync("./tsconfig.json")).toBe(true);
725+
expect(fs.existsSync("./package.json")).toBe(true);
726+
expect(fs.existsSync("./wrangler.toml")).toBe(true);
727+
});
728+
729+
it("should initialize with no interactive prompts if `--y` is used", async () => {
730+
await runWrangler("init --y");
731+
732+
expect(fs.existsSync("./src/index.js")).toBe(false);
733+
expect(fs.existsSync("./src/index.ts")).toBe(true);
734+
expect(fs.existsSync("./tsconfig.json")).toBe(true);
735+
expect(fs.existsSync("./package.json")).toBe(true);
736+
expect(fs.existsSync("./wrangler.toml")).toBe(true);
737+
});
738+
719739
it("should error if `--type` is used", async () => {
720740
await expect(
721741
runWrangler("init --type")

packages/wrangler/src/index.tsx

+32-14
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,17 @@ export async function main(argv: string[]): Promise<void> {
211211
"init [name]",
212212
"📥 Create a wrangler.toml configuration file",
213213
(yargs) => {
214-
return yargs.positional("name", {
215-
describe: "The name of your worker.",
216-
type: "string",
217-
});
214+
return yargs
215+
.positional("name", {
216+
describe: "The name of your worker.",
217+
type: "string",
218+
})
219+
.option("yes", {
220+
describe:
221+
"Skip confirmation prompt, automatic yes to prompts; assume 'yes' as answer to all prompts and run non-interactively.",
222+
type: "boolean",
223+
alias: "y",
224+
});
218225
},
219226
async (args) => {
220227
if ("type" in args) {
@@ -259,11 +266,15 @@ export async function main(argv: string[]): Promise<void> {
259266

260267
let pathToPackageJson = await findUp("package.json");
261268
let shouldCreatePackageJson = false;
269+
const yesFlag = args.yes ?? false;
262270
if (!pathToPackageJson) {
263271
// If no package.json exists, ask to create one
264-
shouldCreatePackageJson = await confirm(
265-
"No package.json found. Would you like to create one?"
266-
);
272+
shouldCreatePackageJson = yesFlag
273+
? yesFlag
274+
: await confirm(
275+
"No package.json found. Would you like to create one?"
276+
);
277+
267278
if (shouldCreatePackageJson) {
268279
await writeFile(
269280
"./package.json",
@@ -298,9 +309,11 @@ export async function main(argv: string[]): Promise<void> {
298309
packageJson.dependencies?.wrangler
299310
)
300311
) {
301-
const shouldInstall = await confirm(
302-
"Would you like to install wrangler into your package.json?"
303-
);
312+
const shouldInstall = yesFlag
313+
? yesFlag
314+
: await confirm(
315+
"Would you like to install wrangler into your package.json?"
316+
);
304317
if (shouldInstall) {
305318
await packageManager.addDevDeps(`wrangler@${wranglerVersion}`);
306319
console.log(`✨ Installed wrangler`);
@@ -313,7 +326,7 @@ export async function main(argv: string[]): Promise<void> {
313326
if (!pathToTSConfig) {
314327
// If there's no tsconfig, offer to create one
315328
// and install @cloudflare/workers-types
316-
if (await confirm("Would you like to use TypeScript?")) {
329+
if (yesFlag || (await confirm("Would you like to use TypeScript?"))) {
317330
isTypescriptProject = true;
318331
await writeFile(
319332
"./tsconfig.json",
@@ -418,9 +431,14 @@ export async function main(argv: string[]): Promise<void> {
418431
}
419432
if (isTypescriptProject) {
420433
if (!fs.existsSync("./src/index.ts")) {
421-
const shouldCreateSource = await confirm(
422-
`Would you like to create a Worker at src/index.ts?`
423-
);
434+
let shouldCreateSource = false;
435+
436+
shouldCreateSource = yesFlag
437+
? yesFlag
438+
: await confirm(
439+
`Would you like to create a Worker at src/index.ts?`
440+
);
441+
424442
if (shouldCreateSource) {
425443
await mkdir("./src", { recursive: true });
426444
await writeFile(

0 commit comments

Comments
 (0)