|
1 | 1 | import yargs from "yargs"; |
2 | 2 |
|
3 | | -import { buildCommand } from "./commands/build.js"; |
4 | | -import { deployCommand } from "./commands/deploy.js"; |
5 | | -import { populateCacheCommand } from "./commands/populate-cache.js"; |
6 | | -import { previewCommand } from "./commands/preview.js"; |
7 | | -import { uploadCommand } from "./commands/upload.js"; |
| 3 | +import { addBuildCommand } from "./commands/build.js"; |
| 4 | +import { addDeployCommand } from "./commands/deploy.js"; |
| 5 | +import { addPopulateCacheCommand } from "./commands/populate-cache.js"; |
| 6 | +import { addPreviewCommand } from "./commands/preview.js"; |
| 7 | +import { addUploadCommand } from "./commands/upload.js"; |
8 | 8 |
|
9 | 9 | export function runCommand() { |
10 | | - return yargs(process.argv.slice(2)) |
| 10 | + let y = yargs(process.argv.slice(2)) |
11 | 11 | .scriptName("opennextjs-cloudflare") |
12 | | - .parserConfiguration({ "unknown-options-as-args": true }) |
13 | | - .command( |
14 | | - "build", |
15 | | - "Build an OpenNext Cloudflare worker", |
16 | | - (c) => |
17 | | - withWranglerOptions(c) |
18 | | - .option("skipNextBuild", { |
19 | | - type: "boolean", |
20 | | - alias: ["skipBuild", "s"], |
21 | | - default: ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)), |
22 | | - desc: "Skip building the Next.js app", |
23 | | - }) |
24 | | - .option("noMinify", { |
25 | | - type: "boolean", |
26 | | - default: false, |
27 | | - desc: "Disable worker minification", |
28 | | - }) |
29 | | - .option("skipWranglerConfigCheck", { |
30 | | - type: "boolean", |
31 | | - default: ["1", "true", "yes"].includes(String(process.env.SKIP_WRANGLER_CONFIG_CHECK)), |
32 | | - desc: "Skip checking for a Wrangler config", |
33 | | - }), |
34 | | - (args) => buildCommand(withWranglerPassthroughArgs(args)) |
35 | | - ) |
36 | | - .command( |
37 | | - "preview", |
38 | | - "Preview a built OpenNext app with a Wrangler dev server", |
39 | | - (c) => withPopulateCacheOptions(c), |
40 | | - (args) => previewCommand(withWranglerPassthroughArgs(args)) |
41 | | - ) |
42 | | - .command( |
43 | | - "deploy", |
44 | | - "Deploy a built OpenNext app to Cloudflare Workers", |
45 | | - (c) => withPopulateCacheOptions(c), |
46 | | - (args) => deployCommand(withWranglerPassthroughArgs(args)) |
47 | | - ) |
48 | | - .command( |
49 | | - "upload", |
50 | | - "Upload a built OpenNext app to Cloudflare Workers", |
51 | | - (c) => withPopulateCacheOptions(c), |
52 | | - (args) => uploadCommand(withWranglerPassthroughArgs(args)) |
53 | | - ) |
54 | | - .command("populateCache", "Populate the cache for a built Next.js app", (c) => |
55 | | - c |
56 | | - .command( |
57 | | - "local", |
58 | | - "Local dev server cache", |
59 | | - (c) => withPopulateCacheOptions(c), |
60 | | - (args) => populateCacheCommand("local", withWranglerPassthroughArgs(args)) |
61 | | - ) |
62 | | - .command( |
63 | | - "remote", |
64 | | - "Remote Cloudflare Worker cache", |
65 | | - (c) => withPopulateCacheOptions(c), |
66 | | - (args) => populateCacheCommand("remote", withWranglerPassthroughArgs(args)) |
67 | | - ) |
68 | | - .demandCommand(1, 1) |
69 | | - ) |
70 | | - .demandCommand(1, 1) |
71 | | - .parse(); |
72 | | -} |
73 | | - |
74 | | -function withWranglerOptions<T extends yargs.Argv>(args: T) { |
75 | | - return args |
76 | | - .options("config", { |
77 | | - type: "string", |
78 | | - alias: "c", |
79 | | - desc: "Path to Wrangler configuration file", |
80 | | - }) |
81 | | - .options("env", { |
82 | | - type: "string", |
83 | | - alias: "e", |
84 | | - desc: "Wrangler environment to use for operations", |
85 | | - }); |
86 | | -} |
| 12 | + .parserConfiguration({ "unknown-options-as-args": true }); |
87 | 13 |
|
88 | | -function withPopulateCacheOptions<T extends yargs.Argv>(args: T) { |
89 | | - return withWranglerOptions(args).options("cacheChunkSize", { |
90 | | - type: "number", |
91 | | - default: 25, |
92 | | - desc: "Number of entries per chunk when populating the cache", |
93 | | - }); |
94 | | -} |
95 | | - |
96 | | -function getWranglerArgs(args: { |
97 | | - _: (string | number)[]; |
98 | | - config: string | undefined; |
99 | | - env: string | undefined; |
100 | | -}): string[] { |
101 | | - return [ |
102 | | - ...(args.config ? ["--config", args.config] : []), |
103 | | - ...(args.env ? ["--env", args.env] : []), |
104 | | - // Note: the first args in `_` will be the commands. |
105 | | - ...args._.slice(args._[0] === "populateCache" ? 2 : 1).map((a) => `${a}`), |
106 | | - ]; |
107 | | -} |
| 14 | + y = addBuildCommand(y); |
| 15 | + y = addPreviewCommand(y); |
| 16 | + y = addDeployCommand(y); |
| 17 | + y = addUploadCommand(y); |
| 18 | + y = addPopulateCacheCommand(y); |
108 | 19 |
|
109 | | -function withWranglerPassthroughArgs< |
110 | | - T extends yargs.ArgumentsCamelCase<{ |
111 | | - config: string | undefined; |
112 | | - env: string | undefined; |
113 | | - }>, |
114 | | ->(args: T) { |
115 | | - return { ...args, wranglerArgs: getWranglerArgs(args) }; |
| 20 | + return y.demandCommand(1, 1).parse(); |
116 | 21 | } |
0 commit comments