11#!/usr/bin/env node
22
3+ import { spawnSync } from "node:child_process" ;
4+ import { existsSync , writeFileSync } from "node:fs" ;
5+ import path from "node:path" ;
36import { build } from "./build.js" ;
7+ import { LOCAL_CONFIG , DEV_CONFIG_PATH } from "./build/constant.js" ;
8+ import { printHeader } from "./build/utils.js" ;
49
510const command = process . argv [ 2 ] ;
6- if ( command !== "build" ) printHelp ( ) ;
11+ if (
12+ command !== "build" &&
13+ command !== "local:config" &&
14+ command !== "local:run"
15+ )
16+ printHelp ( ) ;
717
818const args = parseArgs ( ) ;
919if ( Object . keys ( args ) . includes ( "--help" ) ) printHelp ( ) ;
1020
11- await build ( args [ "--config-path" ] , args [ "--node-externals" ] ) ;
12-
21+ if ( command === "build" ) {
22+ await build ( args [ "--config-path" ] , args [ "--node-externals" ] ) ;
23+ } else if ( command === "local:config" ) {
24+ writeLocalConfig ( ) ;
25+ } else if ( command === "local:run" ) {
26+ await buildLocalConfig ( ) ;
27+ runLocally ( ) ;
28+ }
1329function parseArgs ( ) {
1430 return process . argv . slice ( 2 ) . reduce (
1531 ( acc , key , ind , self ) => {
@@ -29,20 +45,83 @@ function parseArgs() {
2945 ) ;
3046}
3147
48+ async function buildLocalConfig ( ) {
49+ if ( ! existsSync ( DEV_CONFIG_PATH ) ) {
50+ console . error (
51+ "open-next.config.local.ts does not exist. You can run `local:config` first to generate it." ,
52+ ) ;
53+ process . exit ( 1 ) ;
54+ }
55+
56+ // Build OpenNext with dev overrides
57+ printHeader ( "Building OpenNext with open-next.config.local.ts" ) ;
58+ await build ( DEV_CONFIG_PATH , args [ "--node-externals" ] ) ;
59+ }
60+
61+ function runLocally ( ) {
62+ const handlerPath = path . join (
63+ ".open-next" ,
64+ "server-functions" ,
65+ "default" ,
66+ "index.mjs" ,
67+ ) ;
68+ if ( ! existsSync ( handlerPath ) ) {
69+ console . error (
70+ "OpenNext server function not found. Please build it before running this command." ,
71+ ) ;
72+ return ;
73+ }
74+ printHeader ( "Running OpenNext locally" ) ;
75+ spawnSync ( "node" , [ handlerPath ] , {
76+ stdio : "inherit" ,
77+ shell : true ,
78+ } ) ;
79+ }
80+
81+ function writeLocalConfig ( ) {
82+ if ( existsSync ( DEV_CONFIG_PATH ) ) {
83+ console . error (
84+ "open-next.config.local.ts already exists. Please remove it before running this command." ,
85+ ) ;
86+ return ;
87+ }
88+
89+ try {
90+ writeFileSync ( DEV_CONFIG_PATH , LOCAL_CONFIG ) ;
91+ } catch ( e ) {
92+ console . error ( "Error writing open-next.config.local.ts" , e ) ;
93+ }
94+ }
95+
3296function printHelp ( ) {
33- console . log ( "Unknown command" ) ;
34- console . log ( "" ) ;
97+ console . log ( "╔══════════════════════════════════════════╗" ) ;
98+ console . log ( "║ Unknown Command ║" ) ;
99+ console . log ( "╚══════════════════════════════════════════╝\n" ) ;
100+
35101 console . log ( "Usage:" ) ;
36102 console . log ( " npx open-next build" ) ;
37- console . log ( "You can use a custom config path here" ) ;
103+ console . log ( " Build the project with OpenNext.\n" ) ;
104+
105+ console . log ( "Options:" ) ;
106+ console . log ( " --config-path <path>" ) ;
107+ console . log ( " Use a custom config path." ) ;
38108 console . log (
39- " npx open-next build --config-path ./path/to/open-next.config.ts" ,
109+ " Usage: npx open-next build --config-path ./path/to/open-next.config.ts\n " ,
40110 ) ;
111+
112+ console . log ( " --node-externals <modules>" ) ;
113+ console . log ( " Configure externals for the esbuild compilation of the" ) ;
114+ console . log ( " open-next.config.ts file." ) ;
41115 console . log (
42- "You can configure externals for the esbuild compilation of the open-next.config.ts file " ,
116+ " Usage: npx open-next build --node-externals aws-sdk,sharp,sqlite3\n " ,
43117 ) ;
44- console . log ( " npx open-next build --node-externals aws-sdk,sharp,sqlite3" ) ;
45- console . log ( "" ) ;
118+
119+ console . log ( "Other commands:" ) ;
120+ console . log ( " local:run" ) ;
121+ console . log ( " Build and run OpenNext locally with open-next.config.local.ts" ) ;
122+
123+ console . log ( " local:config" ) ;
124+ console . log ( " Generate a config file with dev overrides for OpenNext in open-next.config.local.ts" ) ;
46125
47126 process . exit ( 1 ) ;
48127}
0 commit comments