33/**
44 * ReactPress Client CLI Entry Point
55 * This script allows starting the ReactPress client via npx
6+ * Supports both regular and PM2 startup modes
67 */
78
89const path = require ( 'path' ) ;
910const fs = require ( 'fs' ) ;
1011const { spawn, spawnSync } = require ( 'child_process' ) ;
11- const url = require ( 'url' ) ;
12+
13+ // Capture the original working directory where npx was executed
14+ // BUT prioritize the REACTPRESS_ORIGINAL_CWD environment variable if it exists
15+ // This ensures consistency when running via pnpm dev from root directory
16+ const originalCwd = process . env . REACTPRESS_ORIGINAL_CWD || process . cwd ( ) ;
1217
1318// Get command line arguments
1419const args = process . argv . slice ( 2 ) ;
@@ -28,7 +33,7 @@ Options:
2833 --help, -h Show this help message
2934
3035Examples:
31- npx @fecommunity/reactpress-client # Start client in development mode
36+ npx @fecommunity/reactpress-client # Start client normally
3237 npx @fecommunity/reactpress-client --pm2 # Start client with PM2
3338 npx @fecommunity/reactpress-client --help # Show this help message
3439 ` ) ;
@@ -38,38 +43,7 @@ Examples:
3843// Get the directory where this script is located
3944const binDir = __dirname ;
4045const clientDir = path . join ( binDir , '..' ) ;
41-
42- // Change to the client directory
43- process . chdir ( clientDir ) ;
44-
45- // Try to load configuration
46- let port = 3001 ;
47- let clientSiteUrl = 'http://localhost:3001' ;
48-
49- try {
50- const { config } = require ( '@fecommunity/reactpress-config' ) ;
51-
52- // Extract port from CLIENT_SITE_URL or use CLIENT_PORT
53- if ( config . CLIENT_SITE_URL ) {
54- try {
55- const parsedUrl = url . parse ( config . CLIENT_SITE_URL ) ;
56- if ( parsedUrl . port ) {
57- port = parseInt ( parsedUrl . port , 10 ) ;
58- } else if ( parsedUrl . protocol === 'https:' ) {
59- port = 443 ;
60- } else {
61- port = 80 ;
62- }
63- clientSiteUrl = config . CLIENT_SITE_URL ;
64- } catch ( err ) {
65- console . warn ( '[ReactPress Client] Failed to parse CLIENT_SITE_URL, using default port 3001' ) ;
66- }
67- } else if ( config . CLIENT_PORT ) {
68- port = parseInt ( config . CLIENT_PORT , 10 ) ;
69- }
70- } catch ( err ) {
71- console . warn ( '[ReactPress Client] Failed to load configuration, using default settings' ) ;
72- }
46+ const nextDir = path . join ( clientDir , '.next' ) ;
7347
7448// Function to check if PM2 is installed
7549function isPM2Installed ( ) {
@@ -115,7 +89,6 @@ function startWithPM2() {
11589 }
11690
11791 // Check if the client is built
118- const nextDir = path . join ( clientDir , '.next' ) ;
11992 if ( ! fs . existsSync ( nextDir ) ) {
12093 console . log ( '[ReactPress Client] Client not built yet. Building...' ) ;
12194
@@ -162,85 +135,56 @@ function startWithPM2() {
162135 } ) ;
163136}
164137
165- // Function to start with regular Node.js
138+ // Function to start with regular Node.js (npm start)
166139function startWithNode ( ) {
167- // Check if we're in development or production mode
168- const isDev = process . env . NODE_ENV !== 'production' ;
169-
170- if ( isDev ) {
171- // In development mode, start Next.js dev server
172- console . log ( '[ReactPress Client] Starting Next.js development server...' ) ;
140+ // Check if the app is built
141+ if ( ! fs . existsSync ( nextDir ) ) {
142+ console . log ( '[ReactPress Client] Client not built yet. Building...' ) ;
173143
174- // Use Next.js CLI directly
175- const nextDev = spawn ( 'npx ', [ 'next ' , 'dev' , '-p' , port . toString ( ) ] , {
144+ // Try to build the client
145+ const buildResult = spawnSync ( 'npm ', [ 'run ' , 'build' ] , {
176146 stdio : 'inherit' ,
177147 cwd : clientDir
178148 } ) ;
179149
180- nextDev . on ( 'close' , ( code ) => {
181- console . log ( `[ReactPress Client] Next.js dev server exited with code ${ code } ` ) ;
182- process . exit ( code ) ;
183- } ) ;
184-
185- nextDev . on ( 'error' , ( error ) => {
186- console . error ( '[ReactPress Client] Failed to start Next.js dev server:' , error ) ;
150+ if ( buildResult . status !== 0 ) {
151+ console . error ( '[ReactPress Client] Failed to build client' ) ;
187152 process . exit ( 1 ) ;
188- } ) ;
189- } else {
190- // In production mode, check if the app is built and start it
191- const nextDir = path . join ( clientDir , '.next' ) ;
192-
193- if ( ! fs . existsSync ( nextDir ) ) {
194- console . log ( '[ReactPress Client] Client not built yet. Building...' ) ;
195-
196- // Try to build the client
197- const buildResult = spawnSync ( 'npm' , [ 'run' , 'build' ] , {
198- stdio : 'inherit' ,
199- cwd : clientDir
200- } ) ;
201-
202- if ( buildResult . status !== 0 ) {
203- console . error ( '[ReactPress Client] Failed to build client' ) ;
204- process . exit ( 1 ) ;
205- }
206153 }
207-
208- console . log ( '[ReactPress Client] Starting Next.js production server...' ) ;
209-
210- // Start Next.js production server
211- const nextStart = spawn ( 'npx' , [ 'next' , 'start' , '-p' , port . toString ( ) ] , {
212- stdio : 'inherit' ,
213- cwd : clientDir
214- } ) ;
215-
216- nextStart . on ( 'close' , ( code ) => {
217- console . log ( `[ReactPress Client] Next.js production server exited with code ${ code } ` ) ;
218- process . exit ( code ) ;
219- } ) ;
220-
221- nextStart . on ( 'error' , ( error ) => {
222- console . error ( '[ReactPress Client] Failed to start Next.js production server:' , error ) ;
223- process . exit ( 1 ) ;
224- } ) ;
225154 }
226- }
227155
228- console . log ( `[ReactPress Client] Starting client on port ${ port } ...` ) ;
156+ // ONLY set the environment variable if it's not already set
157+ // This preserves the value set by set-env.js when running pnpm dev from root
158+ if ( ! process . env . REACTPRESS_ORIGINAL_CWD ) {
159+ process . env . REACTPRESS_ORIGINAL_CWD = originalCwd ;
160+ } else {
161+ console . log ( `[ReactPress Client] Using existing REACTPRESS_ORIGINAL_CWD: ${ process . env . REACTPRESS_ORIGINAL_CWD } ` ) ;
162+ }
163+
164+ // Change to the client directory
165+ process . chdir ( clientDir ) ;
166+
167+ // Start with npm start
168+ console . log ( '[ReactPress Client] Starting with npm start...' ) ;
169+ const npmStart = spawn ( 'npm' , [ 'start' ] , {
170+ stdio : 'inherit' ,
171+ cwd : clientDir
172+ } ) ;
173+
174+ npmStart . on ( 'close' , ( code ) => {
175+ console . log ( `[ReactPress Client] npm start process exited with code ${ code } ` ) ;
176+ process . exit ( code ) ;
177+ } ) ;
178+
179+ npmStart . on ( 'error' , ( error ) => {
180+ console . error ( '[ReactPress Client] Failed to start with npm start:' , error ) ;
181+ process . exit ( 1 ) ;
182+ } ) ;
183+ }
229184
230185// Main execution
231186if ( usePM2 ) {
232187 startWithPM2 ( ) ;
233188} else {
234189 startWithNode ( ) ;
235- }
236-
237- // Try to automatically open browser after a short delay (only in dev mode and not with PM2)
238- if ( ! usePM2 && process . env . NODE_ENV !== 'production' ) {
239- setTimeout ( ( ) => {
240- try {
241- require ( 'open' ) ( clientSiteUrl ) ;
242- } catch ( err ) {
243- console . warn ( '[ReactPress Client] Failed to open browser automatically' ) ;
244- }
245- } , 3000 ) ;
246190}
0 commit comments