# Baserow Web-Frontend Justfile
# NOTE: All recipes here must work the same inside Docker and on local dev setups
#       without requiring any changes. Please make sure to not add docker-specific
#       commands or assumptions here.
# Uses yarn for Node.js package management

# Import personal recipes if they exist (not tracked in git)
import? 'local.just'

# Note: just doesn't support dynamic dotenv-filename, so we handle env loading in recipes
_load_env := 'if [ -f "../.env.local" ]; then set -a; source "../.env.local"; set +a; fi'

# Default recipe - show help
default:
    @just --list

# Show documentation links for each command group
[group('0 - help')]
help:
    @echo "Baserow Web-Frontend - Documentation Links"
    @echo "==========================================="
    @echo ""
    @echo "Setup & Development:"
    @echo "  docs/development/running-the-dev-env-locally.md     - Local development setup"
    @echo "  docs/development/running-the-dev-env-with-docker.md - Docker development setup"
    @echo "  docs/development/directory-structure.md             - Project structure"
    @echo ""
    @echo "Testing:"
    @echo "  docs/development/running-tests.md                   - Running frontend tests"
    @echo "  docs/development/e2e-testing.md                     - End-to-end testing"
    @echo ""
    @echo "Code Quality:"
    @echo "  docs/development/code-quality.md                    - Linting and formatting"
    @echo ""
    @echo "Build & Production:"
    @echo "  docs/development/building-and-running-production-images.md - Production images"
    @echo ""
    @echo "IDE Setup:"
    @echo "  docs/development/vscode-setup.md                    - VS Code configuration"
    @echo ""
    @echo "Other:"
    @echo "  docs/development/justfile.md                        - Just command reference"
    @echo ""
    @echo "Quick shortcuts: l=lint, t=test, dev=run-dev-server"

# Allow `just frontend <cmd>` or `just f <cmd>` to work (for consistency with root justfile)
frontend *ARGS:
    @just {{ ARGS }}

# Common shortcuts
alias t := test
alias dev := run-dev-server
alias serve := run-dev-server

# Pass-through to yarn
[group('7 - passthrough')]
yarn *ARGS:
    yarn {{ ARGS }}

# Pass-through to npm
[group('7 - passthrough')]
npm *ARGS:
    npm {{ ARGS }}

# =============================================================================
# Setup & Installation
# =============================================================================

# Install dependencies
[group('1 - setup')]
install:
    yarn install

# =============================================================================
# Code Quality & Linting
# =============================================================================

# Run all linters (eslint + stylelint + prettier). Pass file paths to lint only those files
# (repo-root-relative, e.g. web-frontend/modules/foo.js). Paths outside the three
# frontend trees are silently skipped.
#
# Each tool is invoked in the same cwd as the matching yarn script:
#   - eslint runs from the repo root (where eslint.config.mjs lives and where
#     "files: [premium/web-frontend/**/*]" path patterns can match correctly)
#   - stylelint and prettier run from web-frontend/ so their .stylelintignore and
#     .prettierignore files (which live there) get auto-discovered
# Per-file results therefore match the no-args path, with one intentional
# difference: eslint is invoked with `--no-warn-ignored` so that when
# pre-commit (or a manual diff-driven invocation) passes a file that lives in
# an eslint-ignored location, we don't emit a noisy warning per file. The
# no-args path doesn't need this flag because it walks directories.
[group('4 - code-quality')]
[positional-arguments]
check *ARGS:
    #!/usr/bin/env bash
    set -euo pipefail

    if [ $# -eq 0 ]; then
        yarn run lint
        exit 0
    fi

    JS_FILES=()      # repo-root-relative, for eslint (run from repo root)
    SCSS_FILES=()    # web-frontend-relative, for stylelint (run from web-frontend/)
    PRETTIER_FILES=()  # web-frontend-relative, for prettier (run from web-frontend/)
    for f in "$@"; do
        case "$f" in
            web-frontend/*) rel="${f#web-frontend/}" ;;
            premium/web-frontend/*|enterprise/web-frontend/*) rel="../$f" ;;
            *) continue ;;
        esac
        case "$f" in
            *.js|*.jsx|*.ts|*.tsx|*.vue|*.mjs|*.cjs) JS_FILES+=("$f"); PRETTIER_FILES+=("$rel") ;;
            *.scss) SCSS_FILES+=("$rel"); PRETTIER_FILES+=("$rel") ;;
            *.css|*.json|*.md|*.html|*.yml|*.yaml) PRETTIER_FILES+=("$rel") ;;
        esac
    done

    if [ ${#JS_FILES[@]} -gt 0 ]; then
        [ -f .nuxt/eslint.config.mjs ] || yarn postinstall
        (cd .. && web-frontend/node_modules/.bin/eslint --no-warn-ignored --cache --cache-location=web-frontend/.cache/eslint "${JS_FILES[@]}")
    fi
    if [ ${#SCSS_FILES[@]} -gt 0 ]; then
        ./node_modules/.bin/stylelint --cache --cache-location=.cache/stylelint "${SCSS_FILES[@]}"
    fi
    if [ ${#PRETTIER_FILES[@]} -gt 0 ]; then
        ./node_modules/.bin/prettier --check --log-level=warn --cache --cache-location=.cache/prettier "${PRETTIER_FILES[@]}"
    fi

alias lint := check
alias l := check

# Fix code style issues. Pass file paths to fix only those files (repo-root-relative,
# e.g. web-frontend/modules/foo.js). Paths outside the three frontend trees are
# silently skipped. See `lint` above for the per-tool cwd rationale.
[group('4 - code-quality')]
[positional-arguments]
fix *ARGS:
    #!/usr/bin/env bash
    set -euo pipefail

    if [ $# -eq 0 ]; then
        yarn run fix
        exit 0
    fi

    JS_FILES=()
    SCSS_FILES=()
    PRETTIER_FILES=()
    for f in "$@"; do
        case "$f" in
            web-frontend/*) rel="${f#web-frontend/}" ;;
            premium/web-frontend/*|enterprise/web-frontend/*) rel="../$f" ;;
            *) continue ;;
        esac
        case "$f" in
            *.js|*.jsx|*.ts|*.tsx|*.vue|*.mjs|*.cjs) JS_FILES+=("$f"); PRETTIER_FILES+=("$rel") ;;
            *.scss) SCSS_FILES+=("$rel"); PRETTIER_FILES+=("$rel") ;;
            *.css|*.json|*.md|*.html|*.yml|*.yaml) PRETTIER_FILES+=("$rel") ;;
        esac
    done

    if [ ${#JS_FILES[@]} -gt 0 ]; then
        [ -f .nuxt/eslint.config.mjs ] || yarn postinstall
        (cd .. && web-frontend/node_modules/.bin/eslint --no-warn-ignored --cache --cache-location=web-frontend/.cache/eslint --fix "${JS_FILES[@]}")
    fi
    if [ ${#SCSS_FILES[@]} -gt 0 ]; then
        ./node_modules/.bin/stylelint --cache --cache-location=.cache/stylelint --fix "${SCSS_FILES[@]}"
    fi
    if [ ${#PRETTIER_FILES[@]} -gt 0 ]; then
        ./node_modules/.bin/prettier --write --log-level=warn --cache --cache-location=.cache/prettier "${PRETTIER_FILES[@]}"
    fi

alias format := fix
alias f := fix

# =============================================================================
# Testing
# =============================================================================

# Run tests
[group('3 - testing')]
test *ARGS:
    yarn test {{ ARGS }}

# Run tests with coverage (for CI)
[group('3 - testing')]
ci-test:
    yarn test:coverage

# Update Vitest snapshots
[group('3 - testing')]
update-snapshots:
    EXTRA_VITEST_PARAMS="--update" yarn test

# =============================================================================
# Development
# =============================================================================

# Run development server
[group('2 - development')]
run-dev-server:
    #!/usr/bin/env bash
    set -euo pipefail
    {{ _load_env }}
    NUXT_HOST="${BASEROW_NUXT_HOST:-0.0.0.0}"
    NUXT_PORT="${BASEROW_NUXT_PORT:-3000}"
    yarn run dev --host "$NUXT_HOST" --port "$NUXT_PORT"

# Run Storybook
[group('2 - development')]
storybook:
    #!/usr/bin/env bash
    set -euo pipefail
    {{ _load_env }}
    STORYBOOK_HOST="${BASEROW_STORYBOOK_HOST:-0.0.0.0}"
    STORYBOOK_PORT="${BASEROW_STORYBOOK_PORT:-6006}"
    yarn run storybook --host "$STORYBOOK_HOST" -p "$STORYBOOK_PORT"

# Build Nuxt for production
[group('5 - build')]
build-nuxt:
    yarn run build

# =============================================================================
# Cleanup
# =============================================================================

# Clean Nuxt build artifacts
[group('6 - cleanup')]
clean-nuxt:
    rm -rf .nuxt/ dist/

# Clean all (Nuxt build artifacts + node_modules)
[group('6 - cleanup')]
clean-all: clean-nuxt
    rm -rf node_modules/
