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

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.
@@ -1,302 +1,45 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const { program } = require('commander');
4
- const { execSync } = require('child_process');
3
+ /**
4
+ * ReactPress Server CLI Entry Point
5
+ * This script allows starting the ReactPress server via npx
6
+ */
7
+
5
8
  const path = require('path');
6
9
  const fs = require('fs');
7
- const chalk = require('chalk');
8
- const inquirer = require('inquirer');
9
-
10
- program
11
- .name('@fecommunity/reactpress-server')
12
- .description('ReactPress Server CLI')
13
- .version('1.0.0');
14
-
15
- // Init command - 初始化服务器配置
16
- program
17
- .command('init')
18
- .description('Initialize ReactPress server configuration')
19
- .option('-d, --database <type>', 'Database type (mysql|postgres)', 'mysql')
20
- .option('-p, --port <port>', 'Server port', '3002')
21
- .option('--skip-db-setup', 'Skip database setup')
22
- .action(async (options) => {
23
- console.log(chalk.blue('🚀 Initializing ReactPress Server...\n'));
24
-
25
- const cwd = process.cwd();
26
- const envPath = path.join(cwd, '.env');
27
-
28
- // 检查.env文件是否已存在
29
- if (fs.existsSync(envPath)) {
30
- const { overwrite } = await inquirer.prompt([
31
- {
32
- type: 'confirm',
33
- name: 'overwrite',
34
- message: 'Configuration file (.env) already exists. Overwrite?',
35
- default: false
36
- }
37
- ]);
38
-
39
- if (!overwrite) {
40
- console.log(chalk.yellow('⚠️ Initialization cancelled.'));
41
- process.exit(0);
42
- }
43
- }
44
-
45
- // 收集配置信息
46
- console.log(chalk.cyan('📋 Please provide the following configuration:\n'));
47
-
48
- const config = await inquirer.prompt([
49
- {
50
- type: 'input',
51
- name: 'DB_HOST',
52
- message: 'Database host:',
53
- default: 'localhost'
54
- },
55
- {
56
- type: 'input',
57
- name: 'DB_PORT',
58
- message: 'Database port:',
59
- default: options.database === 'postgres' ? '5432' : '3306'
60
- },
61
- {
62
- type: 'input',
63
- name: 'DB_USERNAME',
64
- message: 'Database username:',
65
- default: 'root'
66
- },
67
- {
68
- type: 'password',
69
- name: 'DB_PASSWORD',
70
- message: 'Database password:',
71
- mask: '*'
72
- },
73
- {
74
- type: 'input',
75
- name: 'DB_DATABASE',
76
- message: 'Database name:',
77
- default: 'reactpress'
78
- },
79
- {
80
- type: 'input',
81
- name: 'SERVER_PORT',
82
- message: 'Server port:',
83
- default: options.port
84
- },
85
- {
86
- type: 'input',
87
- name: 'JWT_SECRET',
88
- message: 'JWT secret (leave empty to generate):',
89
- default: ''
90
- },
91
- {
92
- type: 'input',
93
- name: 'ADMIN_USER',
94
- message: 'Admin username:',
95
- default: 'admin'
96
- },
97
- {
98
- type: 'password',
99
- name: 'ADMIN_PASSWD',
100
- message: 'Admin password:',
101
- default: 'admin',
102
- mask: '*'
103
- }
104
- ]);
105
-
106
- // 如果没有提供JWT密钥则生成一个
107
- if (!config.JWT_SECRET) {
108
- config.JWT_SECRET = require('crypto').randomBytes(32).toString('hex');
109
- console.log(chalk.green('🔐 Generated JWT secret'));
110
- }
111
-
112
- // 创建.env文件
113
- const envContent = `# ReactPress Server Configuration
114
- # Generated on ${new Date().toISOString()}
115
-
116
- # Database Configuration
117
- DB_TYPE=${options.database}
118
- DB_HOST=${config.DB_HOST}
119
- DB_PORT=${config.DB_PORT}
120
- DB_USERNAME=${config.DB_USERNAME}
121
- DB_PASSWORD=${config.DB_PASSWORD}
122
- DB_DATABASE=${config.DB_DATABASE}
123
10
 
124
- # Server Configuration
125
- SERVER_PORT=${config.SERVER_PORT}
126
- SERVER_API_PREFIX=/api
127
-
128
- # Security
129
- JWT_SECRET=${config.JWT_SECRET}
130
- ADMIN_USER=${config.ADMIN_USER}
131
- ADMIN_PASSWD=${config.ADMIN_PASSWD}
132
-
133
- # Environment
134
- NODE_ENV=development
135
- `;
136
-
137
- fs.writeFileSync(envPath, envContent);
138
- console.log(chalk.green('✅ Configuration saved to .env file'));
139
-
140
- if (!options.skipDbSetup) {
141
- console.log(chalk.yellow('\n🗄️ Setting up database...'));
142
- console.log(chalk.gray(`Please ensure your ${options.database} server is running and create the database:`));
143
- console.log(chalk.cyan(`CREATE DATABASE ${config.DB_DATABASE};`));
144
- console.log(chalk.gray('Then run: npx @reactpress/server start'));
145
- }
146
-
147
- console.log(chalk.green('\n🎉 ReactPress Server initialized successfully!'));
148
- console.log(chalk.cyan('\nNext steps:'));
149
- console.log(chalk.gray('1. Start the server: npx @fecommunity/reactpress-server start'));
150
- console.log(chalk.gray('2. Visit API: http://localhost:' + config.SERVER_PORT + '/api'));
151
- console.log(chalk.gray('3. API docs: http://localhost:' + config.SERVER_PORT + '/api (Swagger UI)'));
11
+ // Get the directory where this script is located
12
+ const binDir = __dirname;
13
+ const serverDir = path.join(binDir, '..');
14
+ const distPath = path.join(serverDir, 'dist', 'main.js');
15
+
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'], {
23
+ stdio: 'inherit',
24
+ cwd: serverDir
152
25
  });
153
-
154
- // Start command - 启动服务器
155
- program
156
- .command('start')
157
- .description('Start ReactPress server')
158
- .option('-p, --port <port>', 'Server port')
159
- .option('-d, --dev', 'Start in development mode')
160
- .option('--watch', 'Watch for file changes (dev mode)')
161
- .action(async (options) => {
162
- console.log(chalk.blue('🚀 Starting ReactPress Server...\n'));
163
-
164
- const cwd = process.cwd();
165
- const envPath = path.join(cwd, '.env');
166
-
167
- // 检查.env文件是否存在
168
- if (!fs.existsSync(envPath)) {
169
- console.log(chalk.red('❌ Configuration file (.env) not found!'));
170
- console.log(chalk.yellow('Please run: npx @fecommunity/reactpress-server init'));
171
- process.exit(1);
172
- }
173
-
174
- // 检查是否在项目根目录(有server目录)
175
- const serverPath = path.join(cwd, 'server');
176
- const hasServerDir = fs.existsSync(serverPath);
177
-
178
- let serverMainPath;
179
- if (hasServerDir) {
180
- // 在项目根目录,使用相对路径
181
- serverMainPath = path.join(serverPath, 'dist', 'main.js');
182
- const packageDir = serverPath;
183
-
184
- // 检查构建文件是否存在
185
- if (!fs.existsSync(serverMainPath)) {
186
- console.log(chalk.yellow('⚠️ Built server not found, building...'));
187
- try {
188
- execSync('pnpm run build:server', { cwd: cwd, stdio: 'inherit' });
189
- } catch (error) {
190
- console.log(chalk.red('❌ Build failed'));
191
- process.exit(1);
192
- }
193
- }
194
- } else {
195
- // 可能在独立安装的@reactpress/server包中
196
- try {
197
- const serverPackagePath = require.resolve('@fecommunity/reactpress-server/package.json');
198
- const serverPackageDir = path.dirname(serverPackagePath);
199
- serverMainPath = path.join(serverPackageDir, 'dist', 'main.js');
200
- } catch (error) {
201
- console.log(chalk.red('❌ ReactPress server package not found!'));
202
- console.log(chalk.yellow('Please install: npm install @fecommunity/reactpress-server'));
203
- process.exit(1);
204
- }
205
- }
206
-
207
- // 设置环境变量
208
- const env = {
209
- ...process.env,
210
- NODE_ENV: options.dev ? 'development' : 'production'
211
- };
212
-
213
- if (options.port) {
214
- env.SERVER_PORT = options.port;
215
- }
216
-
217
- try {
218
- if (options.dev || options.watch) {
219
- console.log(chalk.yellow('🔄 Starting in development mode...'));
220
- if (hasServerDir) {
221
- execSync('pnpm run dev:server', {
222
- cwd: cwd,
223
- stdio: 'inherit',
224
- env
225
- });
226
- } else {
227
- console.log(chalk.yellow('⚠️ Development mode requires server source code'));
228
- console.log(chalk.cyan('Starting in production mode instead...'));
229
- execSync(`node ${serverMainPath}`, {
230
- cwd: process.cwd(),
231
- stdio: 'inherit',
232
- env
233
- });
234
- }
235
- } else {
236
- console.log(chalk.green('🌟 Starting in production mode...'));
237
- execSync(`node ${serverMainPath}`, {
238
- cwd: process.cwd(),
239
- stdio: 'inherit',
240
- env
241
- });
242
- }
243
- } catch (error) {
244
- console.log(chalk.red('❌ Failed to start server'));
245
- process.exit(1);
246
- }
247
- });
248
-
249
- // Build command - 构建服务器
250
- program
251
- .command('build')
252
- .description('Build ReactPress server')
253
- .action(() => {
254
- console.log(chalk.blue('🔨 Building ReactPress Server...\n'));
255
-
256
- const cwd = process.cwd();
257
- const serverPath = path.join(cwd, 'server');
258
-
259
- if (!fs.existsSync(serverPath)) {
260
- console.log(chalk.red('❌ Server directory not found!'));
261
- console.log(chalk.yellow('This command should be run from the project root directory.'));
262
- process.exit(1);
263
- }
264
-
265
- try {
266
- execSync('pnpm run build:server', {
267
- cwd: cwd,
268
- stdio: 'inherit'
269
- });
270
- console.log(chalk.green('✅ Build completed successfully!'));
271
- } catch (error) {
272
- console.log(chalk.red('❌ Build failed'));
273
- process.exit(1);
274
- }
275
- });
276
-
277
- // Dev command - 开发模式启动
278
- program
279
- .command('dev')
280
- .description('Start server in development mode')
281
- .option('-p, --port <port>', 'Server port')
282
- .action((options) => {
283
- console.log(chalk.blue('🔄 Starting ReactPress Server in development mode...\n'));
284
-
285
- // 调用start命令的dev模式
286
- const args = ['start', '--dev'];
287
- if (options.port) {
288
- args.push('--port', options.port);
289
- }
290
-
291
- // 重新调用当前脚本
292
- const child = require('child_process').spawn('node', [__filename, ...args], {
293
- stdio: 'inherit',
294
- env: process.env
295
- });
296
-
297
- child.on('exit', (code) => {
298
- process.exit(code);
299
- });
300
- });
301
-
302
- program.parse();
26
+
27
+ if (buildResult.status !== 0) {
28
+ console.error('[ReactPress Server] Failed to build server');
29
+ process.exit(1);
30
+ }
31
+ }
32
+
33
+ // Change to the server directory
34
+ process.chdir(serverDir);
35
+
36
+ // Set environment variables
37
+ process.env.NODE_ENV = process.env.NODE_ENV || 'production';
38
+
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);
45
+ }
package/dist/main.d.ts CHANGED
@@ -1 +1,6 @@
1
+ declare global {
2
+ var installationServer: any;
3
+ var installationPort: number;
4
+ var isInstalling: boolean;
5
+ }
1
6
  export {};