Skip to content

Commit e7f7b97

Browse files
committed
feat: support npx install server
1 parent 9e8166f commit e7f7b97

File tree

7 files changed

+254
-13
lines changed

7 files changed

+254
-13
lines changed

client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fecommunity/reactpress-client",
3-
"version": "1.0.1-beta.3",
3+
"version": "1.0.1-beta.5",
44
"scripts": {
55
"prebuild": "rimraf .next",
66
"build": "next build",

client/public/sitemap.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
3-
<url><loc>http://localhost:3001</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-14T15:50:54.017Z</lastmod></url>
4-
<url><loc>http://localhost:3001/archives</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-14T15:50:54.018Z</lastmod></url>
5-
<url><loc>http://localhost:3001/knowledge</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-14T15:50:54.018Z</lastmod></url>
6-
<url><loc>http://localhost:3001/login</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-14T15:50:54.018Z</lastmod></url>
7-
<url><loc>http://localhost:3001/nav</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-14T15:50:54.018Z</lastmod></url>
8-
<url><loc>http://localhost:3001/rss</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-14T15:50:54.018Z</lastmod></url>
3+
<url><loc>http://localhost:3001</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-20T02:57:50.549Z</lastmod></url>
4+
<url><loc>http://localhost:3001/archives</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-20T02:57:50.550Z</lastmod></url>
5+
<url><loc>http://localhost:3001/knowledge</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-20T02:57:50.550Z</lastmod></url>
6+
<url><loc>http://localhost:3001/login</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-20T02:57:50.550Z</lastmod></url>
7+
<url><loc>http://localhost:3001/nav</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-20T02:57:50.550Z</lastmod></url>
8+
<url><loc>http://localhost:3001/rss</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2025-09-20T02:57:50.550Z</lastmod></url>
99
</urlset>

config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fecommunity/reactpress-config",
3-
"version": "1.0.0-beta.11",
3+
"version": "1.0.0-beta.15",
44
"description": "ReactPress Configuration - Environment and i18n configuration for ReactPress",
55
"author": "fecommunity",
66
"main": "./lib/index.js",

server/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ ReactPress Server - NestJS-based backend API for ReactPress CMS with WordPress-s
77
### Installation & Setup
88

99
```bash
10+
# Regular startup
1011
npx @fecommunity/reactpress-server
12+
13+
# PM2 startup
14+
npx @fecommunity/reactpress-server --pm2
1115
```
1216

1317
That's it! The command will automatically:
@@ -25,6 +29,7 @@ That's it! The command will automatically:
2529
- 🔌 **Database auto-setup** - Creates MySQL database if it doesn't exist
2630
- 🎯 **Seamless flow** - From installation to running server in minutes
2731
- 📖 **Auto-documentation** - Swagger API docs available immediately
32+
-**PM2 support** - Optional PM2 process management for production
2833

2934
### Requirements
3035

@@ -50,6 +55,21 @@ npx @fecommunity/reactpress-server
5055
npx @fecommunity/reactpress-client
5156
```
5257

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+
5373
### Configuration
5474

5575
The installation wizard will create a `.env` file with:
@@ -84,6 +104,9 @@ npm install
84104

85105
# Start development server
86106
npm run dev
107+
108+
# Start with PM2 (development)
109+
npm run pm2:start
87110
```
88111

89112
### Docker Support

server/bin/reactpress-server.js

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
}

server/ecosystem.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
apps: [
3+
{
4+
name: 'reactpress-server',
5+
script: './dist/main.js',
6+
instances: 1,
7+
autorestart: true,
8+
watch: false,
9+
max_memory_restart: '1G',
10+
env: {
11+
NODE_ENV: 'production',
12+
},
13+
env_development: {
14+
NODE_ENV: 'development',
15+
},
16+
},
17+
],
18+
};

server/package.json

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
22
"name": "@fecommunity/reactpress-server",
3-
"version": "1.0.0-beta.19",
3+
"version": "1.0.0-beta.22",
44
"description": "ReactPress Server - NestJS-based backend API for ReactPress CMS",
55
"author": "fecommunity",
66
"license": "MIT",
77
"main": "dist/main.js",
88
"types": "dist/index.d.ts",
9+
"bin": {
10+
"reactpress-server": "./bin/reactpress-server.js"
11+
},
912
"files": [
1013
"dist/**/*",
1114
"public/**/*",
@@ -41,14 +44,20 @@
4144
"dev": "nest start --watch",
4245
"debug": "nest start --debug --watch",
4346
"start": "cross-env NODE_ENV=production node dist/main",
44-
"pm2": "pm2 start npm --name @reactpress/server -- start",
47+
"pm2": "pm2 start npm --name @fecommunity/reactpress-server -- start",
4548
"lint": "tslint -p tsconfig.json -c tslint.json",
4649
"test": "jest",
4750
"test:watch": "jest --watch",
4851
"test:cov": "jest --coverage",
4952
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
5053
"test:e2e": "jest --config ./test/jest-e2e.json",
51-
"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"
5261
},
5362
"dependencies": {
5463
"@fecommunity/reactpress-config": "^1.0.0-beta.7",
@@ -96,7 +105,8 @@
96105
"ua-parser-js": "^0.7.28",
97106
"open": "^8.2.1",
98107
"express": "^4.18.2",
99-
"@nestjs/cli": "^6.9.0"
108+
"@nestjs/cli": "^6.9.0",
109+
"pm2": "^5.2.0"
100110
},
101111
"devDependencies": {
102112
"@nestjs/schematics": "^6.7.0",
@@ -137,4 +147,4 @@
137147
"coverageDirectory": "../coverage",
138148
"testEnvironment": "node"
139149
}
140-
}
150+
}

0 commit comments

Comments
 (0)