Skip to content

Commit

Permalink
chore: add npm wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Denysenko committed Nov 18, 2023
1 parent 78ae9d7 commit 7b7d6b1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
42 changes: 42 additions & 0 deletions npmw
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/sh

basedir=`dirname "$0"`

if [ -f "$basedir/mvnw" ]; then
bindir="$basedir/target/node"
repodir="$basedir/target/node/node_modules"
installCommand="$basedir/mvnw -Pwebapp frontend:install-node-and-npm@install-node-and-npm"

PATH="$basedir/$builddir/:$PATH"
NPM_EXE="$basedir/$builddir/node_modules/npm/bin/npm-cli.js"
NODE_EXE="$basedir/$builddir/node"
elif [ -f "$basedir/gradlew" ]; then
bindir="$basedir/build/node/bin"
repodir="$basedir/build/node/lib/node_modules"
installCommand="$basedir/gradlew npmSetup"
else
echo "Using npm installed globally"
exec npm "$@"
fi

NPM_EXE="$repodir/npm/bin/npm-cli.js"
NODE_EXE="$bindir/node"

if [ ! -x "$NPM_EXE" ] || [ ! -x "$NODE_EXE" ]; then
$installCommand || true
fi

if [ -x "$NODE_EXE" ]; then
echo "Using node installed locally $($NODE_EXE --version)"
PATH="$bindir:$PATH"
else
NODE_EXE='node'
fi

if [ ! -x "$NPM_EXE" ]; then
echo "Local npm not found, using npm installed globally"
npm "$@"
else
echo "Using npm installed locally $($NODE_EXE $NPM_EXE --version)"
$NODE_EXE $NPM_EXE "$@"
fi
31 changes: 31 additions & 0 deletions npmw.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@echo off

setlocal

set NPMW_DIR=%~dp0

if exist "%NPMW_DIR%mvnw.cmd" (
set NODE_EXE=^"^"
set NODE_PATH=%NPMW_DIR%target\node\
set NPM_EXE=^"%NPMW_DIR%target\node\npm.cmd^"
set INSTALL_NPM_COMMAND=^"%NPMW_DIR%mvnw.cmd^" -Pwebapp frontend:install-node-and-npm@install-node-and-npm
) else (
set NODE_EXE=^"%NPMW_DIR%build\node\bin\node.exe^"
set NODE_PATH=%NPMW_DIR%build\node\bin\
set NPM_EXE=^"%NPMW_DIR%build\node\lib\node_modules\npm\bin\npm-cli.js^"
set INSTALL_NPM_COMMAND=^"%NPMW_DIR%gradlew.bat^" npmSetup
)

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

if exist %NODE_EXE% (
Rem execute local npm 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% %NPM_EXE% %*
) else if exist %NPM_EXE% (
Rem execute local npm, whilst adding local npm location to the PATH for this CMD session
endlocal & echo "%PATH%"|find /i "%NODE_PATH%;">nul || set "PATH=%NODE_PATH%;%PATH%" & call %NPM_EXE% %*
) else (
call npm %*
)

0 comments on commit 7b7d6b1

Please sign in to comment.