Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-camels-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

Add `--dry-run` flag to `wrangler setup` and also a `dryRun` option to `runAutoConfig`
69 changes: 67 additions & 2 deletions packages/wrangler/src/__tests__/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ describe("wrangler setup", () => {
-v, --version Show version number [boolean]

OPTIONS
-y, --yes Answer \\"yes\\" to any prompts for configuring your project [boolean] [default: false]
--build Run your project's build command once it has been configured [boolean] [default: false]"
-y, --yes Answer \\"yes\\" to any prompts for configuring your project [boolean] [default: false]
--build Run your project's build command once it has been configured [boolean] [default: false]
--dry-run Runs the command without applying any filesystem modifications [boolean]"
`);
});

Expand Down Expand Up @@ -76,4 +77,68 @@ describe("wrangler setup", () => {
"🎉 Your project is now setup to deploy to Cloudflare"
);
});

describe("--dry-run", () => {
test("should stop before running autoconfig when project is already configured", async () => {
await seed({
"wrangler.jsonc": JSON.stringify({ name: "my-worker" }),
});

const runSpy = vi.spyOn(run, "runAutoConfig");

await runWrangler("setup --dry-run");

// autoconfig should _not_ have been run
expect(runSpy).not.toHaveBeenCalled();

expect(std.out).toContain(
"🎉 Your project is already setup to deploy to Cloudflare"
);
});

test("should run autoconfig when project is not configured and stop at the summary step", async () => {
await seed({
"public/index.html": `<h1>Hello World</h1>`,
});

await runWrangler("setup --dry-run");

expect(
std.out
.replace(/- Worker Name: .*?\n/, "- Worker Name: <WORKER_NAME>\n")
.replace(/"name": ".*?",\n/, '"name": "<WORKER_NAME>",\n')
.replace(/"directory": ".*?"/, '"directory": "<DIR>"')
.replace(
/"compatibility_date": "\d{4}-\d{2}-\d{2}"/,
'"compatibility_date": "yyyy-mm-dd"'
)
).toMatchInlineSnapshot(`
"
⛅️ wrangler x.x.x
──────────────────

Detected Project Settings:
- Worker Name: <WORKER_NAME>
- Framework: static
- Output Directory: <cwd>/public


📄 Create wrangler.jsonc:
{
\\"$schema\\": \\"node_modules/wrangler/config-schema.json\\",
\\"name\\": \\"<WORKER_NAME>\\",
\\"compatibility_date\\": \\"yyyy-mm-dd\\",
\\"observability\\": {
\\"enabled\\": true
},
\\"assets\\": {
\\"directory\\": \\"<DIR>\\"
}
}

✋ Autoconfig process run in dry-run mode, existing now.
"
`);
});
});
});
24 changes: 19 additions & 5 deletions packages/wrangler/src/autoconfig/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { installWrangler } from "./c3-vendor/packages";
import { confirmAutoConfigDetails, displayAutoConfigDetails } from "./details";
import { Static } from "./frameworks/static";
import { usesTypescript } from "./uses-typescript";
import type { AutoConfigDetails } from "./types";
import type { AutoConfigDetails, AutoConfigOptions } from "./types";
import type { RawConfig } from "@cloudflare/workers-utils";

type AutoConfigMetrics = Pick<
Expand All @@ -25,8 +25,13 @@ type AutoConfigMetrics = Pick<

export async function runAutoConfig(
autoConfigDetails: AutoConfigDetails,
{ build = true, skipConfirmation = false } = {}
autoConfigOptions: AutoConfigOptions = {}
): Promise<void> {
const dryRun = autoConfigOptions.dryRun === true;
const runBuild = !dryRun && (autoConfigOptions.runBuild ?? true);
const skipConfirmations =
dryRun || autoConfigOptions.skipConfirmations === true;

const detected: AutoConfigMetrics = {
buildCommand: autoConfigDetails.buildCommand,
outputDir: autoConfigDetails.outputDir,
Expand All @@ -36,12 +41,13 @@ export async function runAutoConfig(
"autoconfig detected",
{
detected,
options: autoConfigOptions,
},
{}
);
displayAutoConfigDetails(autoConfigDetails);

const updatedAutoConfigDetails = skipConfirmation
const updatedAutoConfigDetails = skipConfirmations
? autoConfigDetails
: await confirmAutoConfigDetails(autoConfigDetails);

Expand Down Expand Up @@ -76,10 +82,18 @@ export async function runAutoConfig(
})),
});

if (!(skipConfirmation || (await confirm("Proceed with setup?")))) {
if (!(skipConfirmations || (await confirm("Proceed with setup?")))) {
throw new FatalError("Deployment aborted");
}

if (dryRun) {
logger.log(
`✋ ${"Autoconfig process run in dry-run mode, existing now."}`
);
logger.log("");
return;
}

logger.debug(
`Running autoconfig with:\n${JSON.stringify(autoConfigDetails, null, 2)}...`
);
Expand Down Expand Up @@ -124,7 +138,7 @@ export async function runAutoConfig(
addWranglerToAssetsIgnore(autoConfigDetails.projectPath);
}

if (autoConfigDetails.buildCommand && build) {
if (autoConfigDetails.buildCommand && runBuild) {
await runCommand(
autoConfigDetails.buildCommand,
autoConfigDetails.projectPath,
Expand Down
17 changes: 17 additions & 0 deletions packages/wrangler/src/autoconfig/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,20 @@ export type AutoConfigDetails = {
/** The output directory (if no framework is used, points to the raw asset files) */
outputDir?: string;
};

export type AutoConfigOptions = {
/** Whether to run autoconfig without actually applying any filesystem modification (default: false) */
dryRun?: boolean;
/**
* Whether the build command should be run (default: true)
*
* Note: When `dryRun` is `true` the build command is never run.
*/
runBuild?: boolean;
/**
* Whether the confirmation prompts should be skipped (default: false)
*
* Note: When `dryRun` is `true` the the confirmation prompts are always skipped.
*/
skipConfirmations?: boolean;
};
24 changes: 17 additions & 7 deletions packages/wrangler/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export const setupCommand = createCommand({
type: "boolean",
default: false,
},
"dry-run": {
describe:
"Runs the command without applying any filesystem modifications",
type: "boolean",
},
},

async handler(args, { config }) {
Expand All @@ -33,16 +38,21 @@ export const setupCommand = createCommand({
// Only run auto config if the project is not already configured
if (!details.configured) {
await runAutoConfig(details, {
build: args.build,
skipConfirmation: args.yes,
runBuild: args.build,
skipConfirmations: args.yes,
dryRun: args.dryRun,
});
logger.log("🎉 Your project is now setup to deploy to Cloudflare");
if (!args.dryRun) {
logger.log("🎉 Your project is now setup to deploy to Cloudflare");
}
} else {
logger.log("🎉 Your project is already setup to deploy to Cloudflare");
}
const { type } = await getPackageManager();
logger.log(
`You can now deploy with ${brandColor(details.packageJson ? `${type} run deploy` : "wrangler deploy")}`
);
if (!args.dryRun) {
const { type } = await getPackageManager();
logger.log(
`You can now deploy with ${brandColor(details.packageJson ? `${type} run deploy` : "wrangler deploy")}`
);
}
},
});
Loading