wrangler 2.6.2 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/wrangler.js +9 -1
- package/miniflare-dist/index.mjs +1 -1
- package/package.json +12 -10
- package/src/__tests__/api-dev.test.ts +65 -36
- package/src/__tests__/api-devregistry.test.js +14 -6
- package/src/__tests__/configuration.test.ts +2 -31
- package/src/__tests__/{d1.test.ts → d1/d1.test.ts} +48 -5
- package/src/__tests__/d1/splitter.test.ts +255 -0
- package/src/__tests__/delete.test.ts +5 -2
- package/src/__tests__/deployments.test.ts +20 -6
- package/src/__tests__/dev.test.tsx +52 -19
- package/src/__tests__/generate.test.ts +7 -4
- package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
- package/src/__tests__/helpers/mock-cfetch.ts +2 -57
- package/src/__tests__/helpers/mock-dialogs.ts +70 -86
- package/src/__tests__/helpers/mock-oauth-flow.ts +64 -49
- package/src/__tests__/helpers/mock-process.ts +8 -13
- package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
- package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
- package/src/__tests__/index.test.ts +46 -44
- package/src/__tests__/init.test.ts +761 -537
- package/src/__tests__/jest.setup.ts +20 -24
- package/src/__tests__/kv.test.ts +286 -173
- package/src/__tests__/logout.test.ts +1 -1
- package/src/__tests__/metrics.test.ts +5 -7
- package/src/__tests__/middleware.scheduled.test.ts +40 -30
- package/src/__tests__/middleware.test.ts +144 -120
- package/src/__tests__/pages.test.ts +1617 -1161
- package/src/__tests__/publish.test.ts +174 -125
- package/src/__tests__/r2.test.ts +2 -2
- package/src/__tests__/secret.test.ts +183 -126
- package/src/__tests__/tail.test.ts +6 -0
- package/src/__tests__/tsconfig-sanity.ts +12 -0
- package/src/__tests__/tsconfig.json +8 -0
- package/src/__tests__/tsconfig.tsbuildinfo +1 -0
- package/src/__tests__/whoami.test.tsx +1 -96
- package/src/api/dev.ts +78 -41
- package/src/api/index.ts +1 -1
- package/src/{bundle-reporter.tsx → bundle-reporter.ts} +0 -0
- package/src/cfetch/index.ts +0 -2
- package/src/cfetch/internal.ts +6 -15
- package/src/cli.ts +2 -2
- package/src/config/validation.ts +1 -2
- package/src/create-worker-upload-form.ts +2 -2
- package/src/d1/{delete.tsx → delete.ts} +0 -0
- package/src/d1/execute.tsx +8 -37
- package/src/d1/migrations/apply.tsx +29 -19
- package/src/d1/migrations/{index.tsx → index.ts} +0 -0
- package/src/d1/splitter.ts +161 -0
- package/src/d1/{types.tsx → types.ts} +0 -0
- package/src/delete.ts +3 -8
- package/src/deployments.ts +6 -0
- package/src/deprecated/index.ts +2 -295
- package/src/dev/dev.tsx +2 -2
- package/src/dev/{get-local-persistence-path.tsx → get-local-persistence-path.ts} +0 -0
- package/src/dev/local.tsx +16 -4
- package/src/dev/remote.tsx +28 -1
- package/src/dev/start-server.ts +19 -11
- package/src/dev/use-esbuild.ts +1 -1
- package/src/{dev-registry.tsx → dev-registry.ts} +0 -0
- package/src/dev.tsx +21 -2
- package/src/dialogs.ts +136 -0
- package/src/dispatch-namespace.ts +1 -1
- package/src/docs/index.ts +3 -0
- package/src/environment-variables/factory.ts +88 -0
- package/src/environment-variables/misc-variables.ts +30 -0
- package/src/generate/index.ts +300 -0
- package/src/{index.tsx → index.ts} +10 -13
- package/src/init.ts +92 -52
- package/src/jest.d.ts +4 -0
- package/src/logger.ts +15 -3
- package/src/metrics/metrics-config.ts +1 -1
- package/src/miniflare-cli/assets.ts +4 -0
- package/src/miniflare-cli/index.ts +1 -5
- package/src/miniflare-cli/tsconfig.json +9 -0
- package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
- package/src/miniflare-cli/types.ts +11 -0
- package/src/pages/{build.tsx → build.ts} +0 -0
- package/src/pages/{deployment-tails.tsx → deployment-tails.ts} +0 -0
- package/src/pages/{dev.tsx → dev.ts} +53 -55
- package/src/pages/functions/buildWorker.ts +1 -1
- package/src/pages/functions/tsconfig.json +8 -0
- package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
- package/src/pages/{functions.tsx → functions.ts} +0 -0
- package/src/pages/{hash.tsx → hash.ts} +0 -0
- package/src/pages/{index.tsx → index.ts} +0 -0
- package/src/pages/projects.tsx +3 -5
- package/src/pages/publish.tsx +5 -4
- package/src/pages/upload.tsx +1 -1
- package/src/publish/publish.ts +9 -7
- package/src/pubsub/{pubsub-commands.tsx → pubsub-commands.ts} +1 -1
- package/src/secret/index.ts +1 -1
- package/src/{sites.tsx → sites.ts} +0 -0
- package/src/tail/index.ts +2 -3
- package/src/tsconfig-sanity.ts +16 -0
- package/src/user/access.ts +0 -1
- package/src/user/auth-variables.ts +113 -0
- package/src/user/choose-account.tsx +1 -31
- package/src/user/index.ts +0 -1
- package/src/user/{user.tsx → user.ts} +107 -73
- package/src/{whoami.tsx → whoami.ts} +37 -71
- package/templates/__tests__/tsconfig-sanity.ts +12 -0
- package/templates/__tests__/tsconfig.json +8 -0
- package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
- package/templates/d1-beta-facade.js +36 -0
- package/templates/facade.d.ts +14 -0
- package/templates/first-party-worker-module-facade.ts +4 -3
- package/templates/format-dev-errors.ts +7 -6
- package/templates/init-tests/test-jest-new-worker.js +3 -5
- package/templates/init-tests/test-vitest-new-worker.js +3 -5
- package/templates/init-tests/test-vitest-new-worker.ts +25 -0
- package/templates/middleware/loader-modules.ts +0 -2
- package/templates/middleware/loader-sw.ts +6 -0
- package/templates/pages-dev-pipeline.ts +4 -1
- package/templates/pages-shim.ts +4 -1
- package/templates/pages-template-plugin.ts +12 -7
- package/templates/serve-static-assets.ts +16 -14
- package/templates/tsconfig-sanity.ts +11 -0
- package/templates/tsconfig.init.json +106 -0
- package/templates/tsconfig.json +5 -103
- package/templates/tsconfig.tsbuildinfo +1 -0
- package/wrangler-dist/cli.d.ts +58 -60
- package/wrangler-dist/cli.js +34440 -55514
- package/wrangler-dist/wasm-sync.wasm +0 -0
- package/src/__tests__/dialogs.test.tsx +0 -40
- package/src/dialogs.tsx +0 -168
- package/src/environment-variables.ts +0 -50
- package/src/user/env-vars.ts +0 -46
package/src/dev/use-esbuild.ts
CHANGED
|
File without changes
|
package/src/dev.tsx
CHANGED
|
@@ -30,7 +30,7 @@ import {
|
|
|
30
30
|
} from "./index";
|
|
31
31
|
import type { Config, Environment } from "./config";
|
|
32
32
|
import type { Route } from "./config/environment";
|
|
33
|
-
import type { EnablePagesAssetsServiceBindingOptions } from "./miniflare-cli";
|
|
33
|
+
import type { EnablePagesAssetsServiceBindingOptions } from "./miniflare-cli/types";
|
|
34
34
|
import type { CfWorkerInit } from "./worker";
|
|
35
35
|
import type { CommonYargsOptions } from "./yargs-types";
|
|
36
36
|
import type { Argv, ArgumentsCamelCase } from "yargs";
|
|
@@ -328,7 +328,7 @@ export function devOptions(yargs: Argv<CommonYargsOptions>): Argv<DevArgs> {
|
|
|
328
328
|
}
|
|
329
329
|
|
|
330
330
|
export async function devHandler(args: ArgumentsCamelCase<DevArgs>) {
|
|
331
|
-
if (!args.local) {
|
|
331
|
+
if (!(args.local || args.experimentalLocal)) {
|
|
332
332
|
const isLoggedIn = await loginOrRefreshIfRequired();
|
|
333
333
|
if (!isLoggedIn) {
|
|
334
334
|
throw new Error(
|
|
@@ -376,6 +376,7 @@ type StartDevOptions = ArgumentsCamelCase<DevArgs> &
|
|
|
376
376
|
// They aren't exposed as CLI arguments.
|
|
377
377
|
AdditionalDevProps & {
|
|
378
378
|
forceLocal?: boolean;
|
|
379
|
+
disableDevRegistry?: boolean;
|
|
379
380
|
enablePagesAssetsServiceBinding?: EnablePagesAssetsServiceBindingOptions;
|
|
380
381
|
};
|
|
381
382
|
|
|
@@ -512,11 +513,28 @@ export async function startDev(args: StartDevOptions) {
|
|
|
512
513
|
);
|
|
513
514
|
}
|
|
514
515
|
const devReactElement = render(await getDevReactElement(config));
|
|
516
|
+
|
|
517
|
+
// In the bootstrapper script `bin/wrangler.js`, we open an IPC channel, so
|
|
518
|
+
// IPC messages from this process are propagated through the bootstrapper.
|
|
519
|
+
// Normally, Node's SIGINT handler would close this for us, but interactive
|
|
520
|
+
// mode enables raw mode on stdin which disables the built-in handler. The
|
|
521
|
+
// following line disconnects from the IPC channel when we press `x` or
|
|
522
|
+
// CTRL-C in interactive mode, ensuring no open handles, and allowing for a
|
|
523
|
+
// clean exit. Note, if we called `stop()` using the dev API, we don't want
|
|
524
|
+
// to disconnect here, as the user may still need IPC. We also don't want
|
|
525
|
+
// to disconnect if this file was imported in Jest (not the case with E2E
|
|
526
|
+
// tests), as that would stop communication with the test runner.
|
|
527
|
+
let apiStopped = false;
|
|
528
|
+
void devReactElement.waitUntilExit().then(() => {
|
|
529
|
+
if (!apiStopped && typeof jest === "undefined") process.disconnect?.();
|
|
530
|
+
});
|
|
531
|
+
|
|
515
532
|
rerender = devReactElement.rerender;
|
|
516
533
|
return {
|
|
517
534
|
devReactElement,
|
|
518
535
|
watcher,
|
|
519
536
|
stop: async () => {
|
|
537
|
+
apiStopped = true;
|
|
520
538
|
devReactElement.unmount();
|
|
521
539
|
await watcher?.close();
|
|
522
540
|
},
|
|
@@ -627,6 +645,7 @@ export async function startApiDev(args: StartDevOptions) {
|
|
|
627
645
|
testScheduled: args.testScheduled,
|
|
628
646
|
experimentalLocal: args.experimentalLocal,
|
|
629
647
|
experimentalLocalRemoteKv: args.experimentalLocalRemoteKv,
|
|
648
|
+
disableDevRegistry: args.disableDevRegistry ?? false,
|
|
630
649
|
});
|
|
631
650
|
}
|
|
632
651
|
|
package/src/dialogs.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import prompts from "prompts";
|
|
3
|
+
import { CI } from "./is-ci";
|
|
4
|
+
import isInteractive from "./is-interactive";
|
|
5
|
+
import { logger } from "./logger";
|
|
6
|
+
|
|
7
|
+
// TODO: Use this function across the codebase.
|
|
8
|
+
function isNonInteractiveOrCI(): boolean {
|
|
9
|
+
return !isInteractive() || CI.isCI();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class NoDefaultValueProvided extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
// This is user-facing, so make the message something understandable
|
|
15
|
+
// It _should_ always be caught and replaced with a more descriptive error
|
|
16
|
+
// but this is fine as a fallback.
|
|
17
|
+
super("This command cannot be run in a non-interactive context");
|
|
18
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface ConfirmOptions {
|
|
23
|
+
defaultValue?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function confirm(
|
|
27
|
+
text: string,
|
|
28
|
+
{ defaultValue = true }: ConfirmOptions = {}
|
|
29
|
+
): Promise<boolean> {
|
|
30
|
+
if (isNonInteractiveOrCI()) {
|
|
31
|
+
logger.log(`? ${text}`);
|
|
32
|
+
logger.log(
|
|
33
|
+
`🤖 ${chalk.dim(
|
|
34
|
+
"Using default value in non-interactive context:"
|
|
35
|
+
)} ${chalk.white.bold(defaultValue ? "yes" : "no")}`
|
|
36
|
+
);
|
|
37
|
+
return defaultValue;
|
|
38
|
+
}
|
|
39
|
+
const { value } = await prompts({
|
|
40
|
+
type: "confirm",
|
|
41
|
+
name: "value",
|
|
42
|
+
message: text,
|
|
43
|
+
initial: defaultValue,
|
|
44
|
+
onState: (state) => {
|
|
45
|
+
if (state.aborted) {
|
|
46
|
+
process.nextTick(() => {
|
|
47
|
+
process.exit(1);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface PromptOptions {
|
|
56
|
+
defaultValue?: string;
|
|
57
|
+
isSecret?: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function prompt(
|
|
61
|
+
text: string,
|
|
62
|
+
options: PromptOptions = {}
|
|
63
|
+
): Promise<string> {
|
|
64
|
+
if (isNonInteractiveOrCI()) {
|
|
65
|
+
if (options?.defaultValue === undefined) {
|
|
66
|
+
throw new NoDefaultValueProvided();
|
|
67
|
+
}
|
|
68
|
+
logger.log(`? ${text}`);
|
|
69
|
+
logger.log(
|
|
70
|
+
`🤖 ${chalk.dim(
|
|
71
|
+
"Using default value in non-interactive context:"
|
|
72
|
+
)} ${chalk.white.bold(options.defaultValue)}`
|
|
73
|
+
);
|
|
74
|
+
return options.defaultValue;
|
|
75
|
+
}
|
|
76
|
+
const { value } = await prompts({
|
|
77
|
+
type: "text",
|
|
78
|
+
name: "value",
|
|
79
|
+
message: text,
|
|
80
|
+
initial: options?.defaultValue,
|
|
81
|
+
style: options?.isSecret ? "password" : "default",
|
|
82
|
+
onState: (state) => {
|
|
83
|
+
if (state.aborted) {
|
|
84
|
+
process.nextTick(() => {
|
|
85
|
+
process.exit(1);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface SelectOptions<Values> {
|
|
94
|
+
choices: SelectOption<Values>[];
|
|
95
|
+
defaultOption?: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
interface SelectOption<Values> {
|
|
99
|
+
title: string;
|
|
100
|
+
description?: string;
|
|
101
|
+
value: Values;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function select<Values extends string>(
|
|
105
|
+
text: string,
|
|
106
|
+
options: SelectOptions<Values>
|
|
107
|
+
): Promise<Values> {
|
|
108
|
+
if (isNonInteractiveOrCI()) {
|
|
109
|
+
if (options?.defaultOption === undefined) {
|
|
110
|
+
throw new NoDefaultValueProvided();
|
|
111
|
+
}
|
|
112
|
+
logger.log(`? ${text}`);
|
|
113
|
+
logger.log(
|
|
114
|
+
`🤖 ${chalk.dim(
|
|
115
|
+
"Using default value in non-interactive context:"
|
|
116
|
+
)} ${chalk.white.bold(options.choices[options.defaultOption].title)}`
|
|
117
|
+
);
|
|
118
|
+
return options.choices[options.defaultOption].value;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const { value } = await prompts({
|
|
122
|
+
type: "select",
|
|
123
|
+
name: "value",
|
|
124
|
+
message: text,
|
|
125
|
+
choices: options.choices,
|
|
126
|
+
initial: options.defaultOption,
|
|
127
|
+
onState: (state) => {
|
|
128
|
+
if (state.aborted) {
|
|
129
|
+
process.nextTick(() => {
|
|
130
|
+
process.exit(1);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
return value;
|
|
136
|
+
}
|
package/src/docs/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
import type { ArgumentsCamelCase, Argv } from "yargs";
|
|
12
12
|
|
|
13
13
|
const argToUrlHash = {
|
|
14
|
+
docs: "docs",
|
|
14
15
|
init: "init",
|
|
15
16
|
generate: "generate",
|
|
16
17
|
dev: "dev",
|
|
@@ -38,7 +39,9 @@ export function docsOptions(yargs: Argv<CommonYargsOptions>) {
|
|
|
38
39
|
type: "string",
|
|
39
40
|
// requiresArg: true,
|
|
40
41
|
choices: [
|
|
42
|
+
"docs",
|
|
41
43
|
"init",
|
|
44
|
+
"generate",
|
|
42
45
|
"dev",
|
|
43
46
|
"publish",
|
|
44
47
|
"delete",
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { logger } from "../logger";
|
|
2
|
+
|
|
3
|
+
type VariableNames =
|
|
4
|
+
| "CLOUDFLARE_ACCOUNT_ID"
|
|
5
|
+
| "CLOUDFLARE_API_TOKEN"
|
|
6
|
+
| "CLOUDFLARE_API_KEY"
|
|
7
|
+
| "CLOUDFLARE_EMAIL"
|
|
8
|
+
| "WRANGLER_SEND_METRICS"
|
|
9
|
+
| "CLOUDFLARE_API_BASE_URL"
|
|
10
|
+
| "WRANGLER_LOG"
|
|
11
|
+
| "WRANGLER_API_ENVIRONMENT"
|
|
12
|
+
| "WRANGLER_CLIENT_ID"
|
|
13
|
+
| "WRANGLER_AUTH_DOMAIN"
|
|
14
|
+
| "WRANGLER_AUTH_URL"
|
|
15
|
+
| "WRANGLER_TOKEN_URL"
|
|
16
|
+
| "WRANGLER_REVOKE_URL"
|
|
17
|
+
| "WRANGLER_CF_AUTHORIZATION_TOKEN";
|
|
18
|
+
|
|
19
|
+
type DeprecatedNames =
|
|
20
|
+
| "CF_ACCOUNT_ID"
|
|
21
|
+
| "CF_API_TOKEN"
|
|
22
|
+
| "CF_API_KEY"
|
|
23
|
+
| "CF_EMAIL"
|
|
24
|
+
| "CF_API_BASE_URL";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Create a function used to access an environment variable.
|
|
28
|
+
*
|
|
29
|
+
* This is not memoized to allow us to change the value at runtime, such as in testing.
|
|
30
|
+
* A warning is shown if the client is using a deprecated version - but only once.
|
|
31
|
+
*/
|
|
32
|
+
export function getEnvironmentVariableFactory({
|
|
33
|
+
variableName,
|
|
34
|
+
deprecatedName,
|
|
35
|
+
}: {
|
|
36
|
+
variableName: VariableNames;
|
|
37
|
+
deprecatedName?: DeprecatedNames;
|
|
38
|
+
}): () => string | undefined;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Create a function used to access an environment variable, with a default value.
|
|
42
|
+
*
|
|
43
|
+
* This is not memoized to allow us to change the value at runtime, such as in testing.
|
|
44
|
+
* A warning is shown if the client is using a deprecated version - but only once.
|
|
45
|
+
*/
|
|
46
|
+
export function getEnvironmentVariableFactory({
|
|
47
|
+
variableName,
|
|
48
|
+
deprecatedName,
|
|
49
|
+
defaultValue,
|
|
50
|
+
}: {
|
|
51
|
+
variableName: VariableNames;
|
|
52
|
+
deprecatedName?: DeprecatedNames;
|
|
53
|
+
defaultValue: () => string;
|
|
54
|
+
}): () => string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Create a function used to access an environment variable.
|
|
58
|
+
*
|
|
59
|
+
* This is not memoized to allow us to change the value at runtime, such as in testing.
|
|
60
|
+
* A warning is shown if the client is using a deprecated version - but only once.
|
|
61
|
+
*/
|
|
62
|
+
export function getEnvironmentVariableFactory({
|
|
63
|
+
variableName,
|
|
64
|
+
deprecatedName,
|
|
65
|
+
defaultValue,
|
|
66
|
+
}: {
|
|
67
|
+
variableName: VariableNames;
|
|
68
|
+
deprecatedName?: DeprecatedNames;
|
|
69
|
+
defaultValue?: () => string;
|
|
70
|
+
}): () => string | undefined {
|
|
71
|
+
let hasWarned = false;
|
|
72
|
+
return () => {
|
|
73
|
+
if (process.env[variableName]) {
|
|
74
|
+
return process.env[variableName];
|
|
75
|
+
} else if (deprecatedName && process.env[deprecatedName]) {
|
|
76
|
+
if (!hasWarned) {
|
|
77
|
+
// Only show the warning once.
|
|
78
|
+
hasWarned = true;
|
|
79
|
+
logger.warn(
|
|
80
|
+
`Using "${deprecatedName}" environment variable. This is deprecated. Please use "${variableName}", instead.`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return process.env[deprecatedName];
|
|
84
|
+
} else {
|
|
85
|
+
return defaultValue?.();
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { getEnvironmentVariableFactory } from "./factory";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `WRANGLER_SEND_METRICS` can override whether we attempt to send metrics information to Sparrow.
|
|
5
|
+
*/
|
|
6
|
+
export const getWranglerSendMetricsFromEnv = getEnvironmentVariableFactory({
|
|
7
|
+
variableName: "WRANGLER_SEND_METRICS",
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Set `WRANGLER_API_ENVIRONMENT` environment variable to "staging" to tell Wrangler to hit the staging APIs rather than production.
|
|
12
|
+
*/
|
|
13
|
+
export const getCloudflareApiEnvironmentFromEnv = getEnvironmentVariableFactory(
|
|
14
|
+
{
|
|
15
|
+
variableName: "WRANGLER_API_ENVIRONMENT",
|
|
16
|
+
defaultValue: () => "production",
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `CLOUDFLARE_API_BASE_URL` specifies the URL to the Cloudflare API.
|
|
22
|
+
*/
|
|
23
|
+
export const getCloudflareApiBaseUrl = getEnvironmentVariableFactory({
|
|
24
|
+
variableName: "CLOUDFLARE_API_BASE_URL",
|
|
25
|
+
deprecatedName: "CF_API_BASE_URL",
|
|
26
|
+
defaultValue: () =>
|
|
27
|
+
getCloudflareApiEnvironmentFromEnv() === "staging"
|
|
28
|
+
? "https://api.staging.cloudflare.com/client/v4"
|
|
29
|
+
: "https://api.cloudflare.com/client/v4",
|
|
30
|
+
});
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { cloneIntoDirectory, initializeGit } from "../git-client";
|
|
4
|
+
import { CommandLineArgsError, printWranglerBanner } from "../index";
|
|
5
|
+
import { initHandler } from "../init";
|
|
6
|
+
import { logger } from "../logger";
|
|
7
|
+
import type {
|
|
8
|
+
CommonYargsOptions,
|
|
9
|
+
YargsOptionsToInterface,
|
|
10
|
+
} from "../yargs-types";
|
|
11
|
+
import type { Argv } from "yargs";
|
|
12
|
+
|
|
13
|
+
export function generateOptions(yargs: Argv<CommonYargsOptions>) {
|
|
14
|
+
return yargs
|
|
15
|
+
.positional("name", {
|
|
16
|
+
describe: "Name of the Workers project",
|
|
17
|
+
type: "string",
|
|
18
|
+
demandOption: true,
|
|
19
|
+
})
|
|
20
|
+
.positional("template", {
|
|
21
|
+
type: "string",
|
|
22
|
+
describe: "The URL of a GitHub template",
|
|
23
|
+
})
|
|
24
|
+
.option("type", {
|
|
25
|
+
alias: "t",
|
|
26
|
+
type: "string",
|
|
27
|
+
hidden: true,
|
|
28
|
+
deprecated: true,
|
|
29
|
+
})
|
|
30
|
+
.option("site", {
|
|
31
|
+
alias: "s",
|
|
32
|
+
type: "boolean",
|
|
33
|
+
hidden: true,
|
|
34
|
+
deprecated: true,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
type GenerateArgs = YargsOptionsToInterface<typeof generateOptions>;
|
|
38
|
+
|
|
39
|
+
// Originally, generate was a rust function: https://github.com/cloudflare/wrangler/blob/master/src/cli/mod.rs#L106-L123
|
|
40
|
+
export async function generateHandler(args: GenerateArgs) {
|
|
41
|
+
// somehow, `init` marks name as required but then also runs fine
|
|
42
|
+
// with the name omitted, and then substitutes it at runtime with ""
|
|
43
|
+
// delegate to `wrangler init` if no template is specified
|
|
44
|
+
if (args.template === undefined) {
|
|
45
|
+
return initHandler({
|
|
46
|
+
name: args.name,
|
|
47
|
+
template: undefined,
|
|
48
|
+
site: undefined,
|
|
49
|
+
yes: undefined,
|
|
50
|
+
fromDash: undefined,
|
|
51
|
+
"from-dash": undefined,
|
|
52
|
+
v: undefined,
|
|
53
|
+
config: undefined,
|
|
54
|
+
env: undefined,
|
|
55
|
+
type: undefined,
|
|
56
|
+
_: args._,
|
|
57
|
+
$0: args.$0,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// print down here cuz `init` prints it own its own
|
|
62
|
+
await printWranglerBanner();
|
|
63
|
+
|
|
64
|
+
if (args.type) {
|
|
65
|
+
let message = "The --type option is no longer supported.";
|
|
66
|
+
if (args.type === "webpack") {
|
|
67
|
+
message +=
|
|
68
|
+
"\nIf you wish to use webpack then you will need to create a custom build.";
|
|
69
|
+
// TODO: Add a link to docs
|
|
70
|
+
}
|
|
71
|
+
throw new CommandLineArgsError(message);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (args.name && isRemote(args.name)) {
|
|
75
|
+
[args.template, args.name] = [args.name, args.template];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const creationDirectory = generateWorkerDirectoryName(args.name);
|
|
79
|
+
|
|
80
|
+
if (args.site) {
|
|
81
|
+
const gitDirectory =
|
|
82
|
+
creationDirectory !== process.cwd()
|
|
83
|
+
? path.basename(creationDirectory)
|
|
84
|
+
: "my-site";
|
|
85
|
+
const message =
|
|
86
|
+
"The --site option is no longer supported.\n" +
|
|
87
|
+
"If you wish to create a brand new Worker Sites project then clone the `worker-sites-template` starter repository:\n\n" +
|
|
88
|
+
"```\n" +
|
|
89
|
+
`git clone --depth=1 --branch=wrangler2 https://github.com/cloudflare/worker-sites-template ${gitDirectory}\n` +
|
|
90
|
+
`cd ${gitDirectory}\n` +
|
|
91
|
+
"```\n\n" +
|
|
92
|
+
"Find out more about how to create and maintain Sites projects at https://developers.cloudflare.com/workers/platform/sites.\n" +
|
|
93
|
+
"Have you considered using Cloudflare Pages instead? See https://pages.cloudflare.com/.";
|
|
94
|
+
throw new CommandLineArgsError(message);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
logger.log(
|
|
98
|
+
`Creating a worker in ${path.basename(creationDirectory)} from ${
|
|
99
|
+
args.template
|
|
100
|
+
}`
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
const { remote, subdirectory } = parseTemplatePath(args.template);
|
|
104
|
+
|
|
105
|
+
await cloneIntoDirectory(remote, creationDirectory, subdirectory);
|
|
106
|
+
await initializeGit(creationDirectory);
|
|
107
|
+
|
|
108
|
+
logger.log("✨ Success!");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Creates a path based on the current working directory and a worker name.
|
|
113
|
+
* Automatically increments a counter when searching for an available directory.
|
|
114
|
+
*
|
|
115
|
+
* Running `wrangler generate worker https://some-git-repo` in a directory
|
|
116
|
+
* with the structure:
|
|
117
|
+
* ```
|
|
118
|
+
* - workers
|
|
119
|
+
* |
|
|
120
|
+
* | - worker
|
|
121
|
+
* | | - wrangler.toml
|
|
122
|
+
* | | ...
|
|
123
|
+
* |
|
|
124
|
+
* | - worker-1
|
|
125
|
+
* | | - wrangler.toml
|
|
126
|
+
* | | ...
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* will result in a new worker called `worker-2` being generated.
|
|
130
|
+
*
|
|
131
|
+
* @param workerName the name of the generated worker
|
|
132
|
+
* @returns an absolute path to the directory to generate the worker into
|
|
133
|
+
*/
|
|
134
|
+
function generateWorkerDirectoryName(workerName: string): string {
|
|
135
|
+
let workerDirectoryPath = path.resolve(process.cwd(), workerName);
|
|
136
|
+
let i = 1;
|
|
137
|
+
|
|
138
|
+
while (fs.existsSync(workerDirectoryPath)) {
|
|
139
|
+
workerDirectoryPath = path.resolve(process.cwd(), `${workerName}-${i}`);
|
|
140
|
+
i++;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return workerDirectoryPath;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Checks if an arg is a template, which can be useful if the order
|
|
148
|
+
* of template & worker name args is switched
|
|
149
|
+
*
|
|
150
|
+
* @param arg a template to generate from, or a folder to generate into
|
|
151
|
+
* @returns true if the given arg was remote (a template)
|
|
152
|
+
*/
|
|
153
|
+
function isRemote(arg: string) {
|
|
154
|
+
return /^(https?|ftps?|file|git|ssh):\/\//.test(arg) || arg.includes(":");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* unreadable regex basically copied from degit. i put some named capture groups in,
|
|
159
|
+
* but uhh...there's not much to do short of using pomsky or some other tool.
|
|
160
|
+
*
|
|
161
|
+
* notably: this only supports `https://` and `git@` urls,
|
|
162
|
+
* and is missing support for:
|
|
163
|
+
* - `http`
|
|
164
|
+
* - `ftp(s)`
|
|
165
|
+
* - `file`
|
|
166
|
+
* - `ssh`
|
|
167
|
+
*/
|
|
168
|
+
const TEMPLATE_REGEX =
|
|
169
|
+
/^(?:(?:https:\/\/)?(?<httpsUrl>[^:/]+\.[^:/]+)\/|git@(?<gitUrl>[^:/]+)[:/]|(?<shorthandUrl>[^/]+):)?(?<user>[^/\s]+)\/(?<repository>[^/\s#]+)(?:(?<subdirectoryPath>(?:\/[^/\s#]+)+))?(?:\/)?(?:#(?<tag>.+))?/;
|
|
170
|
+
|
|
171
|
+
// there are a few URL formats we support:
|
|
172
|
+
// - `user/repo` -> assume github, use "https://github.com/user/repo.git"
|
|
173
|
+
// - `https://<httpsUrl>
|
|
174
|
+
// - `git@<gitUrl>`
|
|
175
|
+
// - `(bb|bitbucket|gh|github|gl|gitlab):user/repo` -> parse shorthand into https url
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* There's no URL, so assume a github repo
|
|
179
|
+
*/
|
|
180
|
+
type NoUrl = {
|
|
181
|
+
httpsUrl: undefined;
|
|
182
|
+
gitUrl: undefined;
|
|
183
|
+
shorthandUrl: undefined;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* A https url (e.g. https://bitbucket.org/user/repo)
|
|
188
|
+
*/
|
|
189
|
+
type HttpsUrl = Omit<NoUrl, "httpsUrl"> & { httpsUrl: string };
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* A git url (e.g. git@gitlab.com:user/repo)
|
|
193
|
+
*/
|
|
194
|
+
type GitUrl = Omit<NoUrl, "gitUrl"> & { gitUrl: string };
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* A shorthand url (e.g. github:user/repo)
|
|
198
|
+
*/
|
|
199
|
+
type ShorthandUrl = Omit<NoUrl, "shorthandUrl"> & { shorthandUrl: string };
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Union of all possible URL groups. Exactly one will be present, the rest
|
|
203
|
+
* will be `undefined`.
|
|
204
|
+
*/
|
|
205
|
+
type TemplateRegexUrlGroup = NoUrl | HttpsUrl | GitUrl | ShorthandUrl;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Possible matches of `TEMPLATE_REGEX` against a passed-in template arg
|
|
209
|
+
*/
|
|
210
|
+
type TemplateRegexGroups = {
|
|
211
|
+
/** The user the repo is under */
|
|
212
|
+
user: string;
|
|
213
|
+
|
|
214
|
+
/** The repo name */
|
|
215
|
+
repository: string;
|
|
216
|
+
|
|
217
|
+
/** Optional, path to subdirectory containing template. Begins with `/` */
|
|
218
|
+
subdirectoryPath?: string;
|
|
219
|
+
|
|
220
|
+
/** Optional tag (or branch, etc.) to clone */
|
|
221
|
+
tag?: string;
|
|
222
|
+
} & TemplateRegexUrlGroup;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Parses a regex match on any of the URL groups into a URL base
|
|
226
|
+
*
|
|
227
|
+
* @param urlGroup a regex hit for a URL of any sort
|
|
228
|
+
* @returns the protocol and domain name of the url to clone from
|
|
229
|
+
*/
|
|
230
|
+
function toUrlBase({ httpsUrl, gitUrl, shorthandUrl }: TemplateRegexUrlGroup) {
|
|
231
|
+
if (httpsUrl !== undefined) {
|
|
232
|
+
return `https://${httpsUrl}`;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (gitUrl !== undefined) {
|
|
236
|
+
return `git@${gitUrl}`;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (shorthandUrl !== undefined) {
|
|
240
|
+
switch (shorthandUrl) {
|
|
241
|
+
case "github":
|
|
242
|
+
case "gh":
|
|
243
|
+
return "https://github.com";
|
|
244
|
+
case "gitlab":
|
|
245
|
+
case "gl":
|
|
246
|
+
return "https://gitlab.com";
|
|
247
|
+
case "bitbucket":
|
|
248
|
+
case "bb":
|
|
249
|
+
return "https://bitbucket.org";
|
|
250
|
+
default:
|
|
251
|
+
throw new Error(
|
|
252
|
+
`Unable to parse shorthand ${shorthandUrl}. Supported options are "bitbucket" ("bb"), "github" ("gh"), and "gitlab" ("gl")`
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return "https://github.com";
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Parses a template string (e.g. "user/repo", "github:user/repo/path/to/subdirectory")
|
|
262
|
+
* into a remote URL to clone from and an optional subdirectory to filter for
|
|
263
|
+
*
|
|
264
|
+
* @param templatePath the template string to parse
|
|
265
|
+
* @returns an object containing the remote url and an optional subdirectory to clone
|
|
266
|
+
*/
|
|
267
|
+
function parseTemplatePath(templatePath: string): {
|
|
268
|
+
remote: string;
|
|
269
|
+
subdirectory?: string;
|
|
270
|
+
} {
|
|
271
|
+
if (!templatePath.includes("/")) {
|
|
272
|
+
// template is a cloudflare canonical template, it doesn't include a slash in the name
|
|
273
|
+
return {
|
|
274
|
+
remote: "https://github.com/cloudflare/templates.git",
|
|
275
|
+
subdirectory: templatePath,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const groups = TEMPLATE_REGEX.exec(templatePath)?.groups as unknown as
|
|
280
|
+
| TemplateRegexGroups
|
|
281
|
+
| undefined;
|
|
282
|
+
|
|
283
|
+
if (!groups) {
|
|
284
|
+
throw new Error(`Unable to parse ${templatePath} as a template`);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const { user, repository, subdirectoryPath, tag, ...urlGroups } = groups;
|
|
288
|
+
|
|
289
|
+
const urlBase = toUrlBase(urlGroups);
|
|
290
|
+
const isHttp = urlBase.startsWith("http");
|
|
291
|
+
|
|
292
|
+
const remote = `${urlBase}${isHttp ? "/" : ":"}${user}/${repository}.git${
|
|
293
|
+
tag ? `#${tag}` : ""
|
|
294
|
+
}`;
|
|
295
|
+
|
|
296
|
+
// remove starting /
|
|
297
|
+
const subdirectory = subdirectoryPath?.slice(1);
|
|
298
|
+
|
|
299
|
+
return { remote, subdirectory };
|
|
300
|
+
}
|