@@ -2,36 +2,44 @@ import { existsSync } from "node:fs";
22import { createRequire } from "node:module" ;
33import path from "node:path" ;
44
5+ import { compileOpenNextConfig } from "@opennextjs/aws/build/compileConfig.js" ;
56import { normalizeOptions } from "@opennextjs/aws/build/helper.js" ;
67import { printHeader , showWarningOnWindows } from "@opennextjs/aws/build/utils.js" ;
78import logger from "@opennextjs/aws/logger.js" ;
89import { unstable_readConfig } from "wrangler" ;
910import type yargs from "yargs" ;
1011
1112import { OpenNextConfig } from "../../api/config.js" ;
12- import { ensureCloudflareConfig } from "../build/utils/index.js" ;
13+ import { createOpenNextConfigIfNotExistent , ensureCloudflareConfig } from "../build/utils/index.js" ;
1314
1415export type WithWranglerArgs < T = unknown > = T & {
16+ // Array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
1517 wranglerArgs : string [ ] ;
1618 configPath : string | undefined ;
1719 env : string | undefined ;
1820} ;
1921
2022const nextAppDir = process . cwd ( ) ;
2123
24+ type Options = {
25+ command : string ;
26+ shouldCompileConfig ?: boolean ;
27+ args : WithWranglerArgs ;
28+ } ;
29+
2230/**
23- * Setup the CLI, print necessary messages, and retrieve various options and configs.
31+ * Sets up the CLI and returns config information:
32+ * - Prints necessary header messages and warnings,
33+ * - Retrieves the OpenNext config and validates it.
34+ * - Initialises the OpenNext options.
35+ * - Reads the Wrangler config.
2436 *
2537 * @param command
38+ * @param shouldCompileConfig
2639 * @param args
27- * @param getOpenNextConfig - Function that resolves to a config file
28- * @returns CLI options, OpenNext config, and Wrangler config
40+ * @returns CLI options, OpenNext config, and Wrangler config.
2941 */
30- export async function setupCLI (
31- command : string ,
32- args : WithWranglerArgs ,
33- getOpenNextConfig : ( baseDir : string ) => Promise < { config : OpenNextConfig ; buildDir : string } >
34- ) {
42+ export async function setupCLI ( { command, shouldCompileConfig, args } : Options ) {
3543 printHeader ( `Cloudflare ${ command } ` ) ;
3644
3745 showWarningOnWindows ( ) ;
@@ -40,41 +48,44 @@ export async function setupCLI(
4048 const require = createRequire ( import . meta. url ) ;
4149 const openNextDistDir = path . dirname ( require . resolve ( "@opennextjs/aws/index.js" ) ) ;
4250
43- const { config, buildDir } = await getOpenNextConfig ( baseDir ) ;
51+ const { config, buildDir } = await getOpenNextConfig ( { shouldCompileConfig , baseDir } ) ;
4452 ensureCloudflareConfig ( config ) ;
4553
4654 // Initialize options
4755 const options = normalizeOptions ( config , openNextDistDir , buildDir ) ;
4856 logger . setLevel ( options . debug ? "debug" : "info" ) ;
4957
50- const wranglerConfig = unstable_readConfig ( { env : args . env , config : args . config } ) ;
58+ const wranglerConfig = unstable_readConfig ( { env : args . env , config : args . configPath } ) ;
5159
5260 return { options, config, wranglerConfig, baseDir } ;
5361}
5462
55- /**
56- * Setup the CLI, print necessary messages, and resolve the compiled OpenNext config.
57- *
58- * @param command
59- * @param args
60- * @returns CLI config
61- */
62- export function setupCompiledAppCLI ( command : string , args : WithWranglerArgs ) {
63- return setupCLI ( command , args , async ( baseDir ) => {
64- const configPath = path . join ( baseDir , ".open-next/.build/open-next.config.edge.mjs" ) ;
63+ async function getOpenNextConfig ( opts : {
64+ shouldCompileConfig ?: boolean ;
65+ baseDir : string ;
66+ } ) : Promise < { config : OpenNextConfig ; buildDir : string } > {
67+ if ( opts . shouldCompileConfig ) {
68+ await createOpenNextConfigIfNotExistent ( opts . baseDir ) ;
69+
70+ return compileOpenNextConfig ( opts . baseDir , undefined , { compileEdge : true } ) ;
71+ }
6572
66- if ( ! existsSync ( configPath ) ) {
67- logger . error ( "Could not find compiled Open Next config" ) ;
68- process . exit ( 1 ) ;
69- }
73+ const configPath = path . join ( opts . baseDir , ".open-next/.build/open-next.config.edge.mjs" ) ;
7074
71- const config = await import ( configPath ) . then ( ( mod ) => mod . default ) ;
75+ if ( ! existsSync ( configPath ) ) {
76+ logger . error ( "Could not find compiled Open Next config" ) ;
77+ process . exit ( 1 ) ;
78+ }
7279
73- // Note: buildDir is not used when an app is already compiled.
74- return { config, buildDir : baseDir } ;
75- } ) ;
80+ const config = await import ( configPath ) . then ( ( mod ) => mod . default ) ;
81+
82+ // Note: buildDir is not used when an app is already compiled.
83+ return { config, buildDir : opts . baseDir } ;
7684}
7785
86+ /**
87+ * Add flags for the wrangler config path and environment to the yargs configuration.
88+ */
7889export function withWranglerOptions < T extends yargs . Argv > ( args : T ) {
7990 return args
8091 . options ( "configPath" , {
@@ -89,6 +100,11 @@ export function withWranglerOptions<T extends yargs.Argv>(args: T) {
89100 } ) ;
90101}
91102
103+ /**
104+ *
105+ * @param args
106+ * @returns An array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
107+ */
92108function getWranglerArgs ( args : {
93109 _ : ( string | number ) [ ] ;
94110 configPath : string | undefined ;
@@ -102,6 +118,11 @@ function getWranglerArgs(args: {
102118 ] ;
103119}
104120
121+ /**
122+ *
123+ * @param args
124+ * @returns The inputted args, and an array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
125+ */
105126export function withWranglerPassthroughArgs <
106127 T extends yargs . ArgumentsCamelCase < {
107128 configPath : string | undefined ;
0 commit comments