From f0d304d53236ce12dfe7c18d6189470bb0302db4 Mon Sep 17 00:00:00 2001 From: Jacob M-G Evans Date: Fri, 11 Feb 2022 15:37:30 -0600 Subject: [PATCH] feat: add `yes` flag as automatic answer to all prompts and run non-interactively. - flag can be used with `--yes` or `--y` --- .changeset/olive-maps-promise.md | 10 +++++ packages/wrangler/src/__tests__/index.test.ts | 20 +++++++++ packages/wrangler/src/index.tsx | 45 +++++++++++++------ 3 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 .changeset/olive-maps-promise.md diff --git a/.changeset/olive-maps-promise.md b/.changeset/olive-maps-promise.md new file mode 100644 index 000000000000..c4cc0f31e848 --- /dev/null +++ b/.changeset/olive-maps-promise.md @@ -0,0 +1,10 @@ +--- +"wrangler": patch +--- + +feat: add `--yes` with alias `--y` flag as automatic answer to all prompts and run `wrangler init` non-interactively. +generated during setup: + +- package.json +- TypeScript, which includes tsconfig.json & `@cloudflare/workers-types` +- Template "hello world" Worker at src/index.ts diff --git a/packages/wrangler/src/__tests__/index.test.ts b/packages/wrangler/src/__tests__/index.test.ts index 1fc8ceea70b2..b408ad898e0d 100644 --- a/packages/wrangler/src/__tests__/index.test.ts +++ b/packages/wrangler/src/__tests__/index.test.ts @@ -716,6 +716,26 @@ describe("wrangler", () => { expect(tsconfigJson.compilerOptions).toEqual({}); }); + it("should initialize with no interactive prompts if `--yes` is used", async () => { + await runWrangler("init --yes"); + + expect(fs.existsSync("./src/index.js")).toBe(false); + expect(fs.existsSync("./src/index.ts")).toBe(true); + expect(fs.existsSync("./tsconfig.json")).toBe(true); + expect(fs.existsSync("./package.json")).toBe(true); + expect(fs.existsSync("./wrangler.toml")).toBe(true); + }); + + it("should initialize with no interactive prompts if `--y` is used", async () => { + await runWrangler("init -y"); + + expect(fs.existsSync("./src/index.js")).toBe(false); + expect(fs.existsSync("./src/index.ts")).toBe(true); + expect(fs.existsSync("./tsconfig.json")).toBe(true); + expect(fs.existsSync("./package.json")).toBe(true); + expect(fs.existsSync("./wrangler.toml")).toBe(true); + }); + it("should error if `--type` is used", async () => { await expect( runWrangler("init --type") diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 1a8c0063dc2b..23eb10e77255 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -211,10 +211,16 @@ export async function main(argv: string[]): Promise { "init [name]", "📥 Create a wrangler.toml configuration file", (yargs) => { - return yargs.positional("name", { - describe: "The name of your worker.", - type: "string", - }); + return yargs + .positional("name", { + describe: "The name of your worker.", + type: "string", + }) + .option("yes", { + describe: 'Answer "yes" to any prompts for new projects.', + type: "boolean", + alias: "y", + }); }, async (args) => { if ("type" in args) { @@ -259,11 +265,15 @@ export async function main(argv: string[]): Promise { let pathToPackageJson = await findUp("package.json"); let shouldCreatePackageJson = false; + const yesFlag = args.yes ?? false; if (!pathToPackageJson) { // If no package.json exists, ask to create one - shouldCreatePackageJson = await confirm( - "No package.json found. Would you like to create one?" - ); + shouldCreatePackageJson = + yesFlag || + (await confirm( + "No package.json found. Would you like to create one?" + )); + if (shouldCreatePackageJson) { await writeFile( "./package.json", @@ -298,9 +308,11 @@ export async function main(argv: string[]): Promise { packageJson.dependencies?.wrangler ) ) { - const shouldInstall = await confirm( - "Would you like to install wrangler into your package.json?" - ); + const shouldInstall = + yesFlag || + (await confirm( + "Would you like to install wrangler into your package.json?" + )); if (shouldInstall) { await packageManager.addDevDeps(`wrangler@${wranglerVersion}`); console.log(`✨ Installed wrangler`); @@ -313,7 +325,7 @@ export async function main(argv: string[]): Promise { if (!pathToTSConfig) { // If there's no tsconfig, offer to create one // and install @cloudflare/workers-types - if (await confirm("Would you like to use TypeScript?")) { + if (yesFlag || (await confirm("Would you like to use TypeScript?"))) { isTypescriptProject = true; await writeFile( "./tsconfig.json", @@ -418,9 +430,14 @@ export async function main(argv: string[]): Promise { } if (isTypescriptProject) { if (!fs.existsSync("./src/index.ts")) { - const shouldCreateSource = await confirm( - `Would you like to create a Worker at src/index.ts?` - ); + let shouldCreateSource = false; + + shouldCreateSource = + yesFlag || + (await confirm( + `Would you like to create a Worker at src/index.ts?` + )); + if (shouldCreateSource) { await mkdir("./src", { recursive: true }); await writeFile(