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