#!/bin/bash
###############################################################################
# VChart Component Deployment Script
#
# This script helps deploy the VChart component in various scenarios
###############################################################################
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
COMPONENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DIST_DIR="${COMPONENT_DIR}/../../dist/vchart-component"
DEMO_FILE="${COMPONENT_DIR}/demo.html"
###############################################################################
# Helper Functions
###############################################################################
print_header() {
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}========================================${NC}"
}
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
print_error() {
echo -e "${RED}✗ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠ $1${NC}"
}
print_info() {
echo -e "${BLUE}ℹ $1${NC}"
}
###############################################################################
# Deployment Functions
###############################################################################
check_dependencies() {
print_header "Checking Dependencies"
# Check Node.js
if command -v node &> /dev/null; then
NODE_VERSION=$(node --version)
print_success "Node.js: $NODE_VERSION"
else
print_error "Node.js not found. Please install Node.js >= 16.0.0"
exit 1
fi
# Check npm
if command -v npm &> /dev/null; then
NPM_VERSION=$(npm --version)
print_success "npm: $NPM_VERSION"
else
print_error "npm not found"
exit 1
fi
# Check TypeScript
if command -v tsc &> /dev/null; then
TSC_VERSION=$(tsc --version)
print_success "TypeScript: $TSC_VERSION"
else
print_warning "TypeScript not found globally (using local)"
fi
}
run_tests() {
print_header "Running Tests"
cd "${COMPONENT_DIR}"
if [ -f "package.json" ]; then
npm test
print_success "All tests passed"
else
print_warning "No package.json found, skipping tests"
fi
}
build_typescript() {
print_header "Building TypeScript"
cd "${COMPONENT_DIR}"
if [ -f "tsconfig.json" ]; then
npx tsc
print_success "TypeScript compiled successfully"
else
print_error "tsconfig.json not found"
exit 1
fi
}
package_component() {
print_header "Packaging Component"
# Create dist directory
mkdir -p "${DIST_DIR}"
# Copy essential files
cp "${COMPONENT_DIR}"/*.ts "${DIST_DIR}/" 2>/dev/null || true
cp "${COMPONENT_DIR}"/*.tsx "${DIST_DIR}/" 2>/dev/null || true
cp -r "${COMPONENT_DIR}/specs" "${DIST_DIR}/" 2>/dev/null || true
cp -r "${COMPONENT_DIR}/component" "${DIST_DIR}/" 2>/dev/null || true
cp "${COMPONENT_DIR}/package.json" "${DIST_DIR}/" 2>/dev/null || true
cp "${COMPONENT_DIR}/README.md" "${DIST_DIR}/" 2>/dev/null || true
cp "${COMPONENT_DIR}/QUICK_START.md" "${DIST_DIR}/" 2>/dev/null || true
print_success "Component packaged to ${DIST_DIR}"
}
deploy_standalone_demo() {
print_header "Deploying Standalone Demo"
if [ -f "${DEMO_FILE}" ]; then
# Ask for deployment location
echo -e "${BLUE}Enter deployment directory (default: ./demo):${NC}"
read DEPLOY_DIR
DEPLOY_DIR=${DEPLOY_DIR:-./demo}
mkdir -p "${DEPLOY_DIR}"
cp "${DEMO_FILE}" "${DEPLOY_DIR}/index.html"
print_success "Demo deployed to ${DEPLOY_DIR}/index.html"
print_info "Open in browser: file://${DEPLOY_DIR}/index.html"
else
print_error "demo.html not found"
exit 1
fi
}
create_apaas_package() {
print_header "Creating aPaaS Package"
PACKAGE_DIR="${COMPONENT_DIR}/../../apaas-package"
mkdir -p "${PACKAGE_DIR}"
# Copy aPaaS component
cp -r "${COMPONENT_DIR}/component/tiktok-dashboard" "${PACKAGE_DIR}/"
# Copy dependencies
cp -r "${COMPONENT_DIR}/specs" "${PACKAGE_DIR}/"
cp "${COMPONENT_DIR}"/{types,utils,data-fetcher,responsive}.ts "${PACKAGE_DIR}/" 2>/dev/null || true
# Create zip
cd "${PACKAGE_DIR}/.."
zip -r vchart-apaas-component.zip apaas-package/
print_success "aPaaS package created: vchart-apaas-component.zip"
}
start_dev_server() {
print_header "Starting Development Server"
if [ -f "${DEMO_FILE}" ]; then
# Try to use python simple server
if command -v python3 &> /dev/null; then
print_info "Starting server at http://localhost:8000"
cd "${COMPONENT_DIR}"
python3 -m http.server 8000
elif command -v python &> /dev/null; then
print_info "Starting server at http://localhost:8000"
cd "${COMPONENT_DIR}"
python -m SimpleHTTPServer 8000
else
print_error "Python not found. Cannot start server."
print_info "Please open demo.html directly in your browser"
fi
else
print_error "demo.html not found"
exit 1
fi
}
show_usage() {
cat << EOF
VChart Component Deployment Script
Usage: $0 [command]
Commands:
check Check dependencies
test Run tests
build Build TypeScript
package Package component for distribution
demo Deploy standalone demo
apaas Create aPaaS package
serve Start development server
all Run all steps (check, test, build, package)
help Show this help message
Examples:
$0 check # Check dependencies
$0 test # Run tests
$0 all # Full deployment
$0 demo # Deploy demo
$0 serve # Start dev server
EOF
}
###############################################################################
# Main Script
###############################################################################
main() {
case "${1:-help}" in
check)
check_dependencies
;;
test)
run_tests
;;
build)
build_typescript
;;
package)
package_component
;;
demo)
deploy_standalone_demo
;;
apaas)
create_apaas_package
;;
serve)
start_dev_server
;;
all)
check_dependencies
run_tests
build_typescript
package_component
print_header "Deployment Complete"
print_success "All steps completed successfully!"
;;
help|--help|-h)
show_usage
;;
*)
print_error "Unknown command: $1"
show_usage
exit 1
;;
esac
}
# Run main function
main "$@"