wrangler 0.0.0-ece06ea → 0.0.0-ed08df4
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/Cloudflare_CA.pem +18 -0
- package/README.md +27 -14
- package/bin/wrangler.js +34 -2
- package/miniflare-dist/index.mjs +4886 -0
- package/package.json +71 -36
- package/pages/functions/buildPlugin.ts +92 -0
- package/pages/functions/buildWorker.ts +138 -0
- package/pages/functions/filepath-routing.test.ts +234 -0
- package/pages/functions/filepath-routing.ts +189 -0
- package/pages/functions/identifiers.ts +78 -0
- package/pages/functions/routes.ts +156 -0
- package/pages/functions/template-plugin.ts +147 -0
- package/pages/functions/template-worker.ts +143 -0
- package/src/__tests__/config-cache.test.ts +36 -0
- package/src/__tests__/configuration.test.ts +3125 -0
- package/src/__tests__/dev.test.tsx +829 -0
- package/src/__tests__/guess-worker-format.test.ts +85 -0
- package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
- package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
- package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +48 -0
- package/src/__tests__/helpers/mock-bin.ts +36 -0
- package/src/__tests__/helpers/mock-cfetch.ts +211 -0
- package/src/__tests__/helpers/mock-console.ts +87 -0
- package/src/__tests__/helpers/mock-dialogs.ts +90 -0
- package/src/__tests__/helpers/mock-http-server.ts +46 -0
- package/src/__tests__/helpers/mock-istty.ts +27 -0
- package/src/__tests__/helpers/mock-kv.ts +33 -0
- package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
- package/src/__tests__/helpers/mock-stdin.ts +103 -0
- package/src/__tests__/helpers/mock-web-socket.ts +29 -0
- package/src/__tests__/helpers/run-in-tmp.ts +39 -0
- package/src/__tests__/helpers/run-wrangler.ts +16 -0
- package/src/__tests__/helpers/write-worker-source.ts +31 -0
- package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
- package/src/__tests__/https-options.test.ts +130 -0
- package/src/__tests__/index.test.ts +221 -132
- package/src/__tests__/init.test.ts +1516 -0
- package/src/__tests__/jest.setup.ts +77 -0
- package/src/__tests__/kv.test.ts +1481 -0
- package/src/__tests__/logger.test.ts +115 -0
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/pages.test.ts +327 -0
- package/src/__tests__/parse.test.ts +326 -0
- package/src/__tests__/publish.test.ts +4996 -0
- package/src/__tests__/r2.test.ts +210 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +446 -0
- package/src/__tests__/tail.test.ts +690 -0
- package/src/__tests__/user.test.ts +131 -0
- package/src/__tests__/whoami.test.tsx +105 -0
- package/src/bundle.ts +169 -0
- package/src/cfetch/index.ts +104 -0
- package/src/cfetch/internal.ts +140 -0
- package/src/cli.ts +10 -7
- package/src/config/README.md +107 -0
- package/src/config/config.ts +234 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +394 -0
- package/src/config/index.ts +63 -0
- package/src/config/validation-helpers.ts +575 -0
- package/src/config/validation.ts +1424 -0
- package/src/config-cache.ts +45 -0
- package/src/create-worker-preview.ts +175 -0
- package/src/create-worker-upload-form.ts +240 -0
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +421 -0
- package/src/dev/local.tsx +312 -0
- package/src/dev/remote.tsx +213 -0
- package/src/dev/use-esbuild.ts +118 -0
- package/src/dialogs.tsx +22 -20
- package/src/entry.ts +322 -0
- package/src/environment-variables.ts +35 -0
- package/src/errors.ts +11 -0
- package/src/https-options.ts +123 -0
- package/src/index.tsx +1869 -1020
- package/src/inspect.ts +623 -0
- package/src/intl-polyfill.d.ts +139 -0
- package/src/kv.ts +344 -0
- package/src/logger.ts +66 -0
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/enum-keys.ts +17 -0
- package/src/miniflare-cli/index.ts +41 -0
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +258 -0
- package/src/open-in-browser.ts +26 -0
- package/src/package-manager.ts +127 -0
- package/src/pages.tsx +1756 -218
- package/src/parse.ts +185 -0
- package/src/paths.ts +26 -0
- package/src/proxy.ts +449 -0
- package/src/publish.ts +377 -262
- package/src/r2.ts +50 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +237 -59
- package/src/tail/filters.ts +279 -0
- package/src/tail/index.ts +299 -0
- package/src/tail/printing.ts +67 -0
- package/src/update-check.ts +19 -0
- package/src/user.tsx +395 -221
- package/src/whoami.tsx +70 -0
- package/src/worker.ts +172 -0
- package/templates/gitignore +171 -0
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +15 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -1
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +108921 -132528
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/api/form_data.ts +0 -158
- package/src/api/inspect.ts +0 -441
- package/src/api/preview.ts +0 -123
- package/src/api/worker.ts +0 -161
- package/src/cfetch.ts +0 -72
- package/src/config.ts +0 -124
- package/src/dev.tsx +0 -736
- package/src/kv.tsx +0 -211
- package/src/tail.tsx +0 -71
- package/src/util/fetch.ts +0 -74
- package/wrangler-dist/cli.js.map +0 -7
package/src/pages.tsx
CHANGED
|
@@ -1,24 +1,115 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import { existsSync } from "fs";
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
1
|
+
/* eslint-disable no-shadow */
|
|
2
|
+
|
|
3
|
+
import { execSync, spawn } from "node:child_process";
|
|
4
|
+
import { existsSync, lstatSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { dirname, join, sep } from "node:path";
|
|
8
|
+
import { cwd } from "node:process";
|
|
9
|
+
import { URL } from "node:url";
|
|
10
|
+
import { hash } from "blake3-wasm";
|
|
11
|
+
import { watch } from "chokidar";
|
|
12
|
+
import { render, Text } from "ink";
|
|
13
|
+
import SelectInput from "ink-select-input";
|
|
14
|
+
import Spinner from "ink-spinner";
|
|
15
|
+
import Table from "ink-table";
|
|
16
|
+
import { getType } from "mime";
|
|
17
|
+
import prettyBytes from "pretty-bytes";
|
|
18
|
+
import React from "react";
|
|
19
|
+
import { format as timeagoFormat } from "timeago.js";
|
|
20
|
+
import { File, FormData } from "undici";
|
|
21
|
+
import { buildPlugin } from "../pages/functions/buildPlugin";
|
|
22
|
+
import { buildWorker } from "../pages/functions/buildWorker";
|
|
23
|
+
import { generateConfigFromFileTree } from "../pages/functions/filepath-routing";
|
|
24
|
+
import { writeRoutesModule } from "../pages/functions/routes";
|
|
25
|
+
import { fetchResult } from "./cfetch";
|
|
26
|
+
import { getConfigCache, saveToConfigCache } from "./config-cache";
|
|
27
|
+
import { prompt } from "./dialogs";
|
|
28
|
+
import { FatalError } from "./errors";
|
|
29
|
+
import { logger } from "./logger";
|
|
30
|
+
import { getRequestContextCheckOptions } from "./miniflare-cli/request-context";
|
|
31
|
+
import openInBrowser from "./open-in-browser";
|
|
32
|
+
import { toUrlPath } from "./paths";
|
|
33
|
+
import { requireAuth } from "./user";
|
|
34
|
+
import type { Config } from "../pages/functions/routes";
|
|
35
|
+
import type { Headers, Request, fetch } from "@miniflare/core";
|
|
36
|
+
import type { BuildResult } from "esbuild";
|
|
8
37
|
import type { MiniflareOptions } from "miniflare";
|
|
9
|
-
import type {
|
|
10
|
-
|
|
38
|
+
import type { BuilderCallback, CommandModule } from "yargs";
|
|
39
|
+
|
|
40
|
+
export type Project = {
|
|
41
|
+
name: string;
|
|
42
|
+
subdomain: string;
|
|
43
|
+
domains: Array<string>;
|
|
44
|
+
source?: {
|
|
45
|
+
type: string;
|
|
46
|
+
};
|
|
47
|
+
latest_deployment?: {
|
|
48
|
+
modified_on: string;
|
|
49
|
+
};
|
|
50
|
+
created_on: string;
|
|
51
|
+
production_branch: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type Deployment = {
|
|
55
|
+
id: string;
|
|
56
|
+
environment: string;
|
|
57
|
+
deployment_trigger: {
|
|
58
|
+
metadata: {
|
|
59
|
+
commit_hash: string;
|
|
60
|
+
branch: string;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
url: string;
|
|
64
|
+
latest_stage: {
|
|
65
|
+
status: string;
|
|
66
|
+
ended_on: string;
|
|
67
|
+
};
|
|
68
|
+
project_name: string;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
interface PagesConfigCache {
|
|
72
|
+
account_id?: string;
|
|
73
|
+
project_name?: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const PAGES_CONFIG_CACHE_FILENAME = "pages.json";
|
|
77
|
+
|
|
78
|
+
// Defer importing miniflare until we really need it. This takes ~0.5s
|
|
79
|
+
// and also modifies some `stream/web` and `undici` prototypes, so we
|
|
80
|
+
// don't want to do this if pages commands aren't being called.
|
|
81
|
+
|
|
82
|
+
export const pagesBetaWarning =
|
|
83
|
+
"🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose";
|
|
11
84
|
|
|
12
|
-
|
|
85
|
+
const isInPagesCI = !!process.env.CF_PAGES;
|
|
13
86
|
|
|
14
|
-
const
|
|
87
|
+
const CLEANUP_CALLBACKS: (() => void)[] = [];
|
|
88
|
+
const CLEANUP = () => {
|
|
89
|
+
CLEANUP_CALLBACKS.forEach((callback) => callback());
|
|
90
|
+
RUNNING_BUILDERS.forEach((builder) => builder.stop?.());
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
process.on("SIGINT", () => {
|
|
94
|
+
CLEANUP();
|
|
95
|
+
process.exit();
|
|
96
|
+
});
|
|
97
|
+
process.on("SIGTERM", () => {
|
|
98
|
+
CLEANUP();
|
|
99
|
+
process.exit();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
function isWindows() {
|
|
103
|
+
return process.platform === "win32";
|
|
104
|
+
}
|
|
15
105
|
|
|
16
106
|
const SECONDS_TO_WAIT_FOR_PROXY = 5;
|
|
17
107
|
|
|
18
|
-
|
|
108
|
+
async function sleep(ms: number) {
|
|
19
109
|
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
110
|
+
}
|
|
20
111
|
|
|
21
|
-
|
|
112
|
+
function getPids(pid: number) {
|
|
22
113
|
const pids: number[] = [pid];
|
|
23
114
|
let command: string, regExp: RegExp;
|
|
24
115
|
|
|
@@ -43,9 +134,9 @@ const getPids = (pid: number) => {
|
|
|
43
134
|
} catch {}
|
|
44
135
|
|
|
45
136
|
return pids;
|
|
46
|
-
}
|
|
137
|
+
}
|
|
47
138
|
|
|
48
|
-
|
|
139
|
+
function getPort(pid: number) {
|
|
49
140
|
let command: string, regExp: RegExp;
|
|
50
141
|
|
|
51
142
|
if (isWindows()) {
|
|
@@ -66,69 +157,63 @@ const getPort = (pid: number) => {
|
|
|
66
157
|
const match = matches[0];
|
|
67
158
|
if (match) return parseInt(match[1]);
|
|
68
159
|
} catch (thrown) {
|
|
69
|
-
|
|
160
|
+
logger.error(
|
|
70
161
|
`Error scanning for ports of process with PID ${pid}: ${thrown}`
|
|
71
162
|
);
|
|
72
163
|
}
|
|
73
|
-
}
|
|
164
|
+
}
|
|
74
165
|
|
|
75
|
-
|
|
76
|
-
directory,
|
|
166
|
+
async function spawnProxyProcess({
|
|
77
167
|
port,
|
|
78
|
-
|
|
168
|
+
command,
|
|
79
169
|
}: {
|
|
80
|
-
directory?: string;
|
|
81
170
|
port?: number;
|
|
82
|
-
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return undefined;
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
if (directory !== undefined) {
|
|
93
|
-
console.log(`Serving ${directory}...`);
|
|
94
|
-
const args = ["serve", directory];
|
|
95
|
-
if (port) args.push("-p", port.toString());
|
|
96
|
-
proxy = spawn("npx", args, { shell: isWindows() });
|
|
97
|
-
} else {
|
|
98
|
-
const command = remaining;
|
|
99
|
-
if (command.length === 0)
|
|
100
|
-
return exit(
|
|
101
|
-
"Must specify a directory of static assets to serve or a command to run."
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
console.log(`Running ${command.join(" ")}...`);
|
|
105
|
-
proxy = spawn(
|
|
106
|
-
command[0].toString(),
|
|
107
|
-
command.slice(1).map((value) => value.toString()),
|
|
108
|
-
{ shell: isWindows() }
|
|
171
|
+
command: (string | number)[];
|
|
172
|
+
}): Promise<void | number> {
|
|
173
|
+
if (command.length === 0) {
|
|
174
|
+
CLEANUP();
|
|
175
|
+
throw new FatalError(
|
|
176
|
+
"Must specify a directory of static assets to serve or a command to run.",
|
|
177
|
+
1
|
|
109
178
|
);
|
|
110
179
|
}
|
|
111
180
|
|
|
181
|
+
logger.log(`Running ${command.join(" ")}...`);
|
|
182
|
+
const proxy = spawn(
|
|
183
|
+
command[0].toString(),
|
|
184
|
+
command.slice(1).map((value) => value.toString()),
|
|
185
|
+
{
|
|
186
|
+
shell: isWindows(),
|
|
187
|
+
env: {
|
|
188
|
+
BROWSER: "none",
|
|
189
|
+
...process.env,
|
|
190
|
+
},
|
|
191
|
+
}
|
|
192
|
+
);
|
|
193
|
+
CLEANUP_CALLBACKS.push(() => {
|
|
194
|
+
proxy.kill();
|
|
195
|
+
});
|
|
196
|
+
|
|
112
197
|
proxy.stdout.on("data", (data) => {
|
|
113
|
-
|
|
198
|
+
logger.log(`[proxy]: ${data}`);
|
|
114
199
|
});
|
|
115
200
|
|
|
116
201
|
proxy.stderr.on("data", (data) => {
|
|
117
|
-
|
|
202
|
+
logger.error(`[proxy]: ${data}`);
|
|
118
203
|
});
|
|
119
204
|
|
|
120
205
|
proxy.on("close", (code) => {
|
|
121
|
-
|
|
206
|
+
logger.error(`Proxy exited with status ${code}.`);
|
|
122
207
|
});
|
|
123
208
|
|
|
124
209
|
// Wait for proxy process to start...
|
|
125
210
|
while (!proxy.pid) {}
|
|
126
211
|
|
|
127
212
|
if (port === undefined) {
|
|
128
|
-
|
|
213
|
+
logger.log(
|
|
129
214
|
`Sleeping ${SECONDS_TO_WAIT_FOR_PROXY} seconds to allow proxy process to start before attempting to automatically determine port...`
|
|
130
215
|
);
|
|
131
|
-
|
|
216
|
+
logger.log("To skip, specify the proxy port with --proxy.");
|
|
132
217
|
await sleep(SECONDS_TO_WAIT_FOR_PROXY * 1000);
|
|
133
218
|
|
|
134
219
|
port = getPids(proxy.pid)
|
|
@@ -136,209 +221,1662 @@ const spawnProxyProcess = async ({
|
|
|
136
221
|
.filter((port) => port !== undefined)[0];
|
|
137
222
|
|
|
138
223
|
if (port === undefined) {
|
|
139
|
-
|
|
140
|
-
|
|
224
|
+
CLEANUP();
|
|
225
|
+
throw new FatalError(
|
|
226
|
+
"Could not automatically determine proxy port. Please specify the proxy port with --proxy.",
|
|
227
|
+
1
|
|
141
228
|
);
|
|
142
229
|
} else {
|
|
143
|
-
|
|
230
|
+
logger.log(`Automatically determined the proxy port to be ${port}.`);
|
|
144
231
|
}
|
|
145
232
|
}
|
|
146
233
|
|
|
147
|
-
return
|
|
148
|
-
}
|
|
234
|
+
return port;
|
|
235
|
+
}
|
|
149
236
|
|
|
150
|
-
|
|
151
|
-
return
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
237
|
+
function escapeRegex(str: string) {
|
|
238
|
+
return str.replace(/[-/\\^$*+?.()|[]{}]/g, "\\$&");
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
type Replacements = Record<string, string>;
|
|
242
|
+
|
|
243
|
+
function replacer(str: string, replacements: Replacements) {
|
|
244
|
+
for (const [replacement, value] of Object.entries(replacements)) {
|
|
245
|
+
str = str.replace(`:${replacement}`, value);
|
|
246
|
+
}
|
|
247
|
+
return str;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function generateRulesMatcher<T>(
|
|
251
|
+
rules?: Record<string, T>,
|
|
252
|
+
replacer: (match: T, replacements: Replacements) => T = (match) => match
|
|
253
|
+
) {
|
|
254
|
+
// TODO: How can you test cross-host rules?
|
|
255
|
+
if (!rules) return () => [];
|
|
256
|
+
|
|
257
|
+
const compiledRules = Object.entries(rules)
|
|
258
|
+
.map(([rule, match]) => {
|
|
259
|
+
const crossHost = rule.startsWith("https://");
|
|
260
|
+
|
|
261
|
+
rule = rule.split("*").map(escapeRegex).join("(?<splat>.*)");
|
|
262
|
+
|
|
263
|
+
const host_matches = rule.matchAll(
|
|
264
|
+
/(?<=^https:\\\/\\\/[^/]*?):([^\\]+)(?=\\)/g
|
|
265
|
+
);
|
|
266
|
+
for (const match of host_matches) {
|
|
267
|
+
rule = rule.split(match[0]).join(`(?<${match[1]}>[^/.]+)`);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const path_matches = rule.matchAll(/:(\w+)/g);
|
|
271
|
+
for (const match of path_matches) {
|
|
272
|
+
rule = rule.split(match[0]).join(`(?<${match[1]}>[^/]+)`);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
rule = "^" + rule + "$";
|
|
276
|
+
|
|
277
|
+
try {
|
|
278
|
+
const regExp = new RegExp(rule);
|
|
279
|
+
return [{ crossHost, regExp }, match];
|
|
280
|
+
} catch {}
|
|
281
|
+
})
|
|
282
|
+
.filter((value) => value !== undefined) as [
|
|
283
|
+
{ crossHost: boolean; regExp: RegExp },
|
|
284
|
+
T
|
|
285
|
+
][];
|
|
286
|
+
|
|
287
|
+
return ({ request }: { request: Request }) => {
|
|
288
|
+
const { pathname, host } = new URL(request.url);
|
|
289
|
+
|
|
290
|
+
return compiledRules
|
|
291
|
+
.map(([{ crossHost, regExp }, match]) => {
|
|
292
|
+
const test = crossHost ? `https://${host}${pathname}` : pathname;
|
|
293
|
+
const result = regExp.exec(test);
|
|
294
|
+
if (result) {
|
|
295
|
+
return replacer(match, result.groups || {});
|
|
296
|
+
}
|
|
297
|
+
})
|
|
298
|
+
.filter((value) => value !== undefined) as T[];
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function generateHeadersMatcher(headersFile: string) {
|
|
303
|
+
if (existsSync(headersFile)) {
|
|
304
|
+
const contents = readFileSync(headersFile).toString();
|
|
305
|
+
|
|
306
|
+
// TODO: Log errors
|
|
307
|
+
const lines = contents
|
|
308
|
+
.split("\n")
|
|
309
|
+
.map((line) => line.trim())
|
|
310
|
+
.filter((line) => !line.startsWith("#") && line !== "");
|
|
311
|
+
|
|
312
|
+
const rules: Record<string, Record<string, string>> = {};
|
|
313
|
+
let rule: { path: string; headers: Record<string, string> } | undefined =
|
|
314
|
+
undefined;
|
|
315
|
+
|
|
316
|
+
for (const line of lines) {
|
|
317
|
+
if (/^([^\s]+:\/\/|^\/)/.test(line)) {
|
|
318
|
+
if (rule && Object.keys(rule.headers).length > 0) {
|
|
319
|
+
rules[rule.path] = rule.headers;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const path = validateURL(line);
|
|
323
|
+
if (path) {
|
|
324
|
+
rule = {
|
|
325
|
+
path,
|
|
326
|
+
headers: {},
|
|
327
|
+
};
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (!line.includes(":")) continue;
|
|
333
|
+
|
|
334
|
+
const [rawName, ...rawValue] = line.split(":");
|
|
335
|
+
const name = rawName.trim().toLowerCase();
|
|
336
|
+
const value = rawValue.join(":").trim();
|
|
337
|
+
|
|
338
|
+
if (name === "") continue;
|
|
339
|
+
if (!rule) continue;
|
|
340
|
+
|
|
341
|
+
const existingValues = rule.headers[name];
|
|
342
|
+
rule.headers[name] = existingValues
|
|
343
|
+
? `${existingValues}, ${value}`
|
|
344
|
+
: value;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (rule && Object.keys(rule.headers).length > 0) {
|
|
348
|
+
rules[rule.path] = rule.headers;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const rulesMatcher = generateRulesMatcher(rules, (match, replacements) =>
|
|
352
|
+
Object.fromEntries(
|
|
353
|
+
Object.entries(match).map(([name, value]) => [
|
|
354
|
+
name,
|
|
355
|
+
replacer(value, replacements),
|
|
356
|
+
])
|
|
357
|
+
)
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
return (request: Request) => {
|
|
361
|
+
const matches = rulesMatcher({
|
|
362
|
+
request,
|
|
363
|
+
});
|
|
364
|
+
if (matches) return matches;
|
|
365
|
+
};
|
|
366
|
+
} else {
|
|
367
|
+
return () => undefined;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function generateRedirectsMatcher(redirectsFile: string) {
|
|
372
|
+
if (existsSync(redirectsFile)) {
|
|
373
|
+
const contents = readFileSync(redirectsFile).toString();
|
|
374
|
+
|
|
375
|
+
// TODO: Log errors
|
|
376
|
+
const lines = contents
|
|
377
|
+
.split("\n")
|
|
378
|
+
.map((line) => line.trim())
|
|
379
|
+
.filter((line) => !line.startsWith("#") && line !== "");
|
|
380
|
+
|
|
381
|
+
const rules = Object.fromEntries(
|
|
382
|
+
lines
|
|
383
|
+
.map((line) => line.split(" "))
|
|
384
|
+
.filter((tokens) => tokens.length === 2 || tokens.length === 3)
|
|
385
|
+
.map((tokens) => {
|
|
386
|
+
const from = validateURL(tokens[0], true, false, false);
|
|
387
|
+
const to = validateURL(tokens[1], false, true, true);
|
|
388
|
+
let status: number | undefined = parseInt(tokens[2]) || 302;
|
|
389
|
+
status = [301, 302, 303, 307, 308].includes(status)
|
|
390
|
+
? status
|
|
391
|
+
: undefined;
|
|
392
|
+
|
|
393
|
+
return from && to && status ? [from, { to, status }] : undefined;
|
|
160
394
|
})
|
|
161
|
-
.
|
|
395
|
+
.filter((rule) => rule !== undefined) as [
|
|
396
|
+
string,
|
|
397
|
+
{ to: string; status?: number }
|
|
398
|
+
][]
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
const rulesMatcher = generateRulesMatcher(
|
|
402
|
+
rules,
|
|
403
|
+
({ status, to }, replacements) => ({
|
|
404
|
+
status,
|
|
405
|
+
to: replacer(to, replacements),
|
|
406
|
+
})
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
return (request: Request) => {
|
|
410
|
+
const match = rulesMatcher({
|
|
411
|
+
request,
|
|
412
|
+
})[0];
|
|
413
|
+
if (match) return match;
|
|
414
|
+
};
|
|
415
|
+
} else {
|
|
416
|
+
return () => undefined;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function extractPathname(
|
|
421
|
+
path = "/",
|
|
422
|
+
includeSearch: boolean,
|
|
423
|
+
includeHash: boolean
|
|
424
|
+
) {
|
|
425
|
+
if (!path.startsWith("/")) path = `/${path}`;
|
|
426
|
+
const url = new URL(`//${path}`, "relative://");
|
|
427
|
+
return `${url.pathname}${includeSearch ? url.search : ""}${
|
|
428
|
+
includeHash ? url.hash : ""
|
|
429
|
+
}`;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function validateURL(
|
|
433
|
+
token: string,
|
|
434
|
+
onlyRelative = false,
|
|
435
|
+
includeSearch = false,
|
|
436
|
+
includeHash = false
|
|
437
|
+
) {
|
|
438
|
+
const host = /^https:\/\/+(?<host>[^/]+)\/?(?<path>.*)/.exec(token);
|
|
439
|
+
if (host && host.groups && host.groups.host) {
|
|
440
|
+
if (onlyRelative) return;
|
|
441
|
+
|
|
442
|
+
return `https://${host.groups.host}${extractPathname(
|
|
443
|
+
host.groups.path,
|
|
444
|
+
includeSearch,
|
|
445
|
+
includeHash
|
|
446
|
+
)}`;
|
|
447
|
+
} else {
|
|
448
|
+
if (!token.startsWith("/") && onlyRelative) token = `/${token}`;
|
|
449
|
+
|
|
450
|
+
const path = /^\//.exec(token);
|
|
451
|
+
if (path) {
|
|
452
|
+
try {
|
|
453
|
+
return extractPathname(token, includeSearch, includeHash);
|
|
454
|
+
} catch {}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return "";
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function hasFileExtension(pathname: string) {
|
|
461
|
+
return /\/.+\.[a-z0-9]+$/i.test(pathname);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
async function generateAssetsFetch(directory: string): Promise<typeof fetch> {
|
|
465
|
+
// Defer importing miniflare until we really need it
|
|
466
|
+
const { Headers, Request, Response } = await import("@miniflare/core");
|
|
467
|
+
|
|
468
|
+
const headersFile = join(directory, "_headers");
|
|
469
|
+
const redirectsFile = join(directory, "_redirects");
|
|
470
|
+
const workerFile = join(directory, "_worker.js");
|
|
471
|
+
|
|
472
|
+
const ignoredFiles = [headersFile, redirectsFile, workerFile];
|
|
473
|
+
|
|
474
|
+
const assetExists = (path: string) => {
|
|
475
|
+
path = join(directory, path);
|
|
476
|
+
return (
|
|
477
|
+
existsSync(path) &&
|
|
478
|
+
lstatSync(path).isFile() &&
|
|
479
|
+
!ignoredFiles.includes(path)
|
|
480
|
+
);
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
const getAsset = (path: string) => {
|
|
484
|
+
if (assetExists(path)) {
|
|
485
|
+
return join(directory, path);
|
|
486
|
+
}
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
let redirectsMatcher = generateRedirectsMatcher(redirectsFile);
|
|
490
|
+
let headersMatcher = generateHeadersMatcher(headersFile);
|
|
491
|
+
|
|
492
|
+
watch([headersFile, redirectsFile], {
|
|
493
|
+
persistent: true,
|
|
494
|
+
}).on("change", (path) => {
|
|
495
|
+
switch (path) {
|
|
496
|
+
case headersFile: {
|
|
497
|
+
logger.log("_headers modified. Re-evaluating...");
|
|
498
|
+
headersMatcher = generateHeadersMatcher(headersFile);
|
|
499
|
+
break;
|
|
500
|
+
}
|
|
501
|
+
case redirectsFile: {
|
|
502
|
+
logger.log("_redirects modified. Re-evaluating...");
|
|
503
|
+
redirectsMatcher = generateRedirectsMatcher(redirectsFile);
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
const serveAsset = (file: string) => {
|
|
510
|
+
return readFileSync(file);
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
const generateResponse = (request: Request) => {
|
|
514
|
+
const url = new URL(request.url);
|
|
515
|
+
|
|
516
|
+
const deconstructedResponse: {
|
|
517
|
+
status: number;
|
|
518
|
+
headers: Headers;
|
|
519
|
+
body?: Buffer;
|
|
520
|
+
} = {
|
|
521
|
+
status: 200,
|
|
522
|
+
headers: new Headers(),
|
|
523
|
+
body: undefined,
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
const match = redirectsMatcher(request);
|
|
527
|
+
if (match) {
|
|
528
|
+
const { status, to } = match;
|
|
529
|
+
|
|
530
|
+
let location = to;
|
|
531
|
+
let search;
|
|
532
|
+
|
|
533
|
+
if (to.startsWith("/")) {
|
|
534
|
+
search = new URL(location, "http://fakehost").search;
|
|
535
|
+
} else {
|
|
536
|
+
search = new URL(location).search;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
location = `${location}${search ? "" : url.search}`;
|
|
540
|
+
|
|
541
|
+
if (status && [301, 302, 303, 307, 308].includes(status)) {
|
|
542
|
+
deconstructedResponse.status = status;
|
|
543
|
+
} else {
|
|
544
|
+
deconstructedResponse.status = 302;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
deconstructedResponse.headers.set("Location", location);
|
|
548
|
+
return deconstructedResponse;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
if (!request.method?.match(/^(get|head)$/i)) {
|
|
552
|
+
deconstructedResponse.status = 405;
|
|
553
|
+
return deconstructedResponse;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const notFound = () => {
|
|
557
|
+
let cwd = url.pathname;
|
|
558
|
+
while (cwd) {
|
|
559
|
+
cwd = cwd.slice(0, cwd.lastIndexOf("/"));
|
|
560
|
+
|
|
561
|
+
if ((asset = getAsset(`${cwd}/404.html`))) {
|
|
562
|
+
deconstructedResponse.status = 404;
|
|
563
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
564
|
+
deconstructedResponse.headers.set(
|
|
565
|
+
"Content-Type",
|
|
566
|
+
getType(asset) || "application/octet-stream"
|
|
567
|
+
);
|
|
568
|
+
return deconstructedResponse;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
if ((asset = getAsset(`/index.html`))) {
|
|
573
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
574
|
+
deconstructedResponse.headers.set(
|
|
575
|
+
"Content-Type",
|
|
576
|
+
getType(asset) || "application/octet-stream"
|
|
577
|
+
);
|
|
578
|
+
return deconstructedResponse;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
deconstructedResponse.status = 404;
|
|
582
|
+
return deconstructedResponse;
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
let asset;
|
|
586
|
+
|
|
587
|
+
if (url.pathname.endsWith("/")) {
|
|
588
|
+
if ((asset = getAsset(`${url.pathname}/index.html`))) {
|
|
589
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
590
|
+
deconstructedResponse.headers.set(
|
|
591
|
+
"Content-Type",
|
|
592
|
+
getType(asset) || "application/octet-stream"
|
|
593
|
+
);
|
|
594
|
+
return deconstructedResponse;
|
|
595
|
+
} else if (
|
|
596
|
+
(asset = getAsset(`${url.pathname.replace(/\/$/, ".html")}`))
|
|
597
|
+
) {
|
|
598
|
+
deconstructedResponse.status = 301;
|
|
599
|
+
deconstructedResponse.headers.set(
|
|
600
|
+
"Location",
|
|
601
|
+
`${url.pathname.slice(0, -1)}${url.search}`
|
|
602
|
+
);
|
|
603
|
+
return deconstructedResponse;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (url.pathname.endsWith("/index")) {
|
|
608
|
+
deconstructedResponse.status = 301;
|
|
609
|
+
deconstructedResponse.headers.set(
|
|
610
|
+
"Location",
|
|
611
|
+
`${url.pathname.slice(0, -"index".length)}${url.search}`
|
|
612
|
+
);
|
|
613
|
+
return deconstructedResponse;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
if ((asset = getAsset(url.pathname))) {
|
|
617
|
+
if (url.pathname.endsWith(".html")) {
|
|
618
|
+
const extensionlessPath = url.pathname.slice(0, -".html".length);
|
|
619
|
+
if (getAsset(extensionlessPath) || extensionlessPath === "/") {
|
|
620
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
621
|
+
deconstructedResponse.headers.set(
|
|
622
|
+
"Content-Type",
|
|
623
|
+
getType(asset) || "application/octet-stream"
|
|
624
|
+
);
|
|
625
|
+
return deconstructedResponse;
|
|
626
|
+
} else {
|
|
627
|
+
deconstructedResponse.status = 301;
|
|
628
|
+
deconstructedResponse.headers.set(
|
|
629
|
+
"Location",
|
|
630
|
+
`${extensionlessPath}${url.search}`
|
|
631
|
+
);
|
|
632
|
+
return deconstructedResponse;
|
|
633
|
+
}
|
|
634
|
+
} else {
|
|
635
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
636
|
+
deconstructedResponse.headers.set(
|
|
637
|
+
"Content-Type",
|
|
638
|
+
getType(asset) || "application/octet-stream"
|
|
639
|
+
);
|
|
640
|
+
return deconstructedResponse;
|
|
641
|
+
}
|
|
642
|
+
} else if (hasFileExtension(url.pathname)) {
|
|
643
|
+
notFound();
|
|
644
|
+
return deconstructedResponse;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
if ((asset = getAsset(`${url.pathname}.html`))) {
|
|
648
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
649
|
+
deconstructedResponse.headers.set(
|
|
650
|
+
"Content-Type",
|
|
651
|
+
getType(asset) || "application/octet-stream"
|
|
652
|
+
);
|
|
653
|
+
return deconstructedResponse;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
if ((asset = getAsset(`${url.pathname}/index.html`))) {
|
|
657
|
+
deconstructedResponse.status = 301;
|
|
658
|
+
deconstructedResponse.headers.set(
|
|
659
|
+
"Location",
|
|
660
|
+
`${url.pathname}/${url.search}`
|
|
661
|
+
);
|
|
662
|
+
return deconstructedResponse;
|
|
663
|
+
} else {
|
|
664
|
+
notFound();
|
|
665
|
+
return deconstructedResponse;
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
const attachHeaders = (
|
|
670
|
+
request: Request,
|
|
671
|
+
deconstructedResponse: { status: number; headers: Headers; body?: Buffer }
|
|
672
|
+
) => {
|
|
673
|
+
const headers = deconstructedResponse.headers;
|
|
674
|
+
const newHeaders = new Headers({});
|
|
675
|
+
const matches = headersMatcher(request) || [];
|
|
676
|
+
|
|
677
|
+
matches.forEach((match) => {
|
|
678
|
+
Object.entries(match).forEach(([name, value]) => {
|
|
679
|
+
newHeaders.append(name, `${value}`);
|
|
680
|
+
});
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
const combinedHeaders = {
|
|
684
|
+
...Object.fromEntries(headers.entries()),
|
|
685
|
+
...Object.fromEntries(newHeaders.entries()),
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
deconstructedResponse.headers = new Headers({});
|
|
689
|
+
Object.entries(combinedHeaders).forEach(([name, value]) => {
|
|
690
|
+
if (value) deconstructedResponse.headers.set(name, value);
|
|
691
|
+
});
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
return async (input, init) => {
|
|
695
|
+
const request = new Request(input, init);
|
|
696
|
+
const deconstructedResponse = generateResponse(request);
|
|
697
|
+
attachHeaders(request, deconstructedResponse);
|
|
698
|
+
|
|
699
|
+
const headers = new Headers();
|
|
700
|
+
|
|
701
|
+
[...deconstructedResponse.headers.entries()].forEach(([name, value]) => {
|
|
702
|
+
if (value) headers.set(name, value);
|
|
703
|
+
});
|
|
704
|
+
|
|
705
|
+
return new Response(deconstructedResponse.body, {
|
|
706
|
+
headers,
|
|
707
|
+
status: deconstructedResponse.status,
|
|
708
|
+
});
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
const RUNNING_BUILDERS: BuildResult[] = [];
|
|
713
|
+
|
|
714
|
+
async function buildFunctions({
|
|
715
|
+
outfile,
|
|
716
|
+
outputConfigPath,
|
|
717
|
+
functionsDirectory,
|
|
718
|
+
minify = false,
|
|
719
|
+
sourcemap = false,
|
|
720
|
+
fallbackService = "ASSETS",
|
|
721
|
+
watch = false,
|
|
722
|
+
onEnd,
|
|
723
|
+
plugin = false,
|
|
724
|
+
buildOutputDirectory,
|
|
725
|
+
}: {
|
|
726
|
+
outfile: string;
|
|
727
|
+
outputConfigPath?: string;
|
|
728
|
+
functionsDirectory: string;
|
|
729
|
+
minify?: boolean;
|
|
730
|
+
sourcemap?: boolean;
|
|
731
|
+
fallbackService?: string;
|
|
732
|
+
watch?: boolean;
|
|
733
|
+
onEnd?: () => void;
|
|
734
|
+
plugin?: boolean;
|
|
735
|
+
buildOutputDirectory?: string;
|
|
736
|
+
}) {
|
|
737
|
+
RUNNING_BUILDERS.forEach(
|
|
738
|
+
(runningBuilder) => runningBuilder.stop && runningBuilder.stop()
|
|
739
|
+
);
|
|
740
|
+
|
|
741
|
+
const routesModule = join(tmpdir(), "./functionsRoutes.mjs");
|
|
742
|
+
const baseURL = toUrlPath("/");
|
|
743
|
+
|
|
744
|
+
const config: Config = await generateConfigFromFileTree({
|
|
745
|
+
baseDir: functionsDirectory,
|
|
746
|
+
baseURL,
|
|
747
|
+
});
|
|
748
|
+
|
|
749
|
+
if (outputConfigPath) {
|
|
750
|
+
writeFileSync(
|
|
751
|
+
outputConfigPath,
|
|
752
|
+
JSON.stringify({ ...config, baseURL }, null, 2)
|
|
753
|
+
);
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
await writeRoutesModule({
|
|
757
|
+
config,
|
|
758
|
+
srcDir: functionsDirectory,
|
|
759
|
+
outfile: routesModule,
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
if (plugin) {
|
|
763
|
+
RUNNING_BUILDERS.push(
|
|
764
|
+
await buildPlugin({
|
|
765
|
+
routesModule,
|
|
766
|
+
outfile,
|
|
767
|
+
minify,
|
|
768
|
+
sourcemap,
|
|
769
|
+
watch,
|
|
770
|
+
onEnd,
|
|
771
|
+
})
|
|
772
|
+
);
|
|
773
|
+
} else {
|
|
774
|
+
RUNNING_BUILDERS.push(
|
|
775
|
+
await buildWorker({
|
|
776
|
+
routesModule,
|
|
777
|
+
outfile,
|
|
778
|
+
minify,
|
|
779
|
+
sourcemap,
|
|
780
|
+
fallbackService,
|
|
781
|
+
watch,
|
|
782
|
+
onEnd,
|
|
783
|
+
buildOutputDirectory,
|
|
784
|
+
})
|
|
785
|
+
);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
interface CreateDeploymentArgs {
|
|
790
|
+
directory: string;
|
|
791
|
+
projectName?: string;
|
|
792
|
+
branch?: string;
|
|
793
|
+
commitHash?: string;
|
|
794
|
+
commitMessage?: string;
|
|
795
|
+
commitDirty?: boolean;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
const createDeployment: CommandModule<
|
|
799
|
+
CreateDeploymentArgs,
|
|
800
|
+
CreateDeploymentArgs
|
|
801
|
+
> = {
|
|
802
|
+
describe: "🆙 Publish a directory of static assets as a Pages deployment",
|
|
803
|
+
builder: (yargs) => {
|
|
804
|
+
return yargs
|
|
805
|
+
.positional("directory", {
|
|
806
|
+
type: "string",
|
|
807
|
+
demandOption: true,
|
|
808
|
+
description: "The directory of static files to upload",
|
|
809
|
+
})
|
|
810
|
+
.options({
|
|
811
|
+
"project-name": {
|
|
162
812
|
type: "string",
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
813
|
+
description: "The name of the project you want to deploy to",
|
|
814
|
+
},
|
|
815
|
+
branch: {
|
|
816
|
+
type: "string",
|
|
817
|
+
description: "The name of the branch you want to deploy to",
|
|
818
|
+
},
|
|
819
|
+
"commit-hash": {
|
|
820
|
+
type: "string",
|
|
821
|
+
description: "The SHA to attach to this deployment",
|
|
822
|
+
},
|
|
823
|
+
"commit-message": {
|
|
824
|
+
type: "string",
|
|
825
|
+
description: "The commit message to attach to this deployment",
|
|
826
|
+
},
|
|
827
|
+
"commit-dirty": {
|
|
828
|
+
type: "boolean",
|
|
829
|
+
description:
|
|
830
|
+
"Whether or not the workspace should be considered dirty for this deployment",
|
|
831
|
+
},
|
|
832
|
+
})
|
|
833
|
+
.epilogue(pagesBetaWarning);
|
|
834
|
+
},
|
|
835
|
+
handler: async ({
|
|
836
|
+
directory,
|
|
837
|
+
projectName,
|
|
838
|
+
branch,
|
|
839
|
+
commitHash,
|
|
840
|
+
commitMessage,
|
|
841
|
+
commitDirty,
|
|
842
|
+
}) => {
|
|
843
|
+
if (!directory) {
|
|
844
|
+
throw new FatalError("Must specify a directory.", 1);
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
const config = getConfigCache<PagesConfigCache>(
|
|
848
|
+
PAGES_CONFIG_CACHE_FILENAME
|
|
849
|
+
);
|
|
850
|
+
const accountId = await requireAuth(config);
|
|
851
|
+
|
|
852
|
+
projectName ??= config.project_name;
|
|
853
|
+
|
|
854
|
+
const isInteractive = process.stdin.isTTY;
|
|
855
|
+
if (!projectName && isInteractive) {
|
|
856
|
+
const projects = (await listProjects({ accountId })).filter(
|
|
857
|
+
(project) => !project.source
|
|
858
|
+
);
|
|
859
|
+
|
|
860
|
+
let existingOrNew: "existing" | "new" = "new";
|
|
861
|
+
|
|
862
|
+
if (projects.length > 0) {
|
|
863
|
+
existingOrNew = await new Promise<"new" | "existing">((resolve) => {
|
|
864
|
+
const { unmount } = render(
|
|
865
|
+
<>
|
|
866
|
+
<Text>
|
|
867
|
+
No project selected. Would you like to create one or use an
|
|
868
|
+
existing project?
|
|
869
|
+
</Text>
|
|
870
|
+
<SelectInput
|
|
871
|
+
items={[
|
|
872
|
+
{
|
|
873
|
+
key: "new",
|
|
874
|
+
label: "Create a new project",
|
|
875
|
+
value: "new",
|
|
876
|
+
},
|
|
877
|
+
{
|
|
878
|
+
key: "existing",
|
|
879
|
+
label: "Use an existing project",
|
|
880
|
+
value: "existing",
|
|
881
|
+
},
|
|
882
|
+
]}
|
|
883
|
+
onSelect={async (selected) => {
|
|
884
|
+
resolve(selected.value as "new" | "existing");
|
|
885
|
+
unmount();
|
|
886
|
+
}}
|
|
887
|
+
/>
|
|
888
|
+
</>
|
|
889
|
+
);
|
|
204
890
|
});
|
|
205
|
-
},
|
|
206
|
-
async ({
|
|
207
|
-
local,
|
|
208
|
-
directory,
|
|
209
|
-
port,
|
|
210
|
-
proxy: requestedProxyPort,
|
|
211
|
-
"script-path": singleWorkerScriptPath,
|
|
212
|
-
binding: bindings = [],
|
|
213
|
-
kv: kvs = [],
|
|
214
|
-
do: durableObjects = [],
|
|
215
|
-
"--": remaining = [],
|
|
216
|
-
}) => {
|
|
217
|
-
if (!local) {
|
|
218
|
-
console.error("Only local mode is supported at the moment.");
|
|
219
|
-
return;
|
|
220
891
|
}
|
|
221
892
|
|
|
222
|
-
|
|
223
|
-
|
|
893
|
+
switch (existingOrNew) {
|
|
894
|
+
case "existing": {
|
|
895
|
+
projectName = await new Promise((resolve) => {
|
|
896
|
+
const { unmount } = render(
|
|
897
|
+
<>
|
|
898
|
+
<Text>Select a project:</Text>
|
|
899
|
+
<SelectInput
|
|
900
|
+
items={projects.map((project) => ({
|
|
901
|
+
key: project.name,
|
|
902
|
+
label: project.name,
|
|
903
|
+
value: project,
|
|
904
|
+
}))}
|
|
905
|
+
onSelect={async (selected) => {
|
|
906
|
+
resolve(selected.value.name);
|
|
907
|
+
unmount();
|
|
908
|
+
}}
|
|
909
|
+
/>
|
|
910
|
+
</>
|
|
911
|
+
);
|
|
912
|
+
});
|
|
913
|
+
break;
|
|
914
|
+
}
|
|
915
|
+
case "new": {
|
|
916
|
+
projectName = await prompt("Enter the name of your new project:");
|
|
224
917
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
918
|
+
if (!projectName) {
|
|
919
|
+
throw new FatalError("Must specify a project name.", 1);
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
let isGitDir = true;
|
|
923
|
+
try {
|
|
924
|
+
execSync(`git rev-parse --is-inside-work-tree`, {
|
|
925
|
+
stdio: "ignore",
|
|
926
|
+
});
|
|
927
|
+
} catch (err) {
|
|
928
|
+
isGitDir = false;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
const productionBranch = await prompt(
|
|
932
|
+
"Enter the production branch name:",
|
|
933
|
+
"text",
|
|
934
|
+
isGitDir
|
|
935
|
+
? execSync(`git rev-parse --abbrev-ref HEAD`).toString().trim()
|
|
936
|
+
: "production"
|
|
937
|
+
);
|
|
938
|
+
|
|
939
|
+
if (!productionBranch) {
|
|
940
|
+
throw new FatalError("Must specify a production branch.", 1);
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
await fetchResult<Project>(`/accounts/${accountId}/pages/projects`, {
|
|
944
|
+
method: "POST",
|
|
945
|
+
body: JSON.stringify({
|
|
946
|
+
name: projectName,
|
|
947
|
+
production_branch: productionBranch,
|
|
948
|
+
}),
|
|
949
|
+
});
|
|
950
|
+
|
|
951
|
+
saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
|
|
952
|
+
account_id: accountId,
|
|
953
|
+
project_name: projectName,
|
|
954
|
+
});
|
|
955
|
+
|
|
956
|
+
logger.log(`✨ Successfully created the '${projectName}' project.`);
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
if (!projectName) {
|
|
963
|
+
throw new FatalError("Must specify a project name.", 1);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
// We infer git info by default is not passed in
|
|
967
|
+
|
|
968
|
+
let isGitDir = true;
|
|
969
|
+
try {
|
|
970
|
+
execSync(`git rev-parse --is-inside-work-tree`, {
|
|
971
|
+
stdio: "ignore",
|
|
229
972
|
});
|
|
230
|
-
|
|
973
|
+
} catch (err) {
|
|
974
|
+
isGitDir = false;
|
|
975
|
+
}
|
|
231
976
|
|
|
232
|
-
|
|
977
|
+
let isGitDirty = false;
|
|
233
978
|
|
|
234
|
-
|
|
979
|
+
if (isGitDir) {
|
|
980
|
+
try {
|
|
981
|
+
isGitDirty = Boolean(
|
|
982
|
+
execSync(`git status --porcelain`).toString().length
|
|
983
|
+
);
|
|
235
984
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
985
|
+
if (!branch) {
|
|
986
|
+
branch = execSync(`git rev-parse --abbrev-ref HEAD`)
|
|
987
|
+
.toString()
|
|
988
|
+
.trim();
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
if (!commitHash) {
|
|
992
|
+
commitHash = execSync(`git rev-parse HEAD`).toString().trim();
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
if (!commitMessage) {
|
|
996
|
+
commitMessage = execSync(`git show -s --format=%B ${commitHash}`)
|
|
997
|
+
.toString()
|
|
998
|
+
.trim();
|
|
999
|
+
}
|
|
1000
|
+
} catch (err) {}
|
|
1001
|
+
|
|
1002
|
+
if (isGitDirty && !commitDirty) {
|
|
1003
|
+
logger.warn(
|
|
1004
|
+
`Warning: Your working directory is a git repo and has uncommitted changes\nTo silense this warning, pass in --commit-dirty=true`
|
|
1005
|
+
);
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
if (commitDirty === undefined) {
|
|
1009
|
+
commitDirty = isGitDirty;
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
let builtFunctions: string | undefined = undefined;
|
|
1014
|
+
const functionsDirectory = join(cwd(), "functions");
|
|
1015
|
+
if (existsSync(functionsDirectory)) {
|
|
1016
|
+
const outfile = join(tmpdir(), "./functionsWorker.js");
|
|
1017
|
+
|
|
1018
|
+
await new Promise((resolve) =>
|
|
1019
|
+
buildFunctions({
|
|
1020
|
+
outfile,
|
|
1021
|
+
functionsDirectory,
|
|
1022
|
+
onEnd: () => resolve(null),
|
|
1023
|
+
buildOutputDirectory: dirname(outfile),
|
|
1024
|
+
})
|
|
1025
|
+
);
|
|
1026
|
+
|
|
1027
|
+
builtFunctions = readFileSync(outfile, "utf-8");
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
type File = {
|
|
1031
|
+
content: Buffer;
|
|
1032
|
+
metadata: Metadata;
|
|
1033
|
+
};
|
|
1034
|
+
|
|
1035
|
+
type Metadata = {
|
|
1036
|
+
sizeInBytes: number;
|
|
1037
|
+
hash: string;
|
|
1038
|
+
};
|
|
1039
|
+
|
|
1040
|
+
const IGNORE_LIST = [
|
|
1041
|
+
"_worker.js",
|
|
1042
|
+
"_redirects",
|
|
1043
|
+
"_headers",
|
|
1044
|
+
".DS_Store",
|
|
1045
|
+
"node_modules",
|
|
1046
|
+
];
|
|
1047
|
+
|
|
1048
|
+
const walk = async (
|
|
1049
|
+
dir: string,
|
|
1050
|
+
fileMap: Map<string, File> = new Map(),
|
|
1051
|
+
depth = 0
|
|
1052
|
+
) => {
|
|
1053
|
+
const files = await readdir(dir);
|
|
1054
|
+
|
|
1055
|
+
await Promise.all(
|
|
1056
|
+
files.map(async (file) => {
|
|
1057
|
+
const filepath = join(dir, file);
|
|
1058
|
+
const filestat = await stat(filepath);
|
|
1059
|
+
|
|
1060
|
+
if (IGNORE_LIST.includes(file)) {
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
if (filestat.isSymbolicLink()) {
|
|
1065
|
+
return;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
if (filestat.isDirectory()) {
|
|
1069
|
+
fileMap = await walk(filepath, fileMap, depth + 1);
|
|
1070
|
+
} else {
|
|
1071
|
+
let name;
|
|
1072
|
+
if (depth) {
|
|
1073
|
+
name = filepath.split(sep).slice(1).join("/");
|
|
1074
|
+
} else {
|
|
1075
|
+
name = file;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
// TODO: Move this to later so we don't hold as much in memory
|
|
1079
|
+
const fileContent = await readFile(filepath);
|
|
1080
|
+
|
|
1081
|
+
const base64Content = fileContent.toString("base64");
|
|
1082
|
+
const extension =
|
|
1083
|
+
name.split(".").length > 1 ? name.split(".").at(-1) || "" : "";
|
|
1084
|
+
|
|
1085
|
+
const content = base64Content + extension;
|
|
248
1086
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
1087
|
+
if (filestat.size > 25 * 1024 * 1024) {
|
|
1088
|
+
throw new Error(
|
|
1089
|
+
`Error: Pages only supports files up to ${prettyBytes(
|
|
1090
|
+
25 * 1024 * 1024
|
|
1091
|
+
)} in size\n${name} is ${prettyBytes(filestat.size)} in size`
|
|
1092
|
+
);
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
fileMap.set(name, {
|
|
1096
|
+
content: fileContent,
|
|
1097
|
+
metadata: {
|
|
1098
|
+
sizeInBytes: filestat.size,
|
|
1099
|
+
hash: hash(content).toString("hex").slice(0, 32),
|
|
1100
|
+
},
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
})
|
|
1104
|
+
);
|
|
1105
|
+
|
|
1106
|
+
return fileMap;
|
|
1107
|
+
};
|
|
1108
|
+
|
|
1109
|
+
const fileMap = await walk(directory);
|
|
1110
|
+
|
|
1111
|
+
const start = Date.now();
|
|
1112
|
+
|
|
1113
|
+
const files: Array<Promise<void>> = [];
|
|
1114
|
+
|
|
1115
|
+
if (fileMap.size > 1000) {
|
|
1116
|
+
throw new Error(
|
|
1117
|
+
`Error: Pages only supports up to 1,000 files in a deployment at the moment.\nTry a smaller project perhaps?`
|
|
1118
|
+
);
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
let counter = 0;
|
|
1122
|
+
|
|
1123
|
+
const { rerender, unmount } = render(
|
|
1124
|
+
<Progress done={counter} total={fileMap.size} />
|
|
1125
|
+
);
|
|
1126
|
+
|
|
1127
|
+
fileMap.forEach((file: File, name: string) => {
|
|
1128
|
+
const form = new FormData();
|
|
1129
|
+
form.append(
|
|
1130
|
+
"file",
|
|
1131
|
+
new File([new Uint8Array(file.content.buffer)], name)
|
|
1132
|
+
);
|
|
1133
|
+
|
|
1134
|
+
// TODO: Consider a retry
|
|
1135
|
+
|
|
1136
|
+
const promise = fetchResult<{ id: string }>(
|
|
1137
|
+
`/accounts/${accountId}/pages/projects/${projectName}/file`,
|
|
1138
|
+
{
|
|
1139
|
+
method: "POST",
|
|
1140
|
+
body: form,
|
|
1141
|
+
}
|
|
1142
|
+
).then((response) => {
|
|
1143
|
+
counter++;
|
|
1144
|
+
rerender(<Progress done={counter} total={fileMap.size} />);
|
|
1145
|
+
if (response.id != file.metadata.hash) {
|
|
1146
|
+
throw new Error(
|
|
1147
|
+
`Looks like there was an issue uploading '${name}'. Try again perhaps?`
|
|
252
1148
|
);
|
|
253
1149
|
}
|
|
1150
|
+
});
|
|
1151
|
+
|
|
1152
|
+
files.push(promise);
|
|
1153
|
+
});
|
|
1154
|
+
|
|
1155
|
+
await Promise.all(files);
|
|
1156
|
+
|
|
1157
|
+
unmount();
|
|
1158
|
+
|
|
1159
|
+
const uploadMs = Date.now() - start;
|
|
1160
|
+
|
|
1161
|
+
logger.log(
|
|
1162
|
+
`✨ Success! Uploaded ${fileMap.size} files ${formatTime(uploadMs)}\n`
|
|
1163
|
+
);
|
|
1164
|
+
|
|
1165
|
+
const formData = new FormData();
|
|
254
1166
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
1167
|
+
formData.append(
|
|
1168
|
+
"manifest",
|
|
1169
|
+
JSON.stringify(
|
|
1170
|
+
Object.fromEntries(
|
|
1171
|
+
[...fileMap.entries()].map(([fileName, file]) => [
|
|
1172
|
+
`/${fileName}`,
|
|
1173
|
+
file.metadata.hash,
|
|
1174
|
+
])
|
|
1175
|
+
)
|
|
1176
|
+
)
|
|
1177
|
+
);
|
|
1178
|
+
|
|
1179
|
+
if (branch) {
|
|
1180
|
+
formData.append("branch", branch);
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
if (commitMessage) {
|
|
1184
|
+
formData.append("commit_message", commitMessage);
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
if (commitHash) {
|
|
1188
|
+
formData.append("commit_hash", commitHash);
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
if (commitDirty !== undefined) {
|
|
1192
|
+
formData.append("commit_dirty", commitDirty);
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
let _headers: string | undefined,
|
|
1196
|
+
_redirects: string | undefined,
|
|
1197
|
+
_workerJS: string | undefined;
|
|
1198
|
+
|
|
1199
|
+
try {
|
|
1200
|
+
_headers = readFileSync(join(directory, "_headers"), "utf-8");
|
|
1201
|
+
} catch {}
|
|
1202
|
+
|
|
1203
|
+
try {
|
|
1204
|
+
_redirects = readFileSync(join(directory, "_redirects"), "utf-8");
|
|
1205
|
+
} catch {}
|
|
1206
|
+
|
|
1207
|
+
try {
|
|
1208
|
+
_workerJS = readFileSync(join(directory, "_worker.js"), "utf-8");
|
|
1209
|
+
} catch {}
|
|
1210
|
+
|
|
1211
|
+
if (_headers) {
|
|
1212
|
+
formData.append("_headers", new File([_headers], "_headers"));
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
if (_redirects) {
|
|
1216
|
+
formData.append("_redirects", new File([_redirects], "_redirects"));
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
if (builtFunctions) {
|
|
1220
|
+
formData.append("_worker.js", new File([builtFunctions], "_worker.js"));
|
|
1221
|
+
} else if (_workerJS) {
|
|
1222
|
+
formData.append("_worker.js", new File([_workerJS], "_worker.js"));
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
const deploymentResponse = await fetchResult<Deployment>(
|
|
1226
|
+
`/accounts/${accountId}/pages/projects/${projectName}/deployments`,
|
|
1227
|
+
{
|
|
1228
|
+
method: "POST",
|
|
1229
|
+
body: formData,
|
|
258
1230
|
}
|
|
1231
|
+
);
|
|
259
1232
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
modules: true,
|
|
1233
|
+
saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
|
|
1234
|
+
account_id: accountId,
|
|
1235
|
+
project_name: projectName,
|
|
1236
|
+
});
|
|
265
1237
|
|
|
266
|
-
|
|
1238
|
+
logger.log(
|
|
1239
|
+
`✨ Deployment complete! Take a peek over at ${deploymentResponse.url}`
|
|
1240
|
+
);
|
|
1241
|
+
},
|
|
1242
|
+
};
|
|
267
1243
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
1244
|
+
export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
1245
|
+
return yargs
|
|
1246
|
+
.command(
|
|
1247
|
+
"dev [directory] [-- command]",
|
|
1248
|
+
"🧑💻 Develop your full-stack Pages application locally",
|
|
1249
|
+
(yargs) => {
|
|
1250
|
+
return yargs
|
|
1251
|
+
.positional("directory", {
|
|
1252
|
+
type: "string",
|
|
1253
|
+
demandOption: undefined,
|
|
1254
|
+
description: "The directory of static assets to serve",
|
|
1255
|
+
})
|
|
1256
|
+
.positional("command", {
|
|
1257
|
+
type: "string",
|
|
1258
|
+
demandOption: undefined,
|
|
1259
|
+
description: "The proxy command to run",
|
|
1260
|
+
})
|
|
1261
|
+
.options({
|
|
1262
|
+
local: {
|
|
1263
|
+
type: "boolean",
|
|
1264
|
+
default: true,
|
|
1265
|
+
description: "Run on my machine",
|
|
1266
|
+
},
|
|
1267
|
+
port: {
|
|
1268
|
+
type: "number",
|
|
1269
|
+
default: 8788,
|
|
1270
|
+
description: "The port to listen on (serve from)",
|
|
1271
|
+
},
|
|
1272
|
+
proxy: {
|
|
1273
|
+
type: "number",
|
|
1274
|
+
description:
|
|
1275
|
+
"The port to proxy (where the static assets are served)",
|
|
1276
|
+
},
|
|
1277
|
+
"script-path": {
|
|
1278
|
+
type: "string",
|
|
1279
|
+
default: "_worker.js",
|
|
1280
|
+
description:
|
|
1281
|
+
"The location of the single Worker script if not using functions",
|
|
1282
|
+
},
|
|
1283
|
+
binding: {
|
|
1284
|
+
type: "array",
|
|
1285
|
+
description: "Bind variable/secret (KEY=VALUE)",
|
|
1286
|
+
alias: "b",
|
|
1287
|
+
},
|
|
1288
|
+
kv: {
|
|
1289
|
+
type: "array",
|
|
1290
|
+
description: "KV namespace to bind",
|
|
1291
|
+
alias: "k",
|
|
1292
|
+
},
|
|
1293
|
+
do: {
|
|
1294
|
+
type: "array",
|
|
1295
|
+
description: "Durable Object to bind (NAME=CLASS)",
|
|
1296
|
+
alias: "o",
|
|
1297
|
+
},
|
|
1298
|
+
"live-reload": {
|
|
1299
|
+
type: "boolean",
|
|
1300
|
+
default: false,
|
|
1301
|
+
description: "Auto reload HTML pages when change is detected",
|
|
1302
|
+
},
|
|
1303
|
+
// TODO: Miniflare user options
|
|
1304
|
+
})
|
|
1305
|
+
.epilogue(pagesBetaWarning);
|
|
1306
|
+
},
|
|
1307
|
+
async ({
|
|
1308
|
+
local,
|
|
1309
|
+
directory,
|
|
1310
|
+
port,
|
|
1311
|
+
proxy: requestedProxyPort,
|
|
1312
|
+
"script-path": singleWorkerScriptPath,
|
|
1313
|
+
binding: bindings = [],
|
|
1314
|
+
kv: kvs = [],
|
|
1315
|
+
do: durableObjects = [],
|
|
1316
|
+
"live-reload": liveReload,
|
|
1317
|
+
_: [_pages, _dev, ...remaining],
|
|
1318
|
+
}) => {
|
|
1319
|
+
// Beta message for `wrangler pages <commands>` usage
|
|
1320
|
+
logger.log(pagesBetaWarning);
|
|
273
1321
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
1322
|
+
if (!local) {
|
|
1323
|
+
logger.error("Only local mode is supported at the moment.");
|
|
1324
|
+
return;
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
const functionsDirectory = "./functions";
|
|
1328
|
+
const usingFunctions = existsSync(functionsDirectory);
|
|
1329
|
+
|
|
1330
|
+
const command = remaining as (string | number)[];
|
|
1331
|
+
|
|
1332
|
+
let proxyPort: number | void;
|
|
1333
|
+
|
|
1334
|
+
if (directory === undefined) {
|
|
1335
|
+
proxyPort = await spawnProxyProcess({
|
|
1336
|
+
port: requestedProxyPort,
|
|
1337
|
+
command,
|
|
1338
|
+
});
|
|
1339
|
+
if (proxyPort === undefined) return undefined;
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
let miniflareArgs: MiniflareOptions = {};
|
|
1343
|
+
|
|
1344
|
+
let scriptReadyResolve: () => void;
|
|
1345
|
+
const scriptReadyPromise = new Promise<void>(
|
|
1346
|
+
(resolve) => (scriptReadyResolve = resolve)
|
|
1347
|
+
);
|
|
1348
|
+
|
|
1349
|
+
if (usingFunctions) {
|
|
1350
|
+
const outfile = join(tmpdir(), "./functionsWorker.js");
|
|
1351
|
+
|
|
1352
|
+
logger.log(`Compiling worker to "${outfile}"...`);
|
|
1353
|
+
|
|
1354
|
+
try {
|
|
1355
|
+
await buildFunctions({
|
|
1356
|
+
outfile,
|
|
1357
|
+
functionsDirectory,
|
|
1358
|
+
sourcemap: true,
|
|
1359
|
+
watch: true,
|
|
1360
|
+
onEnd: () => scriptReadyResolve(),
|
|
1361
|
+
buildOutputDirectory: directory,
|
|
1362
|
+
});
|
|
1363
|
+
} catch {}
|
|
1364
|
+
|
|
1365
|
+
watch([functionsDirectory], {
|
|
1366
|
+
persistent: true,
|
|
1367
|
+
ignoreInitial: true,
|
|
1368
|
+
}).on("all", async () => {
|
|
1369
|
+
await buildFunctions({
|
|
1370
|
+
outfile,
|
|
1371
|
+
functionsDirectory,
|
|
1372
|
+
sourcemap: true,
|
|
1373
|
+
watch: true,
|
|
1374
|
+
onEnd: () => scriptReadyResolve(),
|
|
1375
|
+
buildOutputDirectory: directory,
|
|
1376
|
+
});
|
|
1377
|
+
});
|
|
1378
|
+
|
|
1379
|
+
miniflareArgs = {
|
|
1380
|
+
scriptPath: outfile,
|
|
1381
|
+
};
|
|
1382
|
+
} else {
|
|
1383
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1384
|
+
scriptReadyResolve!();
|
|
1385
|
+
|
|
1386
|
+
const scriptPath =
|
|
1387
|
+
directory !== undefined
|
|
1388
|
+
? join(directory, singleWorkerScriptPath)
|
|
1389
|
+
: singleWorkerScriptPath;
|
|
1390
|
+
|
|
1391
|
+
if (existsSync(scriptPath)) {
|
|
1392
|
+
miniflareArgs = {
|
|
1393
|
+
scriptPath,
|
|
1394
|
+
};
|
|
1395
|
+
} else {
|
|
1396
|
+
logger.log("No functions. Shimming...");
|
|
1397
|
+
miniflareArgs = {
|
|
1398
|
+
// TODO: The fact that these request/response hacks are necessary is ridiculous.
|
|
1399
|
+
// We need to eliminate them from env.ASSETS.fetch (not sure if just local or prod as well)
|
|
1400
|
+
script: `
|
|
1401
|
+
export default {
|
|
1402
|
+
async fetch(request, env, context) {
|
|
1403
|
+
const response = await env.ASSETS.fetch(request.url, request)
|
|
1404
|
+
return new Response(response.body, response)
|
|
1405
|
+
}
|
|
1406
|
+
}`,
|
|
1407
|
+
};
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
// Defer importing miniflare until we really need it
|
|
1412
|
+
const { Miniflare, Log, LogLevel } = await import("miniflare");
|
|
1413
|
+
const { Response, fetch } = await import("@miniflare/core");
|
|
1414
|
+
|
|
1415
|
+
// Wait for esbuild to finish building before starting Miniflare.
|
|
1416
|
+
// This must be before the call to `new Miniflare`, as that will
|
|
1417
|
+
// asynchronously start loading the script. `await startServer()`
|
|
1418
|
+
// internally just waits for that promise to resolve.
|
|
1419
|
+
await scriptReadyPromise;
|
|
1420
|
+
|
|
1421
|
+
// `assetsFetch()` will only be called if there is `proxyPort` defined.
|
|
1422
|
+
// We only define `proxyPort`, above, when there is no `directory` defined.
|
|
1423
|
+
const assetsFetch =
|
|
1424
|
+
directory !== undefined
|
|
1425
|
+
? await generateAssetsFetch(directory)
|
|
1426
|
+
: invalidAssetsFetch;
|
|
1427
|
+
|
|
1428
|
+
const requestContextCheckOptions =
|
|
1429
|
+
await getRequestContextCheckOptions();
|
|
1430
|
+
|
|
1431
|
+
const miniflare = new Miniflare({
|
|
1432
|
+
port,
|
|
1433
|
+
watch: true,
|
|
1434
|
+
modules: true,
|
|
1435
|
+
|
|
1436
|
+
log: new Log(LogLevel.ERROR, { prefix: "pages" }),
|
|
1437
|
+
logUnhandledRejections: true,
|
|
1438
|
+
sourceMap: true,
|
|
1439
|
+
|
|
1440
|
+
kvNamespaces: kvs.map((kv) => kv.toString()),
|
|
1441
|
+
|
|
1442
|
+
durableObjects: Object.fromEntries(
|
|
1443
|
+
durableObjects.map((durableObject) =>
|
|
1444
|
+
durableObject.toString().split("=")
|
|
1445
|
+
)
|
|
278
1446
|
),
|
|
279
1447
|
|
|
1448
|
+
// User bindings
|
|
1449
|
+
bindings: {
|
|
1450
|
+
...Object.fromEntries(
|
|
1451
|
+
bindings
|
|
1452
|
+
.map((binding) => binding.toString().split("="))
|
|
1453
|
+
.map(([key, ...values]) => [key, values.join("=")])
|
|
1454
|
+
),
|
|
1455
|
+
},
|
|
1456
|
+
|
|
280
1457
|
// env.ASSETS.fetch
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
1458
|
+
serviceBindings: {
|
|
1459
|
+
async ASSETS(request: Request) {
|
|
1460
|
+
if (proxyPort) {
|
|
1461
|
+
try {
|
|
1462
|
+
const url = new URL(request.url);
|
|
1463
|
+
url.host = `localhost:${proxyPort}`;
|
|
1464
|
+
return await fetch(url, request);
|
|
1465
|
+
} catch (thrown) {
|
|
1466
|
+
logger.error(`Could not proxy request: ${thrown}`);
|
|
1467
|
+
|
|
1468
|
+
// TODO: Pretty error page
|
|
1469
|
+
return new Response(
|
|
1470
|
+
`[wrangler] Could not proxy request: ${thrown}`,
|
|
1471
|
+
{ status: 502 }
|
|
1472
|
+
);
|
|
1473
|
+
}
|
|
1474
|
+
} else {
|
|
1475
|
+
try {
|
|
1476
|
+
return await assetsFetch(request);
|
|
1477
|
+
} catch (thrown) {
|
|
1478
|
+
logger.error(`Could not serve static asset: ${thrown}`);
|
|
1479
|
+
|
|
1480
|
+
// TODO: Pretty error page
|
|
1481
|
+
return new Response(
|
|
1482
|
+
`[wrangler] Could not serve static asset: ${thrown}`,
|
|
1483
|
+
{ status: 502 }
|
|
1484
|
+
);
|
|
1485
|
+
}
|
|
300
1486
|
}
|
|
301
1487
|
},
|
|
302
1488
|
},
|
|
303
|
-
},
|
|
304
1489
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
1490
|
+
kvPersist: true,
|
|
1491
|
+
durableObjectsPersist: true,
|
|
1492
|
+
cachePersist: true,
|
|
1493
|
+
liveReload,
|
|
308
1494
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
1495
|
+
...requestContextCheckOptions,
|
|
1496
|
+
...miniflareArgs,
|
|
1497
|
+
});
|
|
312
1498
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
"Could not determine Miniflare's port. Please report this issue to the Wrangler team."
|
|
318
|
-
);
|
|
1499
|
+
try {
|
|
1500
|
+
// `startServer` might throw if user code contains errors
|
|
1501
|
+
const server = await miniflare.startServer();
|
|
1502
|
+
logger.log(`Serving at http://localhost:${port}/`);
|
|
319
1503
|
|
|
320
|
-
|
|
1504
|
+
if (process.env.BROWSER !== "none") {
|
|
1505
|
+
await openInBrowser(`http://localhost:${port}/`);
|
|
1506
|
+
}
|
|
321
1507
|
|
|
322
|
-
|
|
1508
|
+
if (directory !== undefined && liveReload) {
|
|
1509
|
+
watch([directory], {
|
|
1510
|
+
persistent: true,
|
|
1511
|
+
ignoreInitial: true,
|
|
1512
|
+
}).on("all", async () => {
|
|
1513
|
+
await miniflare.reload();
|
|
1514
|
+
});
|
|
1515
|
+
}
|
|
323
1516
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
1517
|
+
CLEANUP_CALLBACKS.push(() => {
|
|
1518
|
+
server.close();
|
|
1519
|
+
miniflare.dispose().catch((err) => miniflare.log.error(err));
|
|
1520
|
+
});
|
|
1521
|
+
} catch (e) {
|
|
1522
|
+
miniflare.log.error(e as Error);
|
|
1523
|
+
CLEANUP();
|
|
1524
|
+
throw new FatalError("Could not start Miniflare.", 1);
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
)
|
|
1528
|
+
.command("functions", false, (yargs) =>
|
|
1529
|
+
// we hide this command from help output because
|
|
1530
|
+
// it's not meant to be used directly right now
|
|
1531
|
+
yargs.command(
|
|
1532
|
+
"build [directory]",
|
|
1533
|
+
"Compile a folder of Cloudflare Pages Functions into a single Worker",
|
|
1534
|
+
(yargs) =>
|
|
1535
|
+
yargs
|
|
1536
|
+
.positional("directory", {
|
|
1537
|
+
type: "string",
|
|
1538
|
+
default: "functions",
|
|
1539
|
+
description: "The directory of Pages Functions",
|
|
1540
|
+
})
|
|
1541
|
+
.options({
|
|
1542
|
+
outfile: {
|
|
1543
|
+
type: "string",
|
|
1544
|
+
default: "_worker.js",
|
|
1545
|
+
description: "The location of the output Worker script",
|
|
1546
|
+
},
|
|
1547
|
+
"output-config-path": {
|
|
1548
|
+
type: "string",
|
|
1549
|
+
description: "The location for the output config file",
|
|
1550
|
+
},
|
|
1551
|
+
minify: {
|
|
1552
|
+
type: "boolean",
|
|
1553
|
+
default: false,
|
|
1554
|
+
description: "Minify the output Worker script",
|
|
1555
|
+
},
|
|
1556
|
+
sourcemap: {
|
|
1557
|
+
type: "boolean",
|
|
1558
|
+
default: false,
|
|
1559
|
+
description:
|
|
1560
|
+
"Generate a sourcemap for the output Worker script",
|
|
1561
|
+
},
|
|
1562
|
+
"fallback-service": {
|
|
1563
|
+
type: "string",
|
|
1564
|
+
default: "ASSETS",
|
|
1565
|
+
description:
|
|
1566
|
+
"The service to fallback to at the end of the `next` chain. Setting to '' will fallback to the global `fetch`.",
|
|
1567
|
+
},
|
|
1568
|
+
watch: {
|
|
1569
|
+
type: "boolean",
|
|
1570
|
+
default: false,
|
|
1571
|
+
description:
|
|
1572
|
+
"Watch for changes to the functions and automatically rebuild the Worker script",
|
|
1573
|
+
},
|
|
1574
|
+
plugin: {
|
|
1575
|
+
type: "boolean",
|
|
1576
|
+
default: false,
|
|
1577
|
+
description: "Build a plugin rather than a Worker script",
|
|
1578
|
+
},
|
|
1579
|
+
"build-output-directory": {
|
|
1580
|
+
type: "string",
|
|
1581
|
+
description: "The directory to output static assets to",
|
|
1582
|
+
},
|
|
1583
|
+
})
|
|
1584
|
+
.epilogue(pagesBetaWarning),
|
|
1585
|
+
async ({
|
|
1586
|
+
directory,
|
|
1587
|
+
outfile,
|
|
1588
|
+
"output-config-path": outputConfigPath,
|
|
1589
|
+
minify,
|
|
1590
|
+
sourcemap,
|
|
1591
|
+
fallbackService,
|
|
1592
|
+
watch,
|
|
1593
|
+
plugin,
|
|
1594
|
+
"build-output-directory": buildOutputDirectory,
|
|
1595
|
+
}) => {
|
|
1596
|
+
if (!isInPagesCI) {
|
|
1597
|
+
// Beta message for `wrangler pages <commands>` usage
|
|
1598
|
+
logger.log(pagesBetaWarning);
|
|
1599
|
+
}
|
|
330
1600
|
|
|
331
|
-
|
|
332
|
-
"/",
|
|
333
|
-
httpProxyMiddleware.createProxyMiddleware({
|
|
334
|
-
target: `http://127.0.0.1:${proxyPort}`,
|
|
335
|
-
})
|
|
336
|
-
);
|
|
1601
|
+
buildOutputDirectory ??= dirname(outfile);
|
|
337
1602
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
1603
|
+
await buildFunctions({
|
|
1604
|
+
outfile,
|
|
1605
|
+
outputConfigPath,
|
|
1606
|
+
functionsDirectory: directory,
|
|
1607
|
+
minify,
|
|
1608
|
+
sourcemap,
|
|
1609
|
+
fallbackService,
|
|
1610
|
+
watch,
|
|
1611
|
+
plugin,
|
|
1612
|
+
buildOutputDirectory,
|
|
1613
|
+
});
|
|
1614
|
+
}
|
|
1615
|
+
)
|
|
1616
|
+
)
|
|
1617
|
+
.command("project", "⚡️ Interact with your Pages projects", (yargs) =>
|
|
1618
|
+
yargs
|
|
1619
|
+
.command(
|
|
1620
|
+
"list",
|
|
1621
|
+
"List your Cloudflare Pages projects",
|
|
1622
|
+
(yargs) => yargs.epilogue(pagesBetaWarning),
|
|
1623
|
+
async () => {
|
|
1624
|
+
const config = getConfigCache<PagesConfigCache>(
|
|
1625
|
+
PAGES_CONFIG_CACHE_FILENAME
|
|
1626
|
+
);
|
|
1627
|
+
|
|
1628
|
+
const accountId = await requireAuth(config);
|
|
1629
|
+
|
|
1630
|
+
const projects: Array<Project> = await listProjects({ accountId });
|
|
1631
|
+
|
|
1632
|
+
const data = projects.map((project) => {
|
|
1633
|
+
return {
|
|
1634
|
+
"Project Name": project.name,
|
|
1635
|
+
"Project Domains": `${project.domains.join(", ")}`,
|
|
1636
|
+
"Git Provider": project.source ? "Yes" : "No",
|
|
1637
|
+
"Last Modified": project.latest_deployment
|
|
1638
|
+
? timeagoFormat(project.latest_deployment.modified_on)
|
|
1639
|
+
: timeagoFormat(project.created_on),
|
|
1640
|
+
};
|
|
1641
|
+
});
|
|
1642
|
+
|
|
1643
|
+
saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
|
|
1644
|
+
account_id: accountId,
|
|
1645
|
+
});
|
|
1646
|
+
|
|
1647
|
+
render(<Table data={data}></Table>);
|
|
1648
|
+
}
|
|
1649
|
+
)
|
|
1650
|
+
.command(
|
|
1651
|
+
"create [project-name]",
|
|
1652
|
+
"Create a new Cloudflare Pages project",
|
|
1653
|
+
(yargs) =>
|
|
1654
|
+
yargs
|
|
1655
|
+
.positional("project-name", {
|
|
1656
|
+
type: "string",
|
|
1657
|
+
demandOption: true,
|
|
1658
|
+
description: "The name of your Pages project",
|
|
1659
|
+
})
|
|
1660
|
+
.options({
|
|
1661
|
+
"production-branch": {
|
|
1662
|
+
type: "string",
|
|
1663
|
+
description:
|
|
1664
|
+
"The name of the production branch of your project",
|
|
1665
|
+
},
|
|
1666
|
+
})
|
|
1667
|
+
.epilogue(pagesBetaWarning),
|
|
1668
|
+
async ({ productionBranch, projectName }) => {
|
|
1669
|
+
const config = getConfigCache<PagesConfigCache>(
|
|
1670
|
+
PAGES_CONFIG_CACHE_FILENAME
|
|
1671
|
+
);
|
|
1672
|
+
const accountId = await requireAuth(config);
|
|
1673
|
+
|
|
1674
|
+
const isInteractive = process.stdin.isTTY;
|
|
1675
|
+
if (!projectName && isInteractive) {
|
|
1676
|
+
projectName = await prompt("Enter the name of your new project:");
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
if (!projectName) {
|
|
1680
|
+
throw new FatalError("Must specify a project name.", 1);
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
if (!productionBranch && isInteractive) {
|
|
1684
|
+
let isGitDir = true;
|
|
1685
|
+
try {
|
|
1686
|
+
execSync(`git rev-parse --is-inside-work-tree`, {
|
|
1687
|
+
stdio: "ignore",
|
|
1688
|
+
});
|
|
1689
|
+
} catch (err) {
|
|
1690
|
+
isGitDir = false;
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
productionBranch = await prompt(
|
|
1694
|
+
"Enter the production branch name:",
|
|
1695
|
+
"text",
|
|
1696
|
+
isGitDir
|
|
1697
|
+
? execSync(`git rev-parse --abbrev-ref HEAD`)
|
|
1698
|
+
.toString()
|
|
1699
|
+
.trim()
|
|
1700
|
+
: "production"
|
|
1701
|
+
);
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
if (!productionBranch) {
|
|
1705
|
+
throw new FatalError("Must specify a production branch.", 1);
|
|
1706
|
+
}
|
|
1707
|
+
|
|
1708
|
+
const { subdomain } = await fetchResult<Project>(
|
|
1709
|
+
`/accounts/${accountId}/pages/projects`,
|
|
1710
|
+
{
|
|
1711
|
+
method: "POST",
|
|
1712
|
+
body: JSON.stringify({
|
|
1713
|
+
name: projectName,
|
|
1714
|
+
production_branch: productionBranch,
|
|
1715
|
+
}),
|
|
1716
|
+
}
|
|
1717
|
+
);
|
|
1718
|
+
|
|
1719
|
+
saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
|
|
1720
|
+
account_id: accountId,
|
|
1721
|
+
project_name: projectName,
|
|
1722
|
+
});
|
|
1723
|
+
|
|
1724
|
+
logger.log(
|
|
1725
|
+
`✨ Successfully created the '${projectName}' project. It will be available at https://${subdomain}/ once you create your first deployment.`
|
|
1726
|
+
);
|
|
1727
|
+
logger.log(
|
|
1728
|
+
`To deploy a folder of assets, run 'wrangler pages publish [directory]'.`
|
|
1729
|
+
);
|
|
1730
|
+
}
|
|
1731
|
+
)
|
|
1732
|
+
.epilogue(pagesBetaWarning)
|
|
1733
|
+
)
|
|
1734
|
+
.command(
|
|
1735
|
+
"deployment",
|
|
1736
|
+
"🚀 Interact with the deployments of a project",
|
|
1737
|
+
(yargs) =>
|
|
1738
|
+
yargs
|
|
1739
|
+
.command(
|
|
1740
|
+
"list",
|
|
1741
|
+
"List deployments in your Cloudflare Pages project",
|
|
1742
|
+
(yargs) =>
|
|
1743
|
+
yargs
|
|
1744
|
+
.options({
|
|
1745
|
+
"project-name": {
|
|
1746
|
+
type: "string",
|
|
1747
|
+
description:
|
|
1748
|
+
"The name of the project you would like to list deployments for",
|
|
1749
|
+
},
|
|
1750
|
+
})
|
|
1751
|
+
.epilogue(pagesBetaWarning),
|
|
1752
|
+
async ({ projectName }) => {
|
|
1753
|
+
const config = getConfigCache<PagesConfigCache>(
|
|
1754
|
+
PAGES_CONFIG_CACHE_FILENAME
|
|
1755
|
+
);
|
|
1756
|
+
const accountId = await requireAuth(config);
|
|
1757
|
+
|
|
1758
|
+
projectName ??= config.project_name;
|
|
1759
|
+
|
|
1760
|
+
const isInteractive = process.stdin.isTTY;
|
|
1761
|
+
if (!projectName && isInteractive) {
|
|
1762
|
+
const projects = await listProjects({ accountId });
|
|
1763
|
+
projectName = await new Promise((resolve) => {
|
|
1764
|
+
const { unmount } = render(
|
|
1765
|
+
<>
|
|
1766
|
+
<Text>Select a project:</Text>
|
|
1767
|
+
<SelectInput
|
|
1768
|
+
items={projects.map((project) => ({
|
|
1769
|
+
key: project.name,
|
|
1770
|
+
label: project.name,
|
|
1771
|
+
value: project,
|
|
1772
|
+
}))}
|
|
1773
|
+
onSelect={async (selected) => {
|
|
1774
|
+
resolve(selected.value.name);
|
|
1775
|
+
unmount();
|
|
1776
|
+
}}
|
|
1777
|
+
/>
|
|
1778
|
+
</>
|
|
1779
|
+
);
|
|
1780
|
+
});
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
if (!projectName) {
|
|
1784
|
+
throw new FatalError("Must specify a project name.", 1);
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
const deployments: Array<Deployment> = await fetchResult(
|
|
1788
|
+
`/accounts/${accountId}/pages/projects/${projectName}/deployments`
|
|
1789
|
+
);
|
|
1790
|
+
|
|
1791
|
+
const titleCase = (word: string) =>
|
|
1792
|
+
word.charAt(0).toUpperCase() + word.slice(1);
|
|
1793
|
+
|
|
1794
|
+
const shortSha = (sha: string) => sha.slice(0, 7);
|
|
1795
|
+
|
|
1796
|
+
const getStatus = (deployment: Deployment) => {
|
|
1797
|
+
// Return a pretty time since timestamp if successful otherwise the status
|
|
1798
|
+
if (deployment.latest_stage.status === `success`) {
|
|
1799
|
+
return timeagoFormat(deployment.latest_stage.ended_on);
|
|
1800
|
+
}
|
|
1801
|
+
return titleCase(deployment.latest_stage.status);
|
|
1802
|
+
};
|
|
1803
|
+
|
|
1804
|
+
const data = deployments.map((deployment) => {
|
|
1805
|
+
return {
|
|
1806
|
+
Environment: titleCase(deployment.environment),
|
|
1807
|
+
Branch: deployment.deployment_trigger.metadata.branch,
|
|
1808
|
+
Source: shortSha(
|
|
1809
|
+
deployment.deployment_trigger.metadata.commit_hash
|
|
1810
|
+
),
|
|
1811
|
+
Deployment: deployment.url,
|
|
1812
|
+
Status: getStatus(deployment),
|
|
1813
|
+
// TODO: Use a url shortener
|
|
1814
|
+
Build: `https://dash.cloudflare.com/${accountId}/pages/view/${deployment.project_name}/${deployment.id}`,
|
|
1815
|
+
};
|
|
1816
|
+
});
|
|
1817
|
+
|
|
1818
|
+
saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
|
|
1819
|
+
account_id: accountId,
|
|
1820
|
+
});
|
|
1821
|
+
|
|
1822
|
+
render(<Table data={data}></Table>);
|
|
1823
|
+
}
|
|
1824
|
+
)
|
|
1825
|
+
.command({
|
|
1826
|
+
command: "create [directory]",
|
|
1827
|
+
...createDeployment,
|
|
1828
|
+
} as CommandModule)
|
|
1829
|
+
.epilogue(pagesBetaWarning)
|
|
1830
|
+
)
|
|
1831
|
+
.command({
|
|
1832
|
+
command: "publish [directory]",
|
|
1833
|
+
...createDeployment,
|
|
1834
|
+
} as CommandModule);
|
|
1835
|
+
};
|
|
1836
|
+
|
|
1837
|
+
const invalidAssetsFetch: typeof fetch = () => {
|
|
1838
|
+
throw new Error(
|
|
1839
|
+
"Trying to fetch assets directly when there is no `directory` option specified, and not in `local` mode."
|
|
343
1840
|
);
|
|
344
1841
|
};
|
|
1842
|
+
|
|
1843
|
+
const listProjects = async ({
|
|
1844
|
+
accountId,
|
|
1845
|
+
}: {
|
|
1846
|
+
accountId: string;
|
|
1847
|
+
}): Promise<Array<Project>> => {
|
|
1848
|
+
const pageSize = 10;
|
|
1849
|
+
let page = 1;
|
|
1850
|
+
const results = [];
|
|
1851
|
+
while (results.length % pageSize === 0) {
|
|
1852
|
+
const json: Array<Project> = await fetchResult(
|
|
1853
|
+
`/accounts/${accountId}/pages/projects`,
|
|
1854
|
+
{},
|
|
1855
|
+
new URLSearchParams({
|
|
1856
|
+
per_page: pageSize.toString(),
|
|
1857
|
+
page: page.toString(),
|
|
1858
|
+
})
|
|
1859
|
+
);
|
|
1860
|
+
page++;
|
|
1861
|
+
results.push(...json);
|
|
1862
|
+
if (json.length < pageSize) {
|
|
1863
|
+
break;
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
return results;
|
|
1867
|
+
};
|
|
1868
|
+
|
|
1869
|
+
function formatTime(duration: number) {
|
|
1870
|
+
return `(${(duration / 1000).toFixed(2)} sec)`;
|
|
1871
|
+
}
|
|
1872
|
+
|
|
1873
|
+
function Progress({ done, total }: { done: number; total: number }) {
|
|
1874
|
+
return (
|
|
1875
|
+
<>
|
|
1876
|
+
<Text>
|
|
1877
|
+
<Spinner type="earth" />
|
|
1878
|
+
{` Uploading... (${done}/${total})\n`}
|
|
1879
|
+
</Text>
|
|
1880
|
+
</>
|
|
1881
|
+
);
|
|
1882
|
+
}
|