wrangler 0.0.0-e928f94 → 0.0.0-e978986
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 +48 -4
- package/miniflare-dist/index.mjs +4886 -0
- package/package.json +74 -34
- package/pages/functions/buildPlugin.ts +105 -0
- package/pages/functions/buildWorker.ts +151 -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 +3374 -0
- package/src/__tests__/dev.test.tsx +980 -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 +130 -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 +233 -130
- package/src/__tests__/init.test.ts +1567 -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 +353 -0
- package/src/__tests__/pages.test.ts +657 -0
- package/src/__tests__/parse.test.ts +330 -0
- package/src/__tests__/publish.test.ts +5468 -0
- package/src/__tests__/r2.test.ts +233 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +481 -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/abort.d.ts +3 -0
- package/src/bundle.ts +200 -0
- package/src/cfetch/index.ts +121 -0
- package/src/cfetch/internal.ts +145 -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 +420 -0
- package/src/config/index.ts +225 -0
- package/src/config/validation-helpers.ts +575 -0
- package/src/config/validation.ts +1539 -0
- package/src/config-cache.ts +45 -0
- package/src/create-worker-preview.ts +185 -0
- package/src/create-worker-upload-form.ts +254 -0
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +419 -0
- package/src/dev/local.tsx +318 -0
- package/src/dev/remote.tsx +227 -0
- package/src/dev/use-esbuild.ts +118 -0
- package/src/dialogs.tsx +67 -17
- package/src/durable.ts +102 -0
- 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 +2031 -1041
- package/src/inspect.ts +662 -0
- package/src/intl-polyfill.d.ts +139 -0
- package/src/kv.ts +408 -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 +229 -35
- package/src/open-in-browser.ts +19 -0
- package/src/package-manager.ts +174 -0
- package/src/pages.tsx +1444 -260
- package/src/parse.ts +202 -0
- package/src/paths.ts +26 -0
- package/src/proxy.ts +465 -0
- package/src/publish.ts +498 -261
- package/src/r2.ts +50 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +240 -60
- 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 +180 -0
- package/templates/gitignore +171 -0
- package/templates/new-worker-scheduled.js +17 -0
- package/templates/new-worker-scheduled.ts +32 -0
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +30 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -1
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +118298 -116056
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/api/form_data.ts +0 -141
- package/src/api/inspect.ts +0 -441
- package/src/api/preview.ts +0 -128
- package/src/api/worker.ts +0 -161
- package/src/cfetch.ts +0 -72
- package/src/config.ts +0 -122
- package/src/dev.tsx +0 -724
- package/src/kv.tsx +0 -211
- package/src/tail.tsx +0 -71
- package/wrangler-dist/cli.js.map +0 -7
package/src/dev/dev.tsx
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { watch } from "chokidar";
|
|
4
|
+
import clipboardy from "clipboardy";
|
|
5
|
+
import commandExists from "command-exists";
|
|
6
|
+
import { Box, Text, useApp, useInput, useStdin } from "ink";
|
|
7
|
+
import React, { useState, useEffect, useRef } from "react";
|
|
8
|
+
import { withErrorBoundary, useErrorHandler } from "react-error-boundary";
|
|
9
|
+
import onExit from "signal-exit";
|
|
10
|
+
import tmp from "tmp-promise";
|
|
11
|
+
import { fetch } from "undici";
|
|
12
|
+
import { runCustomBuild } from "../entry";
|
|
13
|
+
import { openInspector } from "../inspect";
|
|
14
|
+
import { logger } from "../logger";
|
|
15
|
+
import openInBrowser from "../open-in-browser";
|
|
16
|
+
import { getAPIToken } from "../user";
|
|
17
|
+
import { Local } from "./local";
|
|
18
|
+
import { Remote } from "./remote";
|
|
19
|
+
import { useEsbuild } from "./use-esbuild";
|
|
20
|
+
import type { Config } from "../config";
|
|
21
|
+
import type { Entry } from "../entry";
|
|
22
|
+
import type { AssetPaths } from "../sites";
|
|
23
|
+
import type { CfWorkerInit } from "../worker";
|
|
24
|
+
import type { EsbuildBundle } from "./use-esbuild";
|
|
25
|
+
|
|
26
|
+
export type DevProps = {
|
|
27
|
+
name?: string;
|
|
28
|
+
entry: Entry;
|
|
29
|
+
port: number;
|
|
30
|
+
ip: string;
|
|
31
|
+
inspectorPort: number;
|
|
32
|
+
rules: Config["rules"];
|
|
33
|
+
accountId: string | undefined;
|
|
34
|
+
initialMode: "local" | "remote";
|
|
35
|
+
jsxFactory: string | undefined;
|
|
36
|
+
jsxFragment: string | undefined;
|
|
37
|
+
tsconfig: string | undefined;
|
|
38
|
+
upstreamProtocol: "https" | "http";
|
|
39
|
+
localProtocol: "https" | "http";
|
|
40
|
+
enableLocalPersistence: boolean;
|
|
41
|
+
bindings: CfWorkerInit["bindings"];
|
|
42
|
+
crons: Config["triggers"]["crons"];
|
|
43
|
+
public: string | undefined;
|
|
44
|
+
assetPaths: AssetPaths | undefined;
|
|
45
|
+
compatibilityDate: string;
|
|
46
|
+
compatibilityFlags: string[] | undefined;
|
|
47
|
+
usageModel: "bundled" | "unbound" | undefined;
|
|
48
|
+
minify: boolean | undefined;
|
|
49
|
+
nodeCompat: boolean | undefined;
|
|
50
|
+
build: {
|
|
51
|
+
command?: string | undefined;
|
|
52
|
+
cwd?: string | undefined;
|
|
53
|
+
watch_dir?: string | undefined;
|
|
54
|
+
};
|
|
55
|
+
env: string | undefined;
|
|
56
|
+
legacyEnv: boolean;
|
|
57
|
+
zone:
|
|
58
|
+
| {
|
|
59
|
+
id: string;
|
|
60
|
+
host: string;
|
|
61
|
+
}
|
|
62
|
+
| undefined;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export function DevImplementation(props: DevProps): JSX.Element {
|
|
66
|
+
const apiToken = props.initialMode === "remote" ? getAPIToken() : undefined;
|
|
67
|
+
const directory = useTmpDir();
|
|
68
|
+
|
|
69
|
+
useCustomBuild(props.entry, props.build);
|
|
70
|
+
|
|
71
|
+
if (props.public && props.entry.format === "service-worker") {
|
|
72
|
+
throw new Error(
|
|
73
|
+
"You cannot use the service-worker format with a `public` directory."
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (props.bindings.wasm_modules && props.entry.format === "modules") {
|
|
78
|
+
throw new Error(
|
|
79
|
+
"You cannot configure [wasm_modules] with an ES module worker. Instead, import the .wasm module directly in your code"
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (props.bindings.text_blobs && props.entry.format === "modules") {
|
|
84
|
+
throw new Error(
|
|
85
|
+
"You cannot configure [text_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure `[rules]` in your wrangler.toml"
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (props.bindings.data_blobs && props.entry.format === "modules") {
|
|
90
|
+
throw new Error(
|
|
91
|
+
"You cannot configure [data_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure `[rules]` in your wrangler.toml"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const bundle = useEsbuild({
|
|
96
|
+
entry: props.entry,
|
|
97
|
+
destination: directory,
|
|
98
|
+
staticRoot: props.public,
|
|
99
|
+
jsxFactory: props.jsxFactory,
|
|
100
|
+
rules: props.rules,
|
|
101
|
+
jsxFragment: props.jsxFragment,
|
|
102
|
+
serveAssetsFromWorker: !!props.public,
|
|
103
|
+
tsconfig: props.tsconfig,
|
|
104
|
+
minify: props.minify,
|
|
105
|
+
nodeCompat: props.nodeCompat,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// only load the UI if we're running in a supported environment
|
|
109
|
+
const { isRawModeSupported } = useStdin();
|
|
110
|
+
return isRawModeSupported ? (
|
|
111
|
+
<InteractiveDevSession {...props} bundle={bundle} apiToken={apiToken} />
|
|
112
|
+
) : (
|
|
113
|
+
<DevSession
|
|
114
|
+
{...props}
|
|
115
|
+
bundle={bundle}
|
|
116
|
+
apiToken={apiToken}
|
|
117
|
+
local={props.initialMode === "local"}
|
|
118
|
+
/>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
type InteractiveDevSessionProps = DevProps & {
|
|
123
|
+
apiToken: string | undefined;
|
|
124
|
+
bundle: EsbuildBundle | undefined;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
function InteractiveDevSession(props: InteractiveDevSessionProps) {
|
|
128
|
+
const toggles = useHotkeys(
|
|
129
|
+
{
|
|
130
|
+
local: props.initialMode === "local",
|
|
131
|
+
tunnel: false,
|
|
132
|
+
},
|
|
133
|
+
props.port,
|
|
134
|
+
props.ip,
|
|
135
|
+
props.inspectorPort,
|
|
136
|
+
props.localProtocol
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
useTunnel(toggles.tunnel);
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<>
|
|
143
|
+
<DevSession {...props} local={toggles.local} />
|
|
144
|
+
<Box borderStyle="round" paddingLeft={1} paddingRight={1}>
|
|
145
|
+
<Text bold={true}>[b]</Text>
|
|
146
|
+
<Text> open a browser, </Text>
|
|
147
|
+
<Text bold={true}>[d]</Text>
|
|
148
|
+
<Text> open Devtools, </Text>
|
|
149
|
+
<Text bold={true}>[l]</Text>
|
|
150
|
+
<Text> {toggles.local ? "turn off" : "turn on"} local mode, </Text>
|
|
151
|
+
<Text bold={true}>[c]</Text>
|
|
152
|
+
<Text> clear console, </Text>
|
|
153
|
+
<Text bold={true}>[x]</Text>
|
|
154
|
+
<Text> to exit</Text>
|
|
155
|
+
</Box>
|
|
156
|
+
</>
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
type DevSessionProps = InteractiveDevSessionProps & { local: boolean };
|
|
161
|
+
|
|
162
|
+
function DevSession(props: DevSessionProps) {
|
|
163
|
+
return props.local ? (
|
|
164
|
+
<Local
|
|
165
|
+
name={props.name}
|
|
166
|
+
bundle={props.bundle}
|
|
167
|
+
format={props.entry.format}
|
|
168
|
+
compatibilityDate={props.compatibilityDate}
|
|
169
|
+
compatibilityFlags={props.compatibilityFlags}
|
|
170
|
+
bindings={props.bindings}
|
|
171
|
+
assetPaths={props.assetPaths}
|
|
172
|
+
public={props.public}
|
|
173
|
+
port={props.port}
|
|
174
|
+
ip={props.ip}
|
|
175
|
+
rules={props.rules}
|
|
176
|
+
inspectorPort={props.inspectorPort}
|
|
177
|
+
enableLocalPersistence={props.enableLocalPersistence}
|
|
178
|
+
crons={props.crons}
|
|
179
|
+
/>
|
|
180
|
+
) : (
|
|
181
|
+
<Remote
|
|
182
|
+
name={props.name}
|
|
183
|
+
bundle={props.bundle}
|
|
184
|
+
format={props.entry.format}
|
|
185
|
+
accountId={props.accountId}
|
|
186
|
+
apiToken={props.apiToken}
|
|
187
|
+
bindings={props.bindings}
|
|
188
|
+
assetPaths={props.assetPaths}
|
|
189
|
+
public={props.public}
|
|
190
|
+
port={props.port}
|
|
191
|
+
ip={props.ip}
|
|
192
|
+
localProtocol={props.localProtocol}
|
|
193
|
+
inspectorPort={props.inspectorPort}
|
|
194
|
+
compatibilityDate={props.compatibilityDate}
|
|
195
|
+
compatibilityFlags={props.compatibilityFlags}
|
|
196
|
+
usageModel={props.usageModel}
|
|
197
|
+
env={props.env}
|
|
198
|
+
legacyEnv={props.legacyEnv}
|
|
199
|
+
zone={props.zone}
|
|
200
|
+
/>
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export interface DirectorySyncResult {
|
|
205
|
+
name: string;
|
|
206
|
+
removeCallback: () => void;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function useTmpDir(): string | undefined {
|
|
210
|
+
const [directory, setDirectory] = useState<DirectorySyncResult>();
|
|
211
|
+
const handleError = useErrorHandler();
|
|
212
|
+
useEffect(() => {
|
|
213
|
+
let dir: DirectorySyncResult | undefined;
|
|
214
|
+
try {
|
|
215
|
+
dir = tmp.dirSync({ unsafeCleanup: true });
|
|
216
|
+
setDirectory(dir);
|
|
217
|
+
return;
|
|
218
|
+
} catch (err) {
|
|
219
|
+
logger.error(
|
|
220
|
+
"Failed to create temporary directory to store built files."
|
|
221
|
+
);
|
|
222
|
+
handleError(err);
|
|
223
|
+
}
|
|
224
|
+
return () => {
|
|
225
|
+
dir?.removeCallback();
|
|
226
|
+
};
|
|
227
|
+
}, [handleError]);
|
|
228
|
+
return directory?.name;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function useCustomBuild(
|
|
232
|
+
expectedEntry: Entry,
|
|
233
|
+
build: {
|
|
234
|
+
command?: string | undefined;
|
|
235
|
+
cwd?: string | undefined;
|
|
236
|
+
watch_dir?: string | undefined;
|
|
237
|
+
}
|
|
238
|
+
): void {
|
|
239
|
+
useEffect(() => {
|
|
240
|
+
if (!build.command) return;
|
|
241
|
+
let watcher: ReturnType<typeof watch> | undefined;
|
|
242
|
+
if (build.watch_dir) {
|
|
243
|
+
watcher = watch(build.watch_dir, {
|
|
244
|
+
persistent: true,
|
|
245
|
+
ignoreInitial: true,
|
|
246
|
+
}).on("all", (_event, filePath) => {
|
|
247
|
+
const relativeFile =
|
|
248
|
+
path.relative(expectedEntry.directory, expectedEntry.file) || ".";
|
|
249
|
+
//TODO: we should buffer requests to the proxy until this completes
|
|
250
|
+
logger.log(`The file ${filePath} changed, restarting build...`);
|
|
251
|
+
runCustomBuild(expectedEntry.file, relativeFile, build).catch((err) => {
|
|
252
|
+
logger.error("Custom build failed:", err);
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return () => {
|
|
258
|
+
watcher?.close();
|
|
259
|
+
};
|
|
260
|
+
}, [build, expectedEntry]);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function sleep(period: number) {
|
|
264
|
+
return new Promise((resolve) => setTimeout(resolve, period));
|
|
265
|
+
}
|
|
266
|
+
const SLEEP_DURATION = 2000;
|
|
267
|
+
// really need a first class api for this
|
|
268
|
+
const hostNameRegex = /userHostname="(.*)"/g;
|
|
269
|
+
async function findTunnelHostname() {
|
|
270
|
+
let hostName: string | undefined;
|
|
271
|
+
while (!hostName) {
|
|
272
|
+
try {
|
|
273
|
+
const resp = await fetch("http://localhost:8789/metrics");
|
|
274
|
+
const data = await resp.text();
|
|
275
|
+
const matches = Array.from(data.matchAll(hostNameRegex));
|
|
276
|
+
hostName = matches[0][1];
|
|
277
|
+
} catch (err) {
|
|
278
|
+
await sleep(SLEEP_DURATION);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return hostName;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Create a tunnel to the remote worker.
|
|
286
|
+
* We've disabled this for now until we figure out a better user experience.
|
|
287
|
+
*/
|
|
288
|
+
function useTunnel(toggle: boolean) {
|
|
289
|
+
const tunnel = useRef<ReturnType<typeof spawn>>();
|
|
290
|
+
const removeSignalExitListener = useRef<() => void>();
|
|
291
|
+
// TODO: test if cloudflared is available, if not
|
|
292
|
+
// point them to a url where they can get docs to install it
|
|
293
|
+
useEffect(() => {
|
|
294
|
+
async function startTunnel() {
|
|
295
|
+
if (toggle) {
|
|
296
|
+
try {
|
|
297
|
+
await commandExists("cloudflared");
|
|
298
|
+
} catch (e) {
|
|
299
|
+
logger.warn(
|
|
300
|
+
"To share your worker on the Internet, please install `cloudflared` from https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation"
|
|
301
|
+
);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
logger.log("⎔ Starting a tunnel...");
|
|
305
|
+
tunnel.current = spawn("cloudflared", [
|
|
306
|
+
"tunnel",
|
|
307
|
+
"--url",
|
|
308
|
+
"http://localhost:8787",
|
|
309
|
+
"--metrics",
|
|
310
|
+
"localhost:8789",
|
|
311
|
+
]);
|
|
312
|
+
|
|
313
|
+
tunnel.current.on("close", (code) => {
|
|
314
|
+
if (code) {
|
|
315
|
+
logger.log(`Tunnel process exited with code ${code}`);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
removeSignalExitListener.current = onExit((_code, _signal) => {
|
|
320
|
+
logger.log("⎔ Shutting down local tunnel.");
|
|
321
|
+
tunnel.current?.kill();
|
|
322
|
+
tunnel.current = undefined;
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
const hostName = await findTunnelHostname();
|
|
326
|
+
await clipboardy.write(hostName);
|
|
327
|
+
logger.log(`⬣ Sharing at ${hostName}, copied to clipboard.`);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
startTunnel().catch(async (err) => {
|
|
332
|
+
logger.error("tunnel:", err);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
return () => {
|
|
336
|
+
if (tunnel.current) {
|
|
337
|
+
logger.log("⎔ Shutting down tunnel.");
|
|
338
|
+
tunnel.current?.kill();
|
|
339
|
+
tunnel.current = undefined;
|
|
340
|
+
removeSignalExitListener.current && removeSignalExitListener.current();
|
|
341
|
+
removeSignalExitListener.current = undefined;
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
}, [toggle]);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
type useHotkeysInitialState = {
|
|
348
|
+
local: boolean;
|
|
349
|
+
tunnel: boolean;
|
|
350
|
+
};
|
|
351
|
+
function useHotkeys(
|
|
352
|
+
initial: useHotkeysInitialState,
|
|
353
|
+
port: number,
|
|
354
|
+
ip: string,
|
|
355
|
+
inspectorPort: number,
|
|
356
|
+
localProtocol: "http" | "https"
|
|
357
|
+
) {
|
|
358
|
+
// UGH, we should put port in context instead
|
|
359
|
+
const [toggles, setToggles] = useState(initial);
|
|
360
|
+
const { exit } = useApp();
|
|
361
|
+
useInput(
|
|
362
|
+
async (
|
|
363
|
+
input,
|
|
364
|
+
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
365
|
+
key
|
|
366
|
+
) => {
|
|
367
|
+
switch (input.toLowerCase()) {
|
|
368
|
+
// clear console
|
|
369
|
+
case "c":
|
|
370
|
+
console.clear();
|
|
371
|
+
// This console.log causes Ink to re-render the `DevSession` component.
|
|
372
|
+
// Couldn't find a better way to tell it to do so...
|
|
373
|
+
console.log();
|
|
374
|
+
break;
|
|
375
|
+
// open browser
|
|
376
|
+
case "b": {
|
|
377
|
+
await openInBrowser(`${localProtocol}://${ip}:${port}`);
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
// toggle inspector
|
|
381
|
+
case "d": {
|
|
382
|
+
await openInspector(inspectorPort);
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
// toggle local
|
|
386
|
+
case "l":
|
|
387
|
+
setToggles((previousToggles) => ({
|
|
388
|
+
...previousToggles,
|
|
389
|
+
local: !previousToggles.local,
|
|
390
|
+
}));
|
|
391
|
+
break;
|
|
392
|
+
// shut down
|
|
393
|
+
case "q":
|
|
394
|
+
case "x":
|
|
395
|
+
exit();
|
|
396
|
+
break;
|
|
397
|
+
default:
|
|
398
|
+
// nothing?
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
);
|
|
403
|
+
return toggles;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function ErrorFallback(props: { error: Error }) {
|
|
407
|
+
const { exit } = useApp();
|
|
408
|
+
useEffect(() => exit(props.error));
|
|
409
|
+
return (
|
|
410
|
+
<>
|
|
411
|
+
<Text>Something went wrong:</Text>
|
|
412
|
+
<Text>{props.error.stack}</Text>
|
|
413
|
+
</>
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export default withErrorBoundary(DevImplementation, {
|
|
418
|
+
FallbackComponent: ErrorFallback,
|
|
419
|
+
});
|