Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer project scoped node/npm binaries over global binaries #14315

Merged
merged 2 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion generators/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const { STRING: TYPE_STRING, LONG: TYPE_LONG } = CommonDBTypes;

module.exports = class extends BaseGenerator {
constructor(args, options) {
super(args, options, { unique: 'namespace' });
super(args, options, { unique: 'namespace', customCommitTask: true });

/*
* When testing a generator with yeoman-test using 'withLocalConfig(localConfig)', it instantiates the
Expand Down
7 changes: 7 additions & 0 deletions generators/server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ const serverFiles = {
{ file: 'pom.xml', options: { interpolate: INTERPOLATE_REGEX } },
],
},
{
condition: generator => generator.buildTool === 'maven',
templates: [
{ file: 'npmw', method: 'copy', noEjs: true },
{ file: 'npmw.cmd', method: 'copy', noEjs: true },
],
},
],
serverResource: [
{
Expand Down
24 changes: 24 additions & 0 deletions generators/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ module.exports = class JHipsterServerGenerator extends BaseBlueprintGenerator {
this.jhipsterOldVersion = this.jhipsterConfig.jhipsterVersion;

useBlueprints = !this.fromBlueprint && this.instantiateBlueprints('server');

// Not using normal blueprints or this is a normal blueprint.
if (!useBlueprints || (this.fromBlueprint && this.sbsBlueprint)) {
this.setFeatures({
customInstallTask: function customInstallTask(preferredPm, defaultInstallTask) {
if ((preferredPm && preferredPm !== 'npm') || this.skipClient || this.jhipsterConfig.skipClient) {
return defaultInstallTask();
}
const gradle = this.jhipsterConfig.buildTool === 'gradle';
const command = gradle ? './gradlew' : './npmw';
const args = gradle ? ['npmInstall'] : ['install'];

const failureCallback = error => {
this.log(chalk.red(`Error executing '${command} ${args.join(' ')}', execute it yourself. (${error.shortMessage})`));
return true;
};

return this.spawnCommand(command, args, { preferLocal: true }).then(
() => true,
error => failureCallback(error)
);
}.bind(this),
});
}
}

// Public API method used by the getter and also by Blueprints
Expand Down
3 changes: 3 additions & 0 deletions generators/server/templates/gradle.properties.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ checkstyleVersion=8.40
## uncomment the below line to enable the selective mode

#org.gradle.configureondemand=true

## Install and use a local version of node and npm.
nodeInstall
23 changes: 23 additions & 0 deletions generators/server/templates/npmw
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

basedir=`dirname "$0"`

if [ -f "$basedir/mvnw" ]; then
builddir="target/node"
installCommand="$basedir/mvnw frontend:install-node-and-npm@install-node-and-npm"
else
builddir=".gradle/npm"
installCommand="$basedir/gradlew npmSetup"
fi

NPM_EXE="$basedir/$builddir/npm"

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

if ! [ -x "$NPM_EXE" ]; then
npm "$@"
else
$NPM_EXE "$@"
fi
24 changes: 24 additions & 0 deletions generators/server/templates/npmw.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@echo off

@setlocal

set NPMW_DIR=%~dp0

if exist "%NPMW_DIR%\mvnw.cmd" (
set NPM_EXE=%NPMW_DIR%\target\node\npm.cmd
set INSTALL_NPM_COMMAND=%NPMW_DIR%\mvnw.cmd frontend:install-node-and-npm@install-node-and-npm
) else (
set NPM_EXE=%NPMW_DIR%\.gradle\npm\npm.cmd
set INSTALL_NPM_COMMAND=%NPMW_DIR%\gradlew.bat npmSetup
)

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

if not exist %NPM_EXE% goto globalNpm

%NPM_EXE% %*

:globalNpm
npm %*
2 changes: 1 addition & 1 deletion generators/server/templates/pom.xml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@
<artifactId>frontend-maven-plugin</artifactId>
<executions>
<execution>
<id>install node and npm</id>
<id>install-node-and-npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
Expand Down
1 change: 0 additions & 1 deletion test-integration/scripts/10-install-jhipster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ if [[ "$JHI_REPO" == *"/generator-jhipster" ]]; then

cd "$JHI_HOME"
git --no-pager log -n 10 --graph --pretty='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
npm install -g npm@$(node -e "console.log(require('./generators/generator-constants').NPM_VERSION);") || true
npm ci
npm install -g "$JHI_HOME"
elif [[ "$JHI_GEN_BRANCH" == "release" ]]; then
Expand Down