Skip to content

Commit

Permalink
feat: improve pre-commit-actions API
Browse files Browse the repository at this point in the history
  • Loading branch information
murdos committed Sep 14, 2024
1 parent 3889fa2 commit b773916
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 7 deletions.
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.startsWith("[") && command.endsWith("]")) {
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.startsWith("'") && command.endsWith("'")) {
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']");
}
}

0 comments on commit b773916

Please sign in to comment.