Skip to content

Commit 61d57e8

Browse files
RamIdeaspetebacondarwin
authored andcommittedMay 17, 2023
Un-revert #3107 and change --delegate-c3 default to true in v3 (#3211)
* [wrangler] Deprecate the init command (#3107) * set --delegate-c3 default to true * test deprecation (warning + c3 delegation) keep existing tests to ensure deprecated behaviour is retained with --no-delegate-c3

File tree

5 files changed

+2610
-2309
lines changed

5 files changed

+2610
-2309
lines changed
 

‎packages/wrangler/src/__tests__/generate.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "node:fs";
22
import path from "node:path";
3+
import { type PackageManager, getPackageManager } from "../package-manager";
34
import { mockConsoleMethods } from "./helpers/mock-console";
45
import { mockConfirm } from "./helpers/mock-dialogs";
56
import { useMockIsTTY } from "./helpers/mock-istty";
@@ -10,8 +11,17 @@ describe("generate", () => {
1011
runInTempDir();
1112
const { setIsTTY } = useMockIsTTY();
1213
const std = mockConsoleMethods();
14+
let mockPackageManager: PackageManager;
1315
beforeEach(() => {
1416
setIsTTY(true);
17+
18+
mockPackageManager = {
19+
cwd: process.cwd(),
20+
type: "mockpm" as "npm",
21+
addDevDeps: jest.fn(),
22+
install: jest.fn(),
23+
};
24+
(getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
1525
});
1626

1727
describe("cli functionality", () => {
@@ -32,6 +42,13 @@ describe("generate", () => {
3242
expect(std.out).toMatchInlineSnapshot(
3343
`"✨ Created no-template/wrangler.toml"`
3444
);
45+
expect(std.warn).toMatchInlineSnapshot(`
46+
"▲ [WARNING] The \`init\` command is no longer supported. Please use \`mockpm create cloudflare\` instead.
47+
48+
The \`init\` command will be removed in a future version.
49+
50+
"
51+
`);
3552
});
3653

3754
it("complains when given the --type argument", async () => {

‎packages/wrangler/src/__tests__/init.test.ts

+2,528-2,309
Large diffs are not rendered by default.

‎packages/wrangler/src/__tests__/jest.setup.ts

+13
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,16 @@ jest.mock("prompts", () => {
177177
}),
178178
};
179179
});
180+
181+
jest.mock("execa", () => {
182+
const realModule = jest.requireActual("execa");
183+
184+
return {
185+
...realModule,
186+
execa: jest.fn((...args: unknown[]) => {
187+
return args[0] === "mockpm"
188+
? Promise.resolve()
189+
: realModule.execa(...args);
190+
}),
191+
};
192+
});

‎packages/wrangler/src/generate/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export async function generateHandler(args: GenerateArgs) {
4646
site: undefined,
4747
yes: undefined,
4848
fromDash: undefined,
49+
delegateC3: false,
4950
v: undefined,
5051
config: undefined,
5152
env: undefined,

‎packages/wrangler/src/init.ts

+51
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from "node:fs";
22
import { writeFile, mkdir } from "node:fs/promises";
33
import path, { dirname } from "node:path";
44
import TOML from "@iarna/toml";
5+
import { execa } from "execa";
56
import { findUp } from "find-up";
67
import { version as wranglerVersion } from "../package.json";
78

@@ -59,6 +60,12 @@ export function initOptions(yargs: CommonYargsArgv) {
5960
"The name of the Worker you wish to download from the Cloudflare dashboard for local development.",
6061
type: "string",
6162
requiresArg: true,
63+
})
64+
.option("delegate-c3", {
65+
describe: "Delegate to Create Cloudflare CLI (C3)",
66+
type: "boolean",
67+
hidden: true,
68+
default: true,
6269
});
6370
}
6471

@@ -173,6 +180,10 @@ export async function initHandler(args: InitArgs) {
173180
let serviceMetadata: undefined | ServiceMetadataRes;
174181

175182
// If --from-dash, check that script actually exists
183+
/*
184+
Even though the init command is now deprecated and replaced by Create Cloudflare CLI (C3),
185+
run this first so, if the script doesn't exist, we can fail early
186+
*/
176187
if (fromDashScriptName) {
177188
const config = readConfig(args.config, args);
178189
accountId = await requireAuth(config);
@@ -188,6 +199,27 @@ export async function initHandler(args: InitArgs) {
188199
}
189200
throw err;
190201
}
202+
203+
// Deprecate the `init --from-dash` command
204+
const replacementC3Command = `\`${packageManager.type} create cloudflare --template pre-existing --name ${fromDashScriptName}\``;
205+
logger.warn(
206+
`The \`init --from-dash\` command is no longer supported. Please use ${replacementC3Command} instead.\nThe \`init\` command will be removed in a future version.`
207+
);
208+
209+
// C3 will run wrangler with the --do-not-delegate flag to communicate with the API
210+
if (args.delegateC3) {
211+
logger.log(`Running ${replacementC3Command}...`);
212+
213+
await execa(packageManager.type, [
214+
"create",
215+
"cloudflare",
216+
fromDashScriptName,
217+
"--type",
218+
"pre-existing",
219+
]);
220+
221+
return;
222+
}
191223
}
192224

193225
if (fs.existsSync(wranglerTomlDestination)) {
@@ -204,6 +236,25 @@ export async function initHandler(args: InitArgs) {
204236
return;
205237
}
206238
} else {
239+
// Deprecate the `init` command
240+
// if a wrangler.toml file does not exist (C3 expects to scaffold *new* projects)
241+
// and if --from-dash is not set (C3 will run wrangler to communicate with the API)
242+
if (!fromDashScriptName) {
243+
const replacementC3Command = `\`${packageManager.type} create cloudflare\``;
244+
245+
logger.warn(
246+
`The \`init\` command is no longer supported. Please use ${replacementC3Command} instead.\nThe \`init\` command will be removed in a future version.`
247+
);
248+
249+
if (args.delegateC3) {
250+
logger.log(`Running ${replacementC3Command}...`);
251+
252+
await execa(packageManager.type, ["create", "cloudflare"]);
253+
254+
return;
255+
}
256+
}
257+
207258
await mkdir(creationDirectory, { recursive: true });
208259
const compatibilityDate = new Date().toISOString().substring(0, 10);
209260

0 commit comments

Comments
 (0)
Please sign in to comment.