diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..11cfd54 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,33 @@ +name: Build and Upload Release + +on: + release: + types: [published] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Build Card (Production) + run: | + chmod +x run.sh + ./run.sh build npm prod + + - name: Show dist contents (sanity check) + run: ls -la dist || true + + - name: Upload build to release + uses: softprops/action-gh-release@v2 + with: + files: dist/** + fail_on_unmatched_files: true + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml new file mode 100644 index 0000000..f33f106 --- /dev/null +++ b/.github/workflows/validate.yml @@ -0,0 +1,23 @@ +name: HACS Validation + +on: + push: + pull_request: + schedule: + - cron: "0 0 * * *" + workflow_dispatch: + +permissions: {} + +jobs: + validate-hacs: + runs-on: "ubuntu-latest" + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: HACS validation + uses: "hacs/action@main" + with: + category: "plugin" + diff --git a/Dockerfile.build b/Dockerfile.build index b6f6a8f..c99fbfb 100644 --- a/Dockerfile.build +++ b/Dockerfile.build @@ -18,5 +18,6 @@ COPY src/ ./src/ # Default command: run the build # This ensures the build runs when the container starts, not during image creation -CMD ["npm", "run", "build"] +# Use BUILD_TARGET to allow running "watch" or other scripts +CMD ["sh", "-c", "npm run ${BUILD_TARGET:-build}"] diff --git a/package.json b/package.json index 350880b..1f402b2 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-typescript": "^11.1.6", "rollup": "^2.79.1", + "rollup-plugin-terser": "^7.0.2", "typescript": "~5.3.3" }, "scripts": { diff --git a/rollup.config.js b/rollup.config.js index 5507d7a..425ce18 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,12 +1,16 @@ import resolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; import typescript from '@rollup/plugin-typescript'; +import { terser } from 'rollup-plugin-terser'; + +const isProd = process.env.NODE_ENV === 'production'; export default { input: 'src/bom-local-radar-card.ts', output: { file: 'dist/bom-local-radar-card.js', format: 'es', + sourcemap: !isProd, }, onwarn(warning, warn) { if (warning.code === 'THIS_IS_UNDEFINED') { @@ -21,6 +25,12 @@ export default { commonjs(), typescript({ tsconfig: './tsconfig.json', + declaration: false, + }), + isProd && terser({ + output: { + comments: false, + }, }), ], // Dependencies are bundled (not external) so the card works in HA diff --git a/run.sh b/run.sh index 664ffb1..f7fbdb7 100755 --- a/run.sh +++ b/run.sh @@ -1,6 +1,6 @@ #!/bin/bash # Main entry point - works with or without npm -# Usage: ./run.sh [build|test|clean] [docker] +# Usage: ./run.sh [build|test|clean] [docker|npm] [prod] set -e @@ -9,13 +9,20 @@ cd "$SCRIPT_DIR" COMMAND="${1:-test}" BUILD_METHOD="${2:-}" +BUILD_TARGET="${3:-}" # Build function - handles both npm and Docker build_card() { local method="$1" + local target="$2" # Auto-detect if not specified - if [ -z "$method" ]; then + if [ -z "$method" ] || [ "$method" = "prod" ]; then + # If the second arg was "prod", treat it as the target and auto-detect method + if [ "$method" = "prod" ]; then + target="prod" + fi + if command -v docker &> /dev/null; then method="docker" elif command -v npm &> /dev/null; then @@ -27,16 +34,21 @@ build_card() { fi fi + # Set NODE_ENV for production builds + if [ "$target" = "prod" ]; then + export NODE_ENV=production + fi + if [ "$method" = "docker" ]; then - echo "🐳 Building with Docker..." + echo "🐳 Building with Docker ($target)..." if ! command -v docker &> /dev/null; then echo "❌ Error: docker not found" exit 1 fi # Pass FORCE_REBUILD if set - FORCE_REBUILD="${FORCE_REBUILD:-0}" bash scripts/build-docker.sh + FORCE_REBUILD="${FORCE_REBUILD:-0}" NODE_ENV="$NODE_ENV" bash scripts/build-docker.sh else - echo "🔨 Building with npm..." + echo "🔨 Building with npm ($target)..." if ! command -v npm &> /dev/null; then echo "❌ Error: npm not found" echo " Install Node.js/npm or use: ./run.sh build docker" @@ -48,7 +60,7 @@ build_card() { npm install fi - npm run build + NODE_ENV="$NODE_ENV" npm run build fi } @@ -179,7 +191,7 @@ update_card() { case "$COMMAND" in build) - build_card "$BUILD_METHOD" + build_card "$BUILD_METHOD" "$BUILD_TARGET" ;; test) ./scripts/test.sh diff --git a/scripts/build-docker.sh b/scripts/build-docker.sh index 27c4c6f..f42d1a5 100755 --- a/scripts/build-docker.sh +++ b/scripts/build-docker.sh @@ -38,18 +38,15 @@ fi # Run the build container and copy file out echo "🔨 Running build..." -CONTAINER_ID=$(docker create bom-local-card-builder) -docker start -a "$CONTAINER_ID" +# Pass NODE_ENV through to the container if set +ENV_ARGS="" +if [ -n "$NODE_ENV" ]; then + echo " Setting NODE_ENV=$NODE_ENV" + ENV_ARGS="-e NODE_ENV=$NODE_ENV" +fi -# Ensure dist directory exists -mkdir -p dist - -# Copy the built file from container -echo "📋 Copying built file from container..." -docker cp "$CONTAINER_ID:/build/dist/bom-local-radar-card.js" dist/bom-local-radar-card.js - -# Remove the container -docker rm "$CONTAINER_ID" > /dev/null +# Run the container (using volume for dist to get the output) +docker run --rm $ENV_ARGS -v "$(pwd)/dist:/build/dist" bom-local-card-builder # Check if build succeeded if [ ! -f "dist/bom-local-radar-card.js" ]; then