@gv-sh/specgen-app 0.4.0 → 0.5.0

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
@@ -1,6 +1,6 @@
1
1
  # SpecGen App - Complete Platform
2
2
 
3
- [![Version](https://img.shields.io/badge/version-0.4.0-blue.svg)](https://github.com/gv-sh/specgen-app)
3
+ [![Version](https://img.shields.io/badge/version-0.5.0-blue.svg)](https://github.com/gv-sh/specgen-app)
4
4
 
5
5
  A unified deployment package for the SpecGen speculative fiction generator platform.
6
6
 
@@ -29,6 +29,23 @@ npx @gv-sh/specgen-app dev
29
29
  npx @gv-sh/specgen-app production
30
30
  ```
31
31
 
32
+ ### Low Memory Mode for Small Servers
33
+
34
+ If you're deploying on a small EC2 instance or any server with limited RAM:
35
+
36
+ ```bash
37
+ # First, add swap space to prevent out-of-memory errors
38
+ sudo fallocate -l 2G /swapfile
39
+ sudo chmod 600 /swapfile
40
+ sudo mkswap /swapfile
41
+ sudo swapon /swapfile
42
+ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
43
+
44
+ # Then use the low-memory versions of the commands
45
+ npx @gv-sh/specgen-app setup-low-memory
46
+ npx @gv-sh/specgen-app production-low-memory
47
+ ```
48
+
32
49
  ### Using NPM Scripts
33
50
 
34
51
  1. **Setup**:
@@ -79,21 +96,29 @@ If you have SSH access to a server (like an EC2 instance), this is the fastest w
79
96
 
80
97
  4. **Install and run SpecGen**:
81
98
  ```bash
99
+ # Add swap space to prevent out-of-memory errors
100
+ sudo fallocate -l 2G /swapfile
101
+ sudo chmod 600 /swapfile
102
+ sudo mkswap /swapfile
103
+ sudo swapon /swapfile
104
+ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
105
+
82
106
  # Create and enter project directory
83
107
  mkdir specgen
84
108
  cd specgen
85
109
 
86
- # Run the setup
87
- npx @gv-sh/specgen-app setup
110
+ # Run the setup with low memory optimization
111
+ npx @gv-sh/specgen-app setup-low-memory
88
112
 
89
- # Start in production mode
90
- npx @gv-sh/specgen-app production
113
+ # Start in production mode with low memory optimization
114
+ npx @gv-sh/specgen-app production-low-memory
91
115
  ```
92
116
 
93
117
  5. **Keep the service running with PM2** (recommended):
94
118
  ```bash
95
119
  sudo npm install -g pm2
96
- pm2 start "npx @gv-sh/specgen-app production" --name "specgen"
120
+ # Use the low-memory production mode with PM2
121
+ pm2 start "npx @gv-sh/specgen-app production-low-memory" --name "specgen"
97
122
  pm2 startup
98
123
  pm2 save
99
124
  ```
@@ -233,7 +258,9 @@ npm run deploy:backup
233
258
  The following commands are available when using `npx @gv-sh/specgen-app` or `specgen-app` (if installed globally):
234
259
 
235
260
  - `setup` - Set up the SpecGen application
261
+ - `setup-low-memory` - Set up the SpecGen application with memory optimizations
236
262
  - `production` - Run the application in production mode
263
+ - `production-low-memory` - Run the application in production mode with memory optimizations
237
264
  - `dev` - Run the application in development mode
238
265
  - `deploy` - Deploy the application
239
266
  - `deploy:stop` - Stop the deployed application
package/bin/cli.js CHANGED
@@ -38,9 +38,15 @@ switch (command) {
38
38
  case 'setup':
39
39
  runScript('setup');
40
40
  break;
41
+ case 'setup-low-memory':
42
+ runScript('setup-low-memory');
43
+ break;
41
44
  case 'production':
42
45
  runScript('production');
43
46
  break;
47
+ case 'production-low-memory':
48
+ runScript('production-low-memory');
49
+ break;
44
50
  case 'dev':
45
51
  runScript('dev');
46
52
  break;
@@ -69,7 +75,9 @@ switch (command) {
69
75
  console.log('Usage: specgen-app <command>');
70
76
  console.log('\nAvailable commands:');
71
77
  console.log(' setup - Set up the SpecGen application');
78
+ console.log(' setup-low-memory - Set up the SpecGen application with memory optimizations');
72
79
  console.log(' production - Run the application in production mode');
80
+ console.log(' production-low-memory - Run the application in production mode with memory optimizations');
73
81
  console.log(' dev - Run the application in development mode');
74
82
  console.log(' deploy - Deploy the application');
75
83
  console.log(' deploy:stop - Stop the deployed application');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gv-sh/specgen-app",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Complete SpecGen application with server, admin, and user interfaces",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -8,10 +8,12 @@
8
8
  },
9
9
  "scripts": {
10
10
  "setup": "chmod +x scripts/setup.sh && ./scripts/setup.sh",
11
+ "setup-low-memory": "chmod +x scripts/setup-low-memory.sh && ./scripts/setup-low-memory.sh",
11
12
  "dev": "chmod +x scripts/dev.sh && ./scripts/dev.sh",
12
13
  "build": "cd admin && npm run build && cd ../user && npm run build",
13
14
  "start": "cd server && npm start",
14
15
  "production": "chmod +x scripts/production.sh && ./scripts/production.sh",
16
+ "production-low-memory": "chmod +x scripts/production-low-memory.sh && ./scripts/production-low-memory.sh",
15
17
  "make-executable": "chmod +x scripts/make-executable.sh && ./scripts/make-executable.sh",
16
18
  "deploy": "chmod +x scripts/deploy.sh && ./scripts/deploy.sh",
17
19
  "deploy:stop": "chmod +x scripts/stop.sh && ./scripts/stop.sh",
@@ -9,4 +9,8 @@ find scripts -name "*.sh" -exec chmod +x {} \;
9
9
  # Make CLI executable
10
10
  chmod +x bin/cli.js
11
11
 
12
+ # Make sure the low memory scripts are executable
13
+ chmod +x scripts/setup-low-memory.sh
14
+ chmod +x scripts/production-low-memory.sh
15
+
12
16
  echo "✅ All scripts are now executable"
@@ -0,0 +1,119 @@
1
+ #!/bin/bash
2
+
3
+ # SpecGen Production Script - Low Memory Version
4
+ set -e
5
+
6
+ echo "🚀 Starting SpecGen in production mode (Low Memory)..."
7
+
8
+ # Function to check if port is available
9
+ check_port() {
10
+ local port=$1
11
+ if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then
12
+ return 1 # Port in use
13
+ else
14
+ return 0 # Port available
15
+ fi
16
+ }
17
+
18
+ # Verify OpenAI API key
19
+ if [ -f "server/.env" ]; then
20
+ # In CI mode, skip API key validation
21
+ if [ "$CI" = "true" ]; then
22
+ echo "CI mode detected - skipping API key validation"
23
+ elif grep -q "OPENAI_API_KEY=your_openai_api_key_here" server/.env; then
24
+ echo "⚠️ No OpenAI API key detected!"
25
+ echo "Enter your OpenAI API key: "
26
+ read -r OPENAI_KEY
27
+
28
+ if [ -z "$OPENAI_KEY" ]; then
29
+ echo "❌ No API key provided. Cannot start in production mode."
30
+ exit 1
31
+ else
32
+ # Update the API key in the .env file
33
+ sed -i.bak "s/OPENAI_API_KEY=.*/OPENAI_API_KEY=$OPENAI_KEY/" server/.env
34
+ rm -f server/.env.bak
35
+ echo "✅ API key updated in server/.env"
36
+ fi
37
+ fi
38
+ else
39
+ echo "❌ server/.env file not found. Run 'npm run setup' first."
40
+ exit 1
41
+ fi
42
+
43
+ # Verify directories exist
44
+ for dir in server admin user; do
45
+ if [ ! -d "$dir" ]; then
46
+ echo "❌ $dir directory not found. Run 'npm run setup' first."
47
+ exit 1
48
+ fi
49
+ done
50
+
51
+ # Make sure node_modules exist in the server directory
52
+ if [ ! -d "server/node_modules" ]; then
53
+ echo "Installing server dependencies..."
54
+ cd server
55
+ # Create a .npmrc file that ignores engine requirements
56
+ echo "engine-strict=false" > .npmrc
57
+ npm install --no-fund --no-audit --production --maxsockets=2 --loglevel=warn
58
+ cd ..
59
+ fi
60
+
61
+ # Set production environment
62
+ export NODE_ENV=production
63
+
64
+ # Check if we need to build
65
+ if [ ! -d "admin/build" ] || [ ! -d "user/build" ]; then
66
+ echo "Building web interfaces (optimized mode)..."
67
+
68
+ # Admin build
69
+ if [ ! -d "admin/build" ]; then
70
+ echo "Building admin..."
71
+ cd admin
72
+ echo "engine-strict=false" > .npmrc
73
+ # Install only production dependencies
74
+ npm install --no-fund --no-audit --production --maxsockets=2 --loglevel=warn
75
+ # Set environment for smaller build
76
+ export GENERATE_SOURCEMAP=false
77
+ export SKIP_PREFLIGHT_CHECK=true
78
+ npm run build
79
+ cd ..
80
+ fi
81
+
82
+ # User build
83
+ if [ ! -d "user/build" ]; then
84
+ echo "Building user..."
85
+ cd user
86
+ echo "engine-strict=false" > .npmrc
87
+ # Install only production dependencies
88
+ npm install --no-fund --no-audit --production --maxsockets=2 --loglevel=warn
89
+ # Set environment for smaller build
90
+ export GENERATE_SOURCEMAP=false
91
+ export SKIP_PREFLIGHT_CHECK=true
92
+ npm run build
93
+ cd ..
94
+ fi
95
+ fi
96
+
97
+ # Create production-ready .env for server
98
+ cat > server/.env.production << EOF
99
+ $(cat server/.env)
100
+ NODE_ENV=production
101
+ EOF
102
+
103
+ # Kill existing processes on required ports if needed
104
+ echo "Checking ports..."
105
+ for port in 3000 3001 3002; do
106
+ if ! check_port $port; then
107
+ echo "Port $port is in use. Attempting to free it..."
108
+ lsof -ti:$port | xargs kill -9 2>/dev/null || true
109
+ sleep 1
110
+ fi
111
+ done
112
+
113
+ # Start production server
114
+ echo "Starting production server..."
115
+ if [ "$CI" = "true" ]; then
116
+ echo "CI mode detected - skipping server start"
117
+ else
118
+ cd server && NODE_ENV=production npm start
119
+ fi
@@ -0,0 +1,203 @@
1
+ #!/bin/bash
2
+
3
+ # SpecGen Low-Memory Setup Script
4
+ set -e
5
+
6
+ echo "🚀 Setting up SpecGen App (Low Memory Mode)..."
7
+
8
+ # Check Node.js
9
+ if ! command -v npm &> /dev/null; then
10
+ echo "❌ npm not found. Please install Node.js from https://nodejs.org/"
11
+ exit 1
12
+ fi
13
+
14
+ # Function to safely remove directories with permission handling
15
+ safe_remove() {
16
+ if [ -d "$1" ]; then
17
+ echo "Removing $1..."
18
+ chmod -R u+w "$1" 2>/dev/null || true
19
+ rm -rf "$1" 2>/dev/null || true
20
+ # If still exists, try with sudo
21
+ if [ -d "$1" ]; then
22
+ echo "Using elevated permissions to remove $1..."
23
+ sudo rm -rf "$1" 2>/dev/null || true
24
+ fi
25
+ fi
26
+ }
27
+
28
+ # Clean existing directories with proper permission handling
29
+ echo "Cleaning existing directories..."
30
+ safe_remove "server"
31
+ safe_remove "admin"
32
+ safe_remove "user"
33
+ rm -f *.tgz package 2>/dev/null || true
34
+
35
+ # Install dependencies one at a time with reduced memory usage
36
+ echo "📦 Installing dependencies (low memory mode)..."
37
+ npm install --no-fund --no-audit --ignore-scripts --production=false --maxsockets=2 --loglevel=warn
38
+
39
+ # Create a .npmrc file that ignores engine requirements
40
+ echo "engine-strict=false" > .npmrc
41
+
42
+ # Download npm packages one at a time
43
+ echo "📦 Downloading packages..."
44
+ npm pack @gv-sh/specgen-server --loglevel=warn
45
+ npm pack @gv-sh/specgen-admin --loglevel=warn
46
+ npm pack @gv-sh/specgen-user --loglevel=warn
47
+
48
+ # Extract packages
49
+ echo "📁 Extracting packages..."
50
+
51
+ # Extract server
52
+ if [ -f "gv-sh-specgen-server-"*.tgz ]; then
53
+ echo "Extracting server..."
54
+ tar -xzf gv-sh-specgen-server-*.tgz
55
+ mv package server
56
+ rm gv-sh-specgen-server-*.tgz
57
+ fi
58
+
59
+ # Extract admin
60
+ if [ -f "gv-sh-specgen-admin-"*.tgz ]; then
61
+ echo "Extracting admin..."
62
+ tar -xzf gv-sh-specgen-admin-*.tgz
63
+ mv package admin
64
+ rm gv-sh-specgen-admin-*.tgz
65
+ fi
66
+
67
+ # Extract user
68
+ if [ -f "gv-sh-specgen-user-"*.tgz ]; then
69
+ echo "Extracting user..."
70
+ tar -xzf gv-sh-specgen-user-*.tgz
71
+ mv package user
72
+ rm gv-sh-specgen-user-*.tgz
73
+ fi
74
+
75
+ # Patch package.json files to fix React scripts
76
+ echo "🔧 Patching package scripts..."
77
+
78
+ # Fix user package start script
79
+ if [ -f "user/package.json" ]; then
80
+ echo "Fixing user package start script..."
81
+ # Replace the start script to use react-scripts instead of missing scripts/start.js
82
+ sed -i.bak 's|"start": "cross-env PORT=3002 REACT_APP_API_URL=http://localhost:3000 node scripts/start.js"|"start": "cross-env PORT=3002 REACT_APP_API_URL=http://localhost:3000 react-scripts start"|g' user/package.json
83
+ rm -f user/package.json.bak
84
+ fi
85
+
86
+ # Fix admin package start script if needed
87
+ if [ -f "admin/package.json" ]; then
88
+ echo "Checking admin package start script..."
89
+ # Check if admin has similar issue
90
+ if grep -q "node scripts/start.js" admin/package.json; then
91
+ echo "Fixing admin package start script..."
92
+ sed -i.bak 's|node scripts/start.js|react-scripts start|g' admin/package.json
93
+ rm -f admin/package.json.bak
94
+ fi
95
+ fi
96
+
97
+ # Create .npmrc files with engine-strict=false in each directory
98
+ echo "Creating .npmrc files to ignore engine requirements..."
99
+ for dir in server admin user; do
100
+ if [ -d "$dir" ]; then
101
+ echo "engine-strict=false" > "$dir/.npmrc"
102
+ fi
103
+ done
104
+
105
+ # Install dependencies for each component with memory optimizations
106
+ echo "📚 Installing dependencies (low memory mode)..."
107
+ for dir in server admin user; do
108
+ if [ -d "$dir" ]; then
109
+ echo "Installing $dir dependencies..."
110
+ (cd "$dir" && npm install --no-fund --no-audit --ignore-scripts --loglevel=warn --production=false --maxsockets=2 --force --omit=dev)
111
+ else
112
+ echo "⚠️ Warning: $dir directory not found"
113
+ fi
114
+ done
115
+
116
+ # Setup environment files
117
+ echo "🔧 Setting up environment files..."
118
+
119
+ # Server .env
120
+ if [ ! -f server/.env ] || [ "$CI" = "true" ]; then
121
+ # If in CI mode, use a dummy key
122
+ if [ "$CI" = "true" ]; then
123
+ # If in CI environment and .env already exists, just use it
124
+ if [ -f server/.env ]; then
125
+ echo "Using existing .env file for CI environment"
126
+ else
127
+ # Create a CI .env file
128
+ cat > server/.env << EOF
129
+ OPENAI_API_KEY=sk-test1234
130
+ NODE_ENV=test
131
+ PORT=3000
132
+ EOF
133
+ echo "Created test .env file for CI environment"
134
+ fi
135
+ KEY_PROVIDED=true
136
+ else
137
+ # Normal interactive mode
138
+ # Prompt for OpenAI API key
139
+ echo "To use SpecGen, you need an OpenAI API key."
140
+ echo "Enter your OpenAI API key (or press enter to set it later): "
141
+ read -r OPENAI_KEY
142
+
143
+ # If key is provided, use it, otherwise use placeholder
144
+ if [ -z "$OPENAI_KEY" ]; then
145
+ OPENAI_KEY="your_openai_api_key_here"
146
+ KEY_PROVIDED=false
147
+ else
148
+ KEY_PROVIDED=true
149
+ fi
150
+
151
+ cat > server/.env << EOF
152
+ # OpenAI API key
153
+ OPENAI_API_KEY=$OPENAI_KEY
154
+ NODE_ENV=development
155
+ PORT=3000
156
+ EOF
157
+
158
+ if [ "$KEY_PROVIDED" = true ]; then
159
+ echo "✅ OpenAI API key saved to server/.env"
160
+ else
161
+ echo "⚠️ No OpenAI API key provided. You'll need to add it later to server/.env"
162
+ fi
163
+ fi
164
+ fi
165
+
166
+ # Admin .env.development
167
+ if [ -d admin ]; then
168
+ cat > admin/.env.development << 'EOF'
169
+ REACT_APP_API_URL=http://localhost:3000
170
+ PORT=3001
171
+ SKIP_PREFLIGHT_CHECK=true
172
+ GENERATE_SOURCEMAP=false
173
+ EOF
174
+ fi
175
+
176
+ # User .env.development
177
+ if [ -d user ]; then
178
+ cat > user/.env.development << 'EOF'
179
+ REACT_APP_API_URL=http://localhost:3000
180
+ PORT=3002
181
+ SKIP_PREFLIGHT_CHECK=true
182
+ GENERATE_SOURCEMAP=false
183
+ EOF
184
+ fi
185
+
186
+ echo "✅ Low-memory setup complete!"
187
+ echo ""
188
+ echo "Next steps:"
189
+ if [ "$KEY_PROVIDED" = false ]; then
190
+ echo "1. Add your OpenAI API key to server/.env"
191
+ echo "2. Use 'npm run dev' to start all services"
192
+ else
193
+ echo "1. Use 'npm run dev' to start all services"
194
+ fi
195
+ echo ""
196
+ echo "If you're deploying on a low-memory system, we recommend:"
197
+ echo "1. npm run build (to create optimized bundles)"
198
+ echo "2. npm run production (to start in production mode)"
199
+ echo ""
200
+ echo "Access URLs:"
201
+ echo " 🌐 User Interface: http://localhost:3002"
202
+ echo " ⚙️ Admin Interface: http://localhost:3001"
203
+ echo " 🔧 API: http://localhost:3000"