Skip to content

Commit

Permalink
fix: lazily run npm install when applying 'prettier' and client modules
Browse files Browse the repository at this point in the history
If a package-lock.json is detected, and npm command is present, run npm install.

Fixes #11106
  • Loading branch information
murdos committed Oct 19, 2024
1 parent fa0c2f7 commit ee30e04
Show file tree
Hide file tree
Showing 31 changed files with 366 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.client.angular.core.domain.AngularModuleFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Service
public class AngularApplicationService {

private final AngularModuleFactory factory;

public AngularApplicationService() {
factory = new AngularModuleFactory();
public AngularApplicationService(NpmLazyInstaller npmLazyInstaller) {
factory = new AngularModuleFactory(npmLazyInstaller);
}

public JHipsterModule buildInitModule(JHipsterModuleProperties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tech.jhipster.lite.module.domain.Indentation;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.packagejson.PackageName;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

Expand All @@ -26,6 +27,11 @@ public class AngularModuleFactory {

private static final String ENGINES_NEEDLE = " \"engines\":";
private static final PackageName ANGULAR_CORE_PACKAGE = packageName("@angular/core");
private final NpmLazyInstaller npmLazyInstaller;

public AngularModuleFactory(NpmLazyInstaller npmLazyInstaller) {
this.npmLazyInstaller = npmLazyInstaller;
}

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
//@formatter:off
Expand Down Expand Up @@ -82,6 +88,9 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.addScript(scriptKey("watch:test"), scriptCommand("ng test --watch"))
.addScript(scriptKey("lint"), scriptCommand("eslint ."))
.and()
.postActions()
.add(context -> npmLazyInstaller.runInstallIn(context.projectFolder()))
.and()
.files()
.add(SOURCE.template("angular.json"), to("angular.json"))
.add(SOURCE.file("tsconfig.json"), to("tsconfig.json"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.client.react.core.domain.ReactCoreModulesFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Service
public class ReactCoreApplicationService {

private final ReactCoreModulesFactory factory;

public ReactCoreApplicationService() {
factory = new ReactCoreModulesFactory();
public ReactCoreApplicationService(NpmLazyInstaller npmLazyInstaller) {
factory = new ReactCoreModulesFactory(npmLazyInstaller);
}

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.module.domain.replacement.MandatoryReplacer;

Expand All @@ -47,6 +48,12 @@ public class ReactCoreModulesFactory {
private static final String TEST_PRIMARY = "src/test/webapp/unit/home/infrastructure/primary";
private static final String DEFAULT_TSCONFIG_PATH = "\"@/*\": [\"src/main/webapp/app/*\"]";

private final NpmLazyInstaller npmLazyInstaller;

public ReactCoreModulesFactory(NpmLazyInstaller npmLazyInstaller) {
this.npmLazyInstaller = npmLazyInstaller;
}

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
//@formatter:off
return moduleBuilder(properties)
Expand Down Expand Up @@ -74,7 +81,10 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.addScript(scriptKey("build:vite"), scriptCommand("vite build --emptyOutDir"))
.addScript(scriptKey("preview"), scriptCommand("vite preview"))
.addScript(scriptKey("start"), scriptCommand("vite"))
.and()
.and()
.postActions()
.add(context -> npmLazyInstaller.runInstallIn(context.projectFolder()))
.and()
.files()
.batch(SOURCE, to("."))
.addTemplate("vite.config.ts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.client.svelte.core.domain.SvelteModuleFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Service
public class SvelteApplicationService {

private final SvelteModuleFactory factory;

public SvelteApplicationService() {
this.factory = new SvelteModuleFactory();
public SvelteApplicationService(NpmLazyInstaller npmLazyInstaller) {
this.factory = new SvelteModuleFactory(npmLazyInstaller);
}

public JHipsterModule buildModule(JHipsterModuleProperties project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.shared.error.domain.Assert;

Expand All @@ -33,6 +34,12 @@ public class SvelteModuleFactory {
private static final JHipsterSource PRIMARY_TEST_SOURCE = SOURCE.append("src/test/unit/common/primary/app");
private static final JHipsterDestination PRIMARY_TEST_DESTINATION = to("src/test/webapp/unit/common/primary/app");

private final NpmLazyInstaller npmLazyInstaller;

public SvelteModuleFactory(NpmLazyInstaller npmLazyInstaller) {
this.npmLazyInstaller = npmLazyInstaller;
}

public JHipsterModule buildSvelteModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);

Expand Down Expand Up @@ -83,6 +90,9 @@ public JHipsterModule buildSvelteModule(JHipsterModuleProperties properties) {
.addScript(scriptKey("test:coverage"), scriptCommand("vitest run --coverage"))
.addScript(scriptKey("test:watch"), scriptCommand("vitest --"))
.and()
.postActions()
.add(context -> npmLazyInstaller.runInstallIn(context.projectFolder()))
.and()
.optionalReplacements()
.in(path("package.json"))
.add(lineBeforeText(ENGINES_NEEDLE), type(properties.indentation()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.client.vue.core.domain.VueModulesFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Service
public class VueApplicationService {

private final VueModulesFactory factory;

public VueApplicationService() {
factory = new VueModulesFactory();
public VueApplicationService(NpmLazyInstaller npmLazyInstaller) {
factory = new VueModulesFactory(npmLazyInstaller);
}

public JHipsterModule buildVueModule(JHipsterModuleProperties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.module.domain.replacement.MandatoryReplacer;
import tech.jhipster.lite.shared.error.domain.Assert;
Expand Down Expand Up @@ -51,6 +52,12 @@ public class VueModulesFactory {
app.use(pinia);
""";

private final NpmLazyInstaller npmLazyInstaller;

public VueModulesFactory(NpmLazyInstaller npmLazyInstaller) {
this.npmLazyInstaller = npmLazyInstaller;
}

public JHipsterModule buildVueModule(JHipsterModuleProperties properties) {
//@formatter:off
return moduleBuilder(properties)
Expand Down Expand Up @@ -80,6 +87,9 @@ public JHipsterModule buildVueModule(JHipsterModuleProperties properties) {
.addScript(scriptKey("start"), scriptCommand("vite"))
.addScript(scriptKey("watch:tsc"), scriptCommand("npm run build:tsc -- --watch"))
.and()
.postActions()
.add(context -> npmLazyInstaller.runInstallIn(context.projectFolder()))
.and()
.files()
.add(SOURCE.file("tsconfig.build.json"), to("tsconfig.build.json"))
.batch(SOURCE, to("."))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.prettier.domain.PrettierModuleFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Service
public class PrettierApplicationService {

private final PrettierModuleFactory factory;

public PrettierApplicationService() {
factory = new PrettierModuleFactory();
public PrettierApplicationService(NpmLazyInstaller npmLazyInstaller) {
this.factory = new PrettierModuleFactory(npmLazyInstaller);
}

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

public class PrettierModuleFactory {

private static final JHipsterSource SOURCE = from("prettier");
private static final JHipsterDestination DESTINATION = to(".");

private final NpmLazyInstaller npmLazyInstaller;

public PrettierModuleFactory(NpmLazyInstaller npmLazyInstaller) {
this.npmLazyInstaller = npmLazyInstaller;
}

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
//@formatter:off
return moduleBuilder(properties)
Expand All @@ -43,6 +50,9 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.addScript(scriptKey("prettier:check"), scriptCommand("prettier --check ."))
.addScript(scriptKey("prettier:format"), scriptCommand("prettier --write ."))
.and()
.postActions()
.add(context -> npmLazyInstaller.runInstallIn(context.projectFolder()))
.and()
.preCommitActions(stagedFilesFilter("*.{md,json,yml,html,css,scss,java,xml,feature}"), preCommitCommands("['prettier --write']"))
.build();
//@formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.typescript.core.domain.TypescriptModuleFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Service
public class TypescriptApplicationService {

private final TypescriptModuleFactory factory;

public TypescriptApplicationService() {
this.factory = new TypescriptModuleFactory();
public TypescriptApplicationService(NpmLazyInstaller npmLazyInstaller) {
this.factory = new TypescriptModuleFactory(npmLazyInstaller);
}

public JHipsterModule buildModule(JHipsterModuleProperties project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@

import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.npm.NpmLazyInstaller;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.shared.error.domain.Assert;

public class TypescriptModuleFactory {

private static final JHipsterSource SOURCE = from("typescript");
private final NpmLazyInstaller npmLazyInstaller;

public TypescriptModuleFactory(NpmLazyInstaller npmLazyInstaller) {
this.npmLazyInstaller = npmLazyInstaller;
}

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);
Expand Down Expand Up @@ -45,6 +51,9 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.addScript(scriptKey("watch:tsc"), scriptCommand("tsc --noEmit --watch"))
.addScript(scriptKey("watch:test"), scriptCommand("vitest --"))
.and()
.postActions()
.add(context -> npmLazyInstaller.runInstallIn(context.projectFolder()))
.and()
.files()
.batch(SOURCE, to("."))
.addFile("tsconfig.json")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tech.jhipster.lite.module.domain.npm;

import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;

/**
* Run npm install if a previous npm install has already been done.
*/
public interface NpmLazyInstaller {
void runInstallIn(JHipsterProjectFolder folder);
}
Loading

0 comments on commit ee30e04

Please sign in to comment.