diff --git a/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java index f1f731efa17..434e040ec1a 100644 --- a/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/client/angular/core/domain/AngularModuleFactory.java @@ -22,7 +22,7 @@ public class AngularModuleFactory { public JHipsterModule buildModule(JHipsterModuleProperties properties) { //@formatter:off return moduleBuilder(properties) - .preCommitActions(stagedFilesFilter("{src/**/,}*.ts"), preCommitCommands("['eslint --fix', 'prettier --write']")) + .preCommitActions(stagedFilesFilter("{src/**/,}*.ts"), preCommitCommands("eslint --fix", "prettier --write")) .gitIgnore() .comment("Angular") .pattern(".angular/") diff --git a/src/main/java/tech/jhipster/lite/generator/client/react/core/domain/ReactCoreModulesFactory.java b/src/main/java/tech/jhipster/lite/generator/client/react/core/domain/ReactCoreModulesFactory.java index 89cba6851f9..39a34ea5091 100644 --- a/src/main/java/tech/jhipster/lite/generator/client/react/core/domain/ReactCoreModulesFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/client/react/core/domain/ReactCoreModulesFactory.java @@ -32,7 +32,7 @@ public class ReactCoreModulesFactory { public JHipsterModule buildModule(JHipsterModuleProperties properties) { //@formatter:off return moduleBuilder(properties) - .preCommitActions(stagedFilesFilter("{src/**/,}*.{ts,tsx}"), preCommitCommands("['eslint --fix', 'prettier --write']")) + .preCommitActions(stagedFilesFilter("{src/**/,}*.{ts,tsx}"), preCommitCommands("eslint --fix", "prettier --write")) .packageJson() .type(MODULE) .addDevDependency(packageName("@testing-library/dom"), REACT) diff --git a/src/main/java/tech/jhipster/lite/generator/client/svelte/core/domain/SvelteModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/client/svelte/core/domain/SvelteModuleFactory.java index 481555f2809..67902e21b29 100644 --- a/src/main/java/tech/jhipster/lite/generator/client/svelte/core/domain/SvelteModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/client/svelte/core/domain/SvelteModuleFactory.java @@ -29,7 +29,7 @@ public JHipsterModule buildSvelteModule(JHipsterModuleProperties properties) { //@formatter:off return moduleBuilder(properties) - .preCommitActions(stagedFilesFilter("{src/**/,}*.ts"), preCommitCommands("['eslint --fix', 'prettier --write']")) + .preCommitActions(stagedFilesFilter("{src/**/,}*.ts"), preCommitCommands("eslint --fix", "prettier --write")) .gitIgnore() .comment("Svelte") .pattern(".svelte-kit/") diff --git a/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java b/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java index 12d1baed97b..93e928c393b 100644 --- a/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java @@ -38,7 +38,7 @@ public class VueModulesFactory { public JHipsterModule buildVueModule(JHipsterModuleProperties properties) { //@formatter:off return moduleBuilder(properties) - .preCommitActions(stagedFilesFilter("{src/**/,}*.{ts,vue}"), preCommitCommands("['eslint --fix', 'prettier --write']")) + .preCommitActions(stagedFilesFilter("{src/**/,}*.{ts,vue}"), preCommitCommands("eslint --fix", "prettier --write")) .documentation(documentationTitle("Vue"), DOCUMENTATION_SOURCE.file("vue.md")) .packageJson() .type(MODULE) diff --git a/src/main/java/tech/jhipster/lite/generator/server/javatool/protobuf/domain/ProtobufModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/javatool/protobuf/domain/ProtobufModuleFactory.java index 33ef589e093..e7c605c6367 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/javatool/protobuf/domain/ProtobufModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/server/javatool/protobuf/domain/ProtobufModuleFactory.java @@ -3,6 +3,7 @@ import static tech.jhipster.lite.module.domain.JHipsterModule.*; import tech.jhipster.lite.module.domain.JHipsterModule; +import tech.jhipster.lite.module.domain.PreCommitCommands; import tech.jhipster.lite.module.domain.file.JHipsterDestination; import tech.jhipster.lite.module.domain.file.JHipsterSource; import tech.jhipster.lite.module.domain.gradleplugin.GradleCommunityPlugin; @@ -110,7 +111,7 @@ public JHipsterModule buildProtobufBackwardsCompatibilityCheckModule(JHipsterMod .pluginManagement(protoBackwardsCompatibilityMavenPluginManagement()) .plugin(protoBackwardsCompatibilityMavenPluginBuilder().build()) .and() - .preCommitActions(stagedFilesFilter("*.proto"), preCommitCommands("() => ['mvn proto-backwards-compatibility:backwards-compatibility-check', 'git add *proto.lock']")) + .preCommitActions(stagedFilesFilter("*.proto"), new PreCommitCommands("() => ['mvn proto-backwards-compatibility:backwards-compatibility-check', 'git add *proto.lock']")) .build(); //@formatter:on } diff --git a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java index 28ae498c6ff..dbf6231e28c 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java +++ b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java @@ -362,8 +362,8 @@ public static StagedFilesFilter stagedFilesFilter(String filesFilter) { return new StagedFilesFilter(filesFilter); } - public static PreCommitCommands preCommitCommands(String commands) { - return new PreCommitCommands(commands); + public static PreCommitCommands preCommitCommands(String... commands) { + return PreCommitCommands.of(commands); } public static GradleCorePluginIdBuilder gradleCorePlugin() { diff --git a/src/main/java/tech/jhipster/lite/module/domain/PreCommitCommands.java b/src/main/java/tech/jhipster/lite/module/domain/PreCommitCommands.java index e51b627f9e5..d19e4bbeb21 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/PreCommitCommands.java +++ b/src/main/java/tech/jhipster/lite/module/domain/PreCommitCommands.java @@ -1,12 +1,39 @@ package tech.jhipster.lite.module.domain; +import static java.util.stream.Collectors.joining; + +import java.util.stream.Stream; import tech.jhipster.lite.shared.error.domain.Assert; +/** + * Pre-commit commands in the format supported by lint-staged. + * Can be a single command, or a list of commands, or a javascript function. + */ public record PreCommitCommands(String commands) { public PreCommitCommands { Assert.notBlank("commands", commands); } + public static PreCommitCommands of(String... commands) { + Assert.notEmpty("commands", commands); + + if (commands.length == 1) { + String command = commands[0]; + if (command.matches("^\\[.*]$")) { + return new PreCommitCommands(command); + } + return new PreCommitCommands(withQuotes(command)); + } + return new PreCommitCommands(Stream.of(commands).map(PreCommitCommands::withQuotes).collect(joining(", ", "[", "]"))); + } + + private static String withQuotes(String command) { + if (command.matches("^'.*'$")) { + return command; + } + return "'%s'".formatted(command); + } + public String get() { return commands; } diff --git a/src/main/java/tech/jhipster/lite/shared/error/domain/Assert.java b/src/main/java/tech/jhipster/lite/shared/error/domain/Assert.java index 306159c57df..08a0a2f6016 100644 --- a/src/main/java/tech/jhipster/lite/shared/error/domain/Assert.java +++ b/src/main/java/tech/jhipster/lite/shared/error/domain/Assert.java @@ -70,6 +70,20 @@ public static void notEmpty(String field, Collection collection) { field(field, collection).notEmpty(); } + /** + * Ensure that the given array is not empty + * + * @param field + * name of the field to check (will be displayed in an exception message) + * @param array + * array to check + * @throws MissingMandatoryValueException + * if the array is null or empty + */ + public static void notEmpty(String field, T[] array) { + field(field, array).notEmpty(); + } + /** * Ensure that the value contains no whitespace * diff --git a/src/test/java/tech/jhipster/lite/module/domain/PreCommitCommandsTest.java b/src/test/java/tech/jhipster/lite/module/domain/PreCommitCommandsTest.java new file mode 100644 index 00000000000..dd3fdc922d2 --- /dev/null +++ b/src/test/java/tech/jhipster/lite/module/domain/PreCommitCommandsTest.java @@ -0,0 +1,30 @@ +package tech.jhipster.lite.module.domain; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import org.junit.jupiter.api.Test; +import tech.jhipster.lite.UnitTest; + +@UnitTest +class PreCommitCommandsTest { + + @Test + void shouldHandleSingleCommand() { + assertThat(PreCommitCommands.of("prettier --write").get()).isEqualTo("'prettier --write'"); + } + + @Test + void shouldHandleSingleCommandAlreadyQuoted() { + assertThat(PreCommitCommands.of("'prettier --write'").get()).isEqualTo("'prettier --write'"); + } + + @Test + void shouldHandleSingleCommandAlreadyArray() { + assertThat(PreCommitCommands.of("['prettier --write']").get()).isEqualTo("['prettier --write']"); + } + + @Test + void shouldHandleMultipleCommands() { + assertThat(PreCommitCommands.of("prettier --write", "eslint --fix").get()).isEqualTo("['prettier --write', 'eslint --fix']"); + } +}