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

feat: improve pre-commit-actions API #10838

Merged
merged 1 commit into from
Sep 14, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/tech/jhipster/lite/shared/error/domain/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> void notEmpty(String field, T[] array) {
field(field, array).notEmpty();
}

/**
* Ensure that the value contains no whitespace
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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']");
}
}