Skip to content

Commit

Permalink
feat: add yes flag as automatic answer to all prompts and run non-i…
Browse files Browse the repository at this point in the history
…nteractively.

- flag can be used with `--yes` or `--y`
  • Loading branch information
JacobMGEvans committed Feb 11, 2022
1 parent 78acd24 commit f0d304d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
10 changes: 10 additions & 0 deletions .changeset/olive-maps-promise.md
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions packages/wrangler/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
45 changes: 31 additions & 14 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,16 @@ export async function main(argv: string[]): Promise<void> {
"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) {
Expand Down Expand Up @@ -259,11 +265,15 @@ export async function main(argv: string[]): Promise<void> {

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",
Expand Down Expand Up @@ -298,9 +308,11 @@ export async function main(argv: string[]): Promise<void> {
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`);
Expand All @@ -313,7 +325,7 @@ export async function main(argv: string[]): Promise<void> {
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",
Expand Down Expand Up @@ -418,9 +430,14 @@ export async function main(argv: string[]): Promise<void> {
}
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(
Expand Down

0 comments on commit f0d304d

Please sign in to comment.