#!/bin/bash

build_legacy_caching() {
    echo "Building legacy caching module..."
    cd sources/legacy-caching
    npm clean-install
    npm run build
    cd ../..
}

cd sources

case "$1" in
    all)
        cd ..
        build_legacy_caching
        cd sources
        npm run all
        ;;
    act)
        # Build and copy outputs to the dist directory
        cd ..
        build_legacy_caching
        cd sources
        npm run build
        cd ..
        cp -r sources/dist .
        # Run act
        $@
        # Revert the changes to the dist directory
        git checkout -- dist
        ;;
    dist)
        cd ..
        build_legacy_caching
        cd sources
        npm clean-install
        npm run build
        cd ..
        cp -r sources/dist .
        ;;
    init-scripts)
        cd test/init-scripts
        ./gradlew check
        ;;
    install)
        cd ..
        build_legacy_caching
        cd sources
        npm clean-install
        npm run build
        ;;
    test)
        shift
        npm test -- $@
        ;;
    *)
        cd ..
        build_legacy_caching
        cd sources
        npm run build
        ;;
esac
