forked from opennextjs/opennextjs-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.ts
More file actions
78 lines (70 loc) · 2.04 KB
/
preview.ts
File metadata and controls
78 lines (70 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import logger from "@opennextjs/aws/logger.js";
import type yargs from "yargs";
import { populateCache, withPopulateCacheOptions } from "./populate-cache.js";
import { getEnvFromPlatformProxy } from "./utils/helpers.js";
import { runWrangler } from "./utils/run-wrangler.js";
import type { WithWranglerArgs } from "./utils/utils.js";
import {
getNormalizedOptions,
printHeaders,
readWranglerConfig,
retrieveCompiledConfig,
withWranglerPassthroughArgs,
} from "./utils/utils.js";
/**
* Implementation of the `opennextjs-cloudflare preview` command.
*
* @param args
*/
export async function previewCommand(
args: WithWranglerArgs<{ cacheChunkSize?: number; remote: boolean }>
): Promise<void> {
printHeaders("preview");
const { config } = await retrieveCompiledConfig();
const buildOpts = getNormalizedOptions(config);
const wranglerConfig = await readWranglerConfig(args);
const envVars = await getEnvFromPlatformProxy(
{
configPath: args.wranglerConfigPath,
environment: args.env,
},
buildOpts
);
await populateCache(
buildOpts,
config,
wranglerConfig,
{
target: args.remote ? "remote" : "local",
environment: args.env,
wranglerConfigPath: args.wranglerConfigPath,
cacheChunkSize: args.cacheChunkSize,
shouldUsePreviewId: args.remote,
},
envVars
);
const result = runWrangler(buildOpts, ["dev", ...args.wranglerArgs], { logging: "all" });
if (!result.success) {
logger.error(`Wrangler dev command failed${result.stderr ? `:\n${result.stderr}` : ""}`);
process.exit(1);
}
}
/**
* Add the `preview` command to yargs configuration.
*
* Consumes 1 positional parameter.
*/
export function addPreviewCommand<T extends yargs.Argv>(y: T) {
return y.command(
"preview [args..]",
"Preview a built OpenNext app with a Wrangler dev server",
(c) =>
withPopulateCacheOptions(c).option("remote", {
type: "boolean",
alias: "r",
default: false,
desc: "Run on the global Cloudflare network with access to production resources",
}),
(args) => previewCommand(withWranglerPassthroughArgs(args))
);
}