Skip to content

Commit 83465f3

Browse files
committed
feature: simplify and improve idempotency of pre-commit actions
See #10909
1 parent 223c051 commit 83465f3

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

src/main/java/tech/jhipster/lite/module/domain/JHipsterModuleShortcuts.java

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package tech.jhipster.lite.module.domain;
22

33
import static tech.jhipster.lite.module.domain.JHipsterModule.*;
4-
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notMatchingRegex;
4+
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.always;
55

66
import java.util.regex.Pattern;
77
import tech.jhipster.lite.module.domain.file.JHipsterSource;
@@ -22,11 +22,7 @@ final class JHipsterModuleShortcuts {
2222
private static final JHipsterProjectFilePath SPRING_TEST_LOG_FILE = path("src/test/resources/logback.xml");
2323
private static final TextNeedleBeforeReplacer JHIPSTER_LOGGER_NEEDLE = lineBeforeText("<!-- jhipster-needle-logback-add-log -->");
2424

25-
private static final Pattern MODULE_EXPORT = Pattern.compile("module.exports = \\{");
26-
private static final Pattern DEFAULT_ES_LINT = Pattern.compile("\\s*'\\*': \\[], //default configuration, replace with your own");
27-
28-
private static final ElementReplacer EXISTING_ESLINT_CONFIGURATION = regex(notMatchingRegex(MODULE_EXPORT), MODULE_EXPORT);
29-
private static final ElementReplacer DEFAULT_ES_LINT_CONFIGURATION = regex(notMatchingRegex(DEFAULT_ES_LINT), DEFAULT_ES_LINT);
25+
private static final Pattern DEFAULT_LINTSTAGED_CONFIGURATION_ENTRY = Pattern.compile("\\s*'\\*': \\[\\s*].*");
3026

3127
private final JHipsterModuleBuilder builder;
3228

@@ -104,16 +100,13 @@ public void preCommitActions(StagedFilesFilter stagedFilesFilter, PreCommitComma
104100
Assert.notNull("stagedFilesFilter", stagedFilesFilter);
105101
Assert.notNull("preCommitCommands", preCommitCommands);
106102

107-
String esLintReplacement =
108-
"module.exports = \\{" +
109-
LINE_BREAK +
110-
builder.properties().indentation().times(1) +
111-
"'%s': %s,".formatted(stagedFilesFilter.get(), preCommitCommands.get());
103+
String newLintStagedConfigurationEntry =
104+
"%s'%s': %s,".formatted(builder.properties().indentation().times(1), stagedFilesFilter, preCommitCommands);
112105

113106
builder
114107
.optionalReplacements()
115108
.in(path(".lintstagedrc.cjs"))
116-
.add(DEFAULT_ES_LINT_CONFIGURATION, "")
117-
.add(EXISTING_ESLINT_CONFIGURATION, esLintReplacement);
109+
.add(regex(always(), DEFAULT_LINTSTAGED_CONFIGURATION_ENTRY), "")
110+
.add(lineAfterRegex("module.exports = \\{"), newLintStagedConfigurationEntry);
118111
}
119112
}

src/main/java/tech/jhipster/lite/module/domain/PreCommitCommands.java

+5
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ private static String withQuotes(String command) {
3737
public String get() {
3838
return commands;
3939
}
40+
41+
@Override
42+
public String toString() {
43+
return commands;
44+
}
4045
}

src/main/java/tech/jhipster/lite/module/domain/StagedFilesFilter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public record StagedFilesFilter(String filter) {
77
Assert.notBlank("filter", filter);
88
}
99

10-
public String get() {
11-
return filter();
10+
@Override
11+
public String toString() {
12+
return filter;
1213
}
1314
}

0 commit comments

Comments
 (0)