#!/usr/bin/env bash
# This is essentially npm run --workspace --if-present SCRIPT but in parallel
set -eou pipefail
if [ $# -ne 1 ]; then
    echo "Usage: npm-run-parallel NPM_SCRIPT_NAME"
    exit 1
fi
SCRIPT=$1

if [[ "$(uname)" == "Darwin"* ]]
then
    # rust-parallel doesn't seem to be available for darwin in nix so we fallback to the sequential version
    npm run --workspaces --if-present "$SCRIPT"
else
    npm query .workspace --json | jq -r '.[].location' | xargs -I {} sh -c "if jq -e .scripts.[\\\"$SCRIPT\\\"] {}/package.json > /dev/null; then echo {}; fi" | rust-parallel npm run "$SCRIPT" --workspace
fi
