@fecommunity/reactpress-server 1.0.0-beta.21 → 1.0.0-beta.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,7 +7,11 @@ ReactPress Server - NestJS-based backend API for ReactPress CMS with WordPress-s
7
7
  ### Installation & Setup
8
8
 
9
9
  ```bash
10
+ # Regular startup
10
11
  npx @fecommunity/reactpress-server
12
+
13
+ # PM2 startup
14
+ npx @fecommunity/reactpress-server --pm2
11
15
  ```
12
16
 
13
17
  That's it! The command will automatically:
@@ -25,6 +29,7 @@ That's it! The command will automatically:
25
29
  - 🔌 **Database auto-setup** - Creates MySQL database if it doesn't exist
26
30
  - 🎯 **Seamless flow** - From installation to running server in minutes
27
31
  - 📖 **Auto-documentation** - Swagger API docs available immediately
32
+ - ⚡ **PM2 support** - Optional PM2 process management for production
28
33
 
29
34
  ### Requirements
30
35
 
@@ -50,6 +55,21 @@ npx @fecommunity/reactpress-server
50
55
  npx @fecommunity/reactpress-client
51
56
  ```
52
57
 
58
+ ### PM2 Support
59
+
60
+ ReactPress server supports PM2 process management for production deployments:
61
+
62
+ ```bash
63
+ # Start with PM2
64
+ npx @fecommunity/reactpress-server --pm2
65
+ ```
66
+
67
+ PM2 features:
68
+ - Automatic process restart on crash
69
+ - Memory monitoring
70
+ - Log management
71
+ - Cluster mode support
72
+
53
73
  ### Configuration
54
74
 
55
75
  The installation wizard will create a `.env` file with:
@@ -84,6 +104,9 @@ npm install
84
104
 
85
105
  # Start development server
86
106
  npm run dev
107
+
108
+ # Start with PM2 (development)
109
+ npm run pm2:start
87
110
  ```
88
111
 
89
112
  ### Docker Support
@@ -3,43 +3,188 @@
3
3
  /**
4
4
  * ReactPress Server CLI Entry Point
5
5
  * This script allows starting the ReactPress server via npx
6
+ * Supports both regular and PM2 startup modes
6
7
  */
7
8
 
8
9
  const path = require('path');
9
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
+ }
10
37
 
11
38
  // Get the directory where this script is located
12
39
  const binDir = __dirname;
13
40
  const serverDir = path.join(binDir, '..');
14
41
  const distPath = path.join(serverDir, 'dist', 'main.js');
42
+ const ecosystemPath = path.join(serverDir, 'ecosystem.config.js');
15
43
 
16
- // Check if the server is built
17
- if (!fs.existsSync(distPath)) {
18
- console.log('[ReactPress Server] Server not built yet. Building...');
19
-
20
- // Try to build the server
21
- const { spawnSync } = require('child_process');
22
- const buildResult = spawnSync('npm', ['run', 'build'], {
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'], {
23
64
  stdio: 'inherit',
24
65
  cwd: serverDir
25
66
  });
26
67
 
27
- if (buildResult.status !== 0) {
28
- console.error('[ReactPress Server] Failed to build server');
29
- process.exit(1);
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
+ });
30
149
  }
31
150
  }
32
151
 
33
- // Change to the server directory
34
- process.chdir(serverDir);
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';
35
175
 
36
- // Set environment variables
37
- process.env.NODE_ENV = process.env.NODE_ENV || 'production';
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
+ }
38
184
 
39
- // Import and run the server
40
- try {
41
- require(distPath);
42
- } catch (error) {
43
- console.error('[ReactPress Server] Failed to start server:', error);
44
- process.exit(1);
185
+ // Main execution
186
+ if (usePM2) {
187
+ startWithPM2();
188
+ } else {
189
+ startWithNode();
45
190
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fecommunity/reactpress-server",
3
- "version": "1.0.0-beta.21",
3
+ "version": "1.0.0-beta.22",
4
4
  "description": "ReactPress Server - NestJS-based backend API for ReactPress CMS",
5
5
  "author": "fecommunity",
6
6
  "license": "MIT",
@@ -44,14 +44,20 @@
44
44
  "dev": "nest start --watch",
45
45
  "debug": "nest start --debug --watch",
46
46
  "start": "cross-env NODE_ENV=production node dist/main",
47
- "pm2": "pm2 start npm --name @reactpress/server -- start",
47
+ "pm2": "pm2 start npm --name @fecommunity/reactpress-server -- start",
48
48
  "lint": "tslint -p tsconfig.json -c tslint.json",
49
49
  "test": "jest",
50
50
  "test:watch": "jest --watch",
51
51
  "test:cov": "jest --coverage",
52
52
  "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
53
53
  "test:e2e": "jest --config ./test/jest-e2e.json",
54
- "prepublishOnly": "npm run build"
54
+ "prepublishOnly": "npm run build",
55
+ "pm2:start": "pm2 start dist/main.js --name reactpress-server",
56
+ "pm2:stop": "pm2 stop reactpress-server",
57
+ "pm2:restart": "pm2 restart reactpress-server",
58
+ "pm2:delete": "pm2 delete reactpress-server",
59
+ "pm2:logs": "pm2 logs reactpress-server",
60
+ "pm2:status": "pm2 status reactpress-server"
55
61
  },
56
62
  "dependencies": {
57
63
  "@fecommunity/reactpress-config": "^1.0.0-beta.7",
@@ -99,7 +105,8 @@
99
105
  "ua-parser-js": "^0.7.28",
100
106
  "open": "^8.2.1",
101
107
  "express": "^4.18.2",
102
- "@nestjs/cli": "^6.9.0"
108
+ "@nestjs/cli": "^6.9.0",
109
+ "pm2": "^5.2.0"
103
110
  },
104
111
  "devDependencies": {
105
112
  "@nestjs/schematics": "^6.7.0",