@echo off

setlocal

set NPMW_DIR=%~dp0

<%_ if (buildToolMaven) { _%>
set NODE_EXE=^"^"
set NODE_PATH=%NPMW_DIR%target\node\
set PNPM_EXE=^"%NPMW_DIR%target\node\pnpm.cmd^"
set INSTALL_NPM_COMMAND=^"%NPMW_DIR%mvnw.cmd^" -Pwebapp frontend:install-node-and-pnpm@install-node-and-pnpm
<%_ } else if (buildToolGradle) { _%>
set NODE_EXE=^"%NPMW_DIR%build\node\bin\node.exe^"
set NODE_PATH=%NPMW_DIR%build\node\bin\
set PNPM_EXE=^"%NPMW_DIR%build\node\lib\node_modules\pnpm\bin\pnpm.cjs^"
set INSTALL_NPM_COMMAND=^"%NPMW_DIR%gradlew.bat^" pnpmSetup --no-daemon
<%_ } _%>

if not exist %NPM_EXE% (
  call %INSTALL_NPM_COMMAND%
)

if exist %NODE_EXE% (
  Rem execute local pnpm with local node, whilst adding local node location to the PATH for this CMD session
  endlocal & echo "%PATH%"|find /i "%NODE_PATH%;">nul || set "PATH=%NODE_PATH%;%PATH%" & call %NODE_EXE% %PNPM_EXE% %*
) else if exist %PNPM_EXE% (
  Rem execute local pnpm, whilst adding local pnpm location to the PATH for this CMD session
  endlocal & echo "%PATH%"|find /i "%NODE_PATH%;">nul || set "PATH=%NODE_PATH%;%PATH%" & call %PNPM_EXE% %*
) else (
  call pnpm %*
)
