feat(npx): added scaffolding for 'npx sim' command that relies on localStorage only, can be ran from anywhere with Nodejs

This commit is contained in:
Waleed Latif
2025-03-04 11:45:55 -08:00
parent d309176c5e
commit b3347afa4e
34 changed files with 3835 additions and 90 deletions

92
scripts/build-standalone.js Executable file
View File

@@ -0,0 +1,92 @@
#!/usr/bin/env node
/**
* Build Standalone Distribution
*
* This script builds a standalone distribution of Sim Studio that can be downloaded
* and run by the CLI with `npx sim`.
*
* The standalone package includes:
* - Pre-built Next.js static export
* - Simplified Express server
* - Configured to use localStorage instead of a database
*/
const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')
const packageJson = require('../package.json')
// Configuration
const STANDALONE_DIR = path.join(__dirname, '../standalone-dist')
const SOURCE_DIR = path.join(__dirname, '..')
const OUTPUT_TARBALL = path.join(__dirname, '../sim-standalone.tar.gz')
console.log('🔨 Building Sim Studio standalone distribution')
// Clean up if the directory exists
if (fs.existsSync(STANDALONE_DIR)) {
console.log('Cleaning up previous build...')
fs.rmSync(STANDALONE_DIR, { recursive: true, force: true })
}
// Create standalone directory
fs.mkdirSync(STANDALONE_DIR, { recursive: true })
fs.mkdirSync(path.join(STANDALONE_DIR, 'public'), { recursive: true })
// Build Next.js static export
console.log('Building Next.js static export...')
try {
// Set environment variable for static export with localStorage
process.env.USE_LOCAL_STORAGE = 'true'
process.env.NEXT_PUBLIC_USE_LOCAL_STORAGE = 'true'
// Build the app
execSync('npm run build', {
cwd: SOURCE_DIR,
stdio: 'inherit',
env: {
...process.env,
USE_LOCAL_STORAGE: 'true',
NEXT_PUBLIC_USE_LOCAL_STORAGE: 'true',
NODE_ENV: 'production',
},
})
// Copy the output to standalone directory
console.log('Copying files to standalone directory...')
fs.cpSync(path.join(SOURCE_DIR, 'out'), path.join(STANDALONE_DIR, 'public'), { recursive: true })
} catch (error) {
console.error('Error building Next.js static export:', error)
process.exit(1)
}
// Copy standalone server files
console.log('Copying standalone server files...')
fs.copyFileSync(
path.join(SOURCE_DIR, 'packages/@sim/cli/standalone/server.js'),
path.join(STANDALONE_DIR, 'server.js')
)
fs.copyFileSync(
path.join(SOURCE_DIR, 'packages/@sim/cli/standalone/package.json'),
path.join(STANDALONE_DIR, 'package.json')
)
// Create tarball
console.log('Creating tarball...')
try {
execSync(`tar -czf "${OUTPUT_TARBALL}" -C "${STANDALONE_DIR}" .`, {
stdio: 'inherit',
})
console.log(`✅ Standalone distribution created: ${OUTPUT_TARBALL}`)
console.log(`📦 Size: ${(fs.statSync(OUTPUT_TARBALL).size / (1024 * 1024)).toFixed(2)} MB`)
} catch (error) {
console.error('Error creating tarball:', error)
process.exit(1)
}
console.log('\n🚀 Next steps:')
console.log('1. Upload the tarball to your release assets')
console.log('2. Update the DOWNLOAD_URL in the CLI code to point to your release')
console.log('3. Publish the CLI package to npm with: npm run cli:publish')

33
scripts/setup_cli.sh Normal file
View File

@@ -0,0 +1,33 @@
#!/bin/bash
set -e
echo "Setting up Sim Studio CLI Package..."
# Create directory structure if it doesn't exist
mkdir -p packages/@sim/cli/bin
mkdir -p packages/@sim/cli/src/commands
mkdir -p packages/@sim/cli/src/utils
# Navigate to CLI directory
cd packages/@sim/cli
# Install dependencies
echo "Installing CLI dependencies..."
npm install
# Build the CLI package
echo "Building CLI package..."
npm run build
# Make the CLI executable
chmod +x bin/sim.js
echo "✅ CLI setup complete!"
echo ""
echo "You can now run:"
echo " npm run cli:start - to test the CLI"
echo " npm run cli:dev - to develop the CLI with live reload"
echo " npm run cli:publish - to publish to npm"
echo ""
echo "Try it out with: ./packages/@sim/cli/bin/sim.js"

View File

@@ -0,0 +1,25 @@
#!/bin/bash
# Check if .env file exists, if not, create from example
if [ ! -f .env ]; then
echo "Creating .env file from .env.example..."
cp .env.example .env
echo "Please update .env file with your configuration."
fi
# Stop any running containers
docker compose down
# Build and start containers in detached mode
docker compose up --build -d
# Wait for database to be ready
echo "Waiting for database to be ready..."
sleep 5
# Apply migrations automatically
echo "Applying database migrations..."
docker compose exec simstudio npm run db:push
echo "Sim Studio is now running at http://localhost:3000"
echo "To view logs, run: docker compose logs -f simstudio"