Skip to content

Commit

Permalink
feature: simplify and improve idempotency of pre-commit actions
Browse files Browse the repository at this point in the history
See #10909
  • Loading branch information
murdos committed Sep 19, 2024
1 parent 223c051 commit 18781de
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ public static RegexReplacer regex(String regex) {
return regex(notContainingReplacement(), regex);
}

public static RegexReplacer regex(Pattern pattern) {
return regex(notContainingReplacement(), pattern);
}

public static RegexReplacer regex(ReplacementCondition condition, String regex) {
return new RegexReplacer(condition, regex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tech.jhipster.lite.module.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notMatchingRegex;

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

private static final Pattern MODULE_EXPORT = Pattern.compile("module.exports = \\{");
private static final Pattern DEFAULT_ES_LINT = Pattern.compile("\\s*'\\*': \\[], //default configuration, replace with your own");

private static final ElementReplacer EXISTING_ESLINT_CONFIGURATION = regex(notMatchingRegex(MODULE_EXPORT), MODULE_EXPORT);
private static final ElementReplacer DEFAULT_ES_LINT_CONFIGURATION = regex(notMatchingRegex(DEFAULT_ES_LINT), DEFAULT_ES_LINT);
private static final Pattern DEFAULT_LINTSTAGED_CONFIGURATION_ENTRY = Pattern.compile("\\s*'\\*': \\[\\s+].*");

private final JHipsterModuleBuilder builder;

Expand Down Expand Up @@ -104,16 +99,13 @@ public void preCommitActions(StagedFilesFilter stagedFilesFilter, PreCommitComma
Assert.notNull("stagedFilesFilter", stagedFilesFilter);
Assert.notNull("preCommitCommands", preCommitCommands);

String esLintReplacement =
"module.exports = \\{" +
LINE_BREAK +
builder.properties().indentation().times(1) +
"'%s': %s,".formatted(stagedFilesFilter.get(), preCommitCommands.get());
String newLintStagedConfigurationEntry =
"%s'%s': %s,".formatted(builder.properties().indentation().times(1), stagedFilesFilter, preCommitCommands);

builder
.optionalReplacements()
.in(path(".lintstagedrc.cjs"))
.add(DEFAULT_ES_LINT_CONFIGURATION, "")
.add(EXISTING_ESLINT_CONFIGURATION, esLintReplacement);
.add(regex(DEFAULT_LINTSTAGED_CONFIGURATION_ENTRY), "")
.add(lineAfterRegex("module.exports = \\{"), newLintStagedConfigurationEntry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ private static String withQuotes(String command) {
public String get() {
return commands;
}

@Override
public String toString() {
return commands;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public record StagedFilesFilter(String filter) {
public String get() {
return filter();
}

@Override
public String toString() {
return filter;
}
}

0 comments on commit 18781de

Please sign in to comment.