Skip to content

Commit

Permalink
Remove warnings, outdated templates, proxy commands to C3 (#6415)
Browse files Browse the repository at this point in the history
  • Loading branch information
irvinebroque authored Sep 13, 2024
1 parent 4653bba commit b27b741
Show file tree
Hide file tree
Showing 644 changed files with 58 additions and 54,443 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-toys-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

chore: Redirect `wrangler generate [template name]` and `wrangler init` to `npm create cloudflare`
92 changes: 24 additions & 68 deletions packages/wrangler/src/__tests__/generate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import { execa } from "execa";
import { vi } from "vitest";
import { getPackageManager } from "../package-manager";
import { mockConsoleMethods } from "./helpers/mock-console";
Expand Down Expand Up @@ -30,7 +31,7 @@ describe("generate", () => {
describe("cli functionality", () => {
afterEach(() => {});

it("defers to `wrangler init` when no template is given", async () => {
it("delegates to `wrangler init` when no template is given", async () => {
mockConfirm(
{
text: "Would you like to use git to manage this Worker?",
Expand All @@ -46,12 +47,30 @@ describe("generate", () => {
`"✨ Created no-template/wrangler.toml"`
);
expect(std.warn).toMatchInlineSnapshot(`
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mThe \`init\` command is no longer supported. Please use \`mockpm create cloudflare/@/^2.5.0 no-template\` instead.[0m
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mThe \`init\` command is no longer supported. Please use \`mockpm create cloudflare/@/^2.5.0 no-template\` instead.[0m
The \`init\` command will be removed in a future version.
The \`init\` command will be removed in a future version.
"
`);
"
`);
});

it("delegates to create cloudflare if Cloudflare template path is given", async () => {
await runWrangler("generate worker-name worker-d1");
expect(execa).toHaveBeenCalledWith(
"mockpm",
[
"create",
"cloudflare@^2.5.0",
"worker-name",
"--accept-defaults",
"--no-deploy",
"--no-open",
],
{
stdio: "inherit",
}
);
});

it("complains when given the --type argument", async () => {
Expand Down Expand Up @@ -127,25 +146,6 @@ describe("generate", () => {
});

describe("cloning", () => {
it("clones a cloudflare template with sparse checkouts", async () => {
await expect(
runWrangler("generate my-worker worker-typescript")
).resolves.toBeUndefined();

expect(readDirectory("my-worker")).toMatchObject<Directory>({
".git": expect.any(Object),
".gitignore": expect.any(String),
"README.md": expect.stringContaining("Template: worker-typescript"),
"package.json": expect.stringContaining("@cloudflare/workers-types"),
src: expect.objectContaining({
"index.ts": expect.any(String),
"index.test.ts": expect.any(String),
}),
"tsconfig.json": expect.any(String),
"wrangler.toml": expect.any(String),
});
});

// mocking out calls to either `isGitInstalled` or `execa("git", ["--version"])`
// was harder than i thought, leaving this for now.
it.todo("clones a cloudflare template with full checkouts");
Expand Down Expand Up @@ -223,50 +223,6 @@ describe("generate", () => {
"wrangler.toml": expect.any(String),
});
});

it("clones a cloudflare template across drives", async () => {
const fsMock = vi.spyOn(fs, "renameSync").mockImplementation(() => {
// Simulate the error we get if we use renameSync across different Windows drives (e.g. C: to D:).
const error = new Error("EXDEV: cross-device link not permitted");
// @ts-expect-error non standard property on Error
error.code = "EXDEV";
throw error;
});
await expect(
runWrangler("generate my-worker worker-typescript")
).resolves.toBeUndefined();

expect(readDirectory("my-worker")).toMatchObject<Directory>({
".git": expect.any(Object),
".gitignore": expect.any(String),
"README.md": expect.stringContaining("Template: worker-typescript"),
"package.json": expect.stringContaining("@cloudflare/workers-types"),
src: expect.objectContaining({
"index.ts": expect.any(String),
"index.test.ts": expect.any(String),
}),
"tsconfig.json": expect.any(String),
"wrangler.toml": expect.any(String),
});

fsMock.mockRestore();
});

it("mocks an error thrown", async () => {
const fsMock = vi.spyOn(fs, "renameSync").mockImplementation(() => {
// Simulate a different error to what we get if we use renameSync across different Windows drives.
const error = new Error("something");
// @ts-expect-error non standard property on Error
error.code = "unknown";
throw error;
});

await expect(
runWrangler("generate my-worker worker-typescript")
).rejects.toThrow();

fsMock.mockRestore();
});
});
});

Expand Down
38 changes: 29 additions & 9 deletions packages/wrangler/src/generate/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import fs from "node:fs";
import path from "node:path";
import { execa } from "execa";
import { getC3CommandFromEnv } from "../environment-variables/misc-variables";
import { UserError } from "../errors";
import { cloneIntoDirectory, initializeGit } from "../git-client";
import { CommandLineArgsError, printWranglerBanner } from "../index";
import { initHandler } from "../init";
import { logger } from "../logger";
import { getPackageManager } from "../package-manager";
import * as shellquote from "../utils/shell-quote";
import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
Expand Down Expand Up @@ -36,7 +40,6 @@ export function generateOptions(yargs: CommonYargsArgv) {
}
type GenerateArgs = StrictYargsOptionsToInterface<typeof generateOptions>;

// Originally, generate was a rust function: https://github.com/cloudflare/wrangler-legacy/blob/master/src/cli/mod.rs#L106-L123
export async function generateHandler(args: GenerateArgs) {
// somehow, `init` marks name as required but then also runs fine
// with the name omitted, and then substitutes it at runtime with ""
Expand Down Expand Up @@ -101,6 +104,27 @@ export async function generateHandler(args: GenerateArgs) {
throw new CommandLineArgsError(message);
}

if (isTemplateFolder(args.template)) {
logger.warn(
`Deprecation: \`wrangler generate\` is deprecated.\n` +
`Running \`npm create cloudflare@latest\` for you instead.\n`
);

const packageManager = await getPackageManager(process.cwd());

const c3Arguments = [
...shellquote.parse(getC3CommandFromEnv()),
...(packageManager.type === "npm" ? ["--"] : []),
args.name,
"--accept-defaults",
"--no-deploy",
"--no-open",
];

await execa(packageManager.type, c3Arguments, { stdio: "inherit" });
return;
}

logger.log(
`Creating a worker in ${path.basename(creationDirectory)} from ${
args.template
Expand Down Expand Up @@ -275,14 +299,6 @@ function parseTemplatePath(templatePath: string): {
remote: string;
subdirectory?: string;
} {
if (!templatePath.includes("/")) {
// template is a cloudflare canonical template, it doesn't include a slash in the name
return {
remote: "https://github.com/cloudflare/workers-sdk.git",
subdirectory: `templates/${templatePath}`,
};
}

const groups = TEMPLATE_REGEX.exec(templatePath)?.groups as unknown as
| TemplateRegexGroups
| undefined;
Expand All @@ -305,3 +321,7 @@ function parseTemplatePath(templatePath: string): {

return { remote, subdirectory };
}

function isTemplateFolder(templatePath: string): boolean {
return !templatePath.includes("/");
}
53 changes: 0 additions & 53 deletions templates/README.md

This file was deleted.

52 changes: 0 additions & 52 deletions templates/examples/ab-test.js

This file was deleted.

41 changes: 0 additions & 41 deletions templates/examples/aggregate-multiple-requests.js

This file was deleted.

26 changes: 0 additions & 26 deletions templates/examples/cryptocurrency-slack-bot/LICENSE

This file was deleted.

Loading

0 comments on commit b27b741

Please sign in to comment.