1+ #!/usr/bin/env node
2+
3+ /**
4+ * ReactPress Server CLI Entry Point
5+ * This script allows starting the ReactPress server via npx
6+ * Supports both regular and PM2 startup modes
7+ */
8+
9+ const path = require ( 'path' ) ;
10+ const fs = require ( 'fs' ) ;
11+ const { spawn, spawnSync } = require ( 'child_process' ) ;
12+
13+ // Get command line arguments
14+ const args = process . argv . slice ( 2 ) ;
15+ const usePM2 = args . includes ( '--pm2' ) ;
16+ const showHelp = args . includes ( '--help' ) || args . includes ( '-h' ) ;
17+
18+ // Show help if requested
19+ if ( showHelp ) {
20+ console . log ( `
21+ ReactPress Server - NestJS-based backend API for ReactPress CMS
22+
23+ Usage:
24+ npx @fecommunity/reactpress-server [options]
25+
26+ Options:
27+ --pm2 Start server with PM2 process manager
28+ --help, -h Show this help message
29+
30+ Examples:
31+ npx @fecommunity/reactpress-server # Start server normally
32+ npx @fecommunity/reactpress-server --pm2 # Start server with PM2
33+ npx @fecommunity/reactpress-server --help # Show this help message
34+ ` ) ;
35+ process . exit ( 0 ) ;
36+ }
37+
38+ // Get the directory where this script is located
39+ const binDir = __dirname ;
40+ const serverDir = path . join ( binDir , '..' ) ;
41+ const distPath = path . join ( serverDir , 'dist' , 'main.js' ) ;
42+ const ecosystemPath = path . join ( serverDir , 'ecosystem.config.js' ) ;
43+
44+ // Function to check if PM2 is installed
45+ function isPM2Installed ( ) {
46+ try {
47+ require . resolve ( 'pm2' ) ;
48+ return true ;
49+ } catch ( e ) {
50+ // Check if PM2 is installed globally
51+ try {
52+ spawnSync ( 'pm2' , [ '--version' ] , { stdio : 'ignore' } ) ;
53+ return true ;
54+ } catch ( e ) {
55+ return false ;
56+ }
57+ }
58+ }
59+
60+ // Function to install PM2
61+ function installPM2 ( ) {
62+ console . log ( '[ReactPress Server] Installing PM2...' ) ;
63+ const installResult = spawnSync ( 'npm' , [ 'install' , 'pm2' , '--no-save' ] , {
64+ stdio : 'inherit' ,
65+ cwd : serverDir
66+ } ) ;
67+
68+ if ( installResult . status !== 0 ) {
69+ console . error ( '[ReactPress Server] Failed to install PM2' ) ;
70+ return false ;
71+ }
72+
73+ return true ;
74+ }
75+
76+ // Function to start with PM2
77+ function startWithPM2 ( ) {
78+ // Check if PM2 is installed
79+ if ( ! isPM2Installed ( ) ) {
80+ // Try to install PM2
81+ if ( ! installPM2 ( ) ) {
82+ console . error ( '[ReactPress Server] Cannot start with PM2' ) ;
83+ process . exit ( 1 ) ;
84+ }
85+ }
86+
87+ // Check if the server is built
88+ if ( ! fs . existsSync ( distPath ) ) {
89+ console . log ( '[ReactPress Server] Server not built yet. Building...' ) ;
90+
91+ // Try to build the server
92+ const buildResult = spawnSync ( 'npm' , [ 'run' , 'build' ] , {
93+ stdio : 'inherit' ,
94+ cwd : serverDir
95+ } ) ;
96+
97+ if ( buildResult . status !== 0 ) {
98+ console . error ( '[ReactPress Server] Failed to build server' ) ;
99+ process . exit ( 1 ) ;
100+ }
101+ }
102+
103+ console . log ( '[ReactPress Server] Starting with PM2...' ) ;
104+
105+ // Use ecosystem.config.js if it exists, otherwise use direct command
106+ let pm2Command = 'pm2' ;
107+ try {
108+ // Try to resolve PM2 path
109+ pm2Command = path . join ( serverDir , 'node_modules' , '.bin' , 'pm2' ) ;
110+ if ( ! fs . existsSync ( pm2Command ) ) {
111+ pm2Command = 'pm2' ;
112+ }
113+ } catch ( e ) {
114+ pm2Command = 'pm2' ;
115+ }
116+
117+ // Check if ecosystem.config.js exists
118+ if ( fs . existsSync ( ecosystemPath ) ) {
119+ const pm2 = spawn ( pm2Command , [ 'start' , ecosystemPath ] , {
120+ stdio : 'inherit' ,
121+ cwd : serverDir
122+ } ) ;
123+
124+ pm2 . on ( 'close' , ( code ) => {
125+ console . log ( `[ReactPress Server] PM2 process exited with code ${ code } ` ) ;
126+ process . exit ( code ) ;
127+ } ) ;
128+
129+ pm2 . on ( 'error' , ( error ) => {
130+ console . error ( '[ReactPress Server] Failed to start with PM2:' , error ) ;
131+ process . exit ( 1 ) ;
132+ } ) ;
133+ } else {
134+ // Fallback to direct start
135+ const pm2 = spawn ( pm2Command , [ 'start' , distPath , '--name' , 'reactpress-server' ] , {
136+ stdio : 'inherit' ,
137+ cwd : serverDir
138+ } ) ;
139+
140+ pm2 . on ( 'close' , ( code ) => {
141+ console . log ( `[ReactPress Server] PM2 process exited with code ${ code } ` ) ;
142+ process . exit ( code ) ;
143+ } ) ;
144+
145+ pm2 . on ( 'error' , ( error ) => {
146+ console . error ( '[ReactPress Server] Failed to start with PM2:' , error ) ;
147+ process . exit ( 1 ) ;
148+ } ) ;
149+ }
150+ }
151+
152+ // Function to start with regular Node.js
153+ function startWithNode ( ) {
154+ // Check if the server is built
155+ if ( ! fs . existsSync ( distPath ) ) {
156+ console . log ( '[ReactPress Server] Server not built yet. Building...' ) ;
157+
158+ // Try to build the server
159+ const buildResult = spawnSync ( 'npm' , [ 'run' , 'build' ] , {
160+ stdio : 'inherit' ,
161+ cwd : serverDir
162+ } ) ;
163+
164+ if ( buildResult . status !== 0 ) {
165+ console . error ( '[ReactPress Server] Failed to build server' ) ;
166+ process . exit ( 1 ) ;
167+ }
168+ }
169+
170+ // Change to the server directory
171+ process . chdir ( serverDir ) ;
172+
173+ // Set environment variables
174+ process . env . NODE_ENV = process . env . NODE_ENV || 'production' ;
175+
176+ // Import and run the server
177+ try {
178+ require ( distPath ) ;
179+ } catch ( error ) {
180+ console . error ( '[ReactPress Server] Failed to start server:' , error ) ;
181+ process . exit ( 1 ) ;
182+ }
183+ }
184+
185+ // Main execution
186+ if ( usePM2 ) {
187+ startWithPM2 ( ) ;
188+ } else {
189+ startWithNode ( ) ;
190+ }
0 commit comments