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 83465f3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.always;

import java.util.regex.Pattern;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
Expand All @@ -22,11 +22,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 +100,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(always(), 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 @@ -7,7 +7,8 @@ public record StagedFilesFilter(String filter) {
Assert.notBlank("filter", filter);
}

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

0 comments on commit 83465f3

Please sign in to comment.