From 01f3595b0b39a66cb4f273e25fc3565043374d8b Mon Sep 17 00:00:00 2001 From: Fabien Puissant Date: Sat, 31 Aug 2024 13:30:04 +0200 Subject: [PATCH] move husky and lintstaged setup from 'prettier' module to 'init' Fix #10701 --- .../init/domain/InitModuleFactory.java | 8 +++++ .../domain/PrettierModuleFactory.java | 31 ++++++++++++++++--- .../domain/file/JHipsterModuleFiles.java | 4 +++ .../{prettier => init}/.husky/pre-commit | 0 .../generator/init/.lintstagedrc.template.cjs | 0 .../generator/prettier/.lintstagedrc.cjs | 3 -- src/test/features/init.feature | 18 +++++------ src/test/features/prettier.feature | 7 ++--- .../init/domain/InitModuleFactoryTest.java | 10 ++++-- .../domain/PrettierModuleFactoryTest.java | 22 ++++++++----- .../module/domain/JHipsterModulesFixture.java | 2 +- .../secondary/JHipsterModulesAssertions.java | 8 +++++ .../projects/init/.lintstagedrc.empty.cjs | 0 .../init/.lintstagedrc.withoutPrettier.cjs | 3 ++ 14 files changed, 84 insertions(+), 32 deletions(-) rename src/main/resources/generator/{prettier => init}/.husky/pre-commit (100%) create mode 100644 src/main/resources/generator/init/.lintstagedrc.template.cjs delete mode 100644 src/main/resources/generator/prettier/.lintstagedrc.cjs create mode 100644 src/test/resources/projects/init/.lintstagedrc.empty.cjs create mode 100644 src/test/resources/projects/init/.lintstagedrc.withoutPrettier.cjs diff --git a/src/main/java/tech/jhipster/lite/generator/init/domain/InitModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/init/domain/InitModuleFactory.java index 93a2458aa76..0016b6c820e 100644 --- a/src/main/java/tech/jhipster/lite/generator/init/domain/InitModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/init/domain/InitModuleFactory.java @@ -1,6 +1,7 @@ package tech.jhipster.lite.generator.init.domain; import static tech.jhipster.lite.module.domain.JHipsterModule.*; +import static tech.jhipster.lite.module.domain.packagejson.VersionSource.COMMON; import tech.jhipster.lite.module.domain.JHipsterModule; import tech.jhipster.lite.module.domain.file.JHipsterDestination; @@ -35,10 +36,17 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) { .addTemplate("README.md") .addTemplate("package.json") .addTemplate(".editorconfig") + .addFile(".lintstagedrc.template.cjs", ".lintstagedrc.cjs") .and() + .addExecutable(SOURCE.append(".husky").file("pre-commit"), DESTINATION.append(".husky/pre-commit")) .add(SOURCE.file("gitignore"), to(".gitignore")) .add(SOURCE.file("gitattributes"), to(".gitattributes")) .and() + .packageJson() + .addDevDependency(packageName("husky"), COMMON) + .addDevDependency(packageName("lint-staged"), COMMON) + .addScript(scriptKey("prepare"), scriptCommand("husky")) + .and() .build(); //@formatter:on } diff --git a/src/main/java/tech/jhipster/lite/generator/prettier/domain/PrettierModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/prettier/domain/PrettierModuleFactory.java index 00f74341f12..72c19cfb849 100644 --- a/src/main/java/tech/jhipster/lite/generator/prettier/domain/PrettierModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/prettier/domain/PrettierModuleFactory.java @@ -3,17 +3,37 @@ import static tech.jhipster.lite.module.domain.JHipsterModule.*; import static tech.jhipster.lite.module.domain.packagejson.VersionSource.COMMON; +import java.util.regex.Pattern; import tech.jhipster.lite.module.domain.JHipsterModule; import tech.jhipster.lite.module.domain.file.JHipsterDestination; import tech.jhipster.lite.module.domain.file.JHipsterSource; import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; +import tech.jhipster.lite.module.domain.replacement.ElementReplacer; +import tech.jhipster.lite.module.domain.replacement.EndOfFileReplacer; +import tech.jhipster.lite.module.domain.replacement.RegexReplacer; public class PrettierModuleFactory { private static final JHipsterSource SOURCE = from("prettier"); private static final JHipsterDestination DESTINATION = to("."); + private static final Pattern MODULE_EXPORT = Pattern.compile("module.exports = \\{"); + + private static final ElementReplacer EXISTING_ESLINT_CONFIGURATION = new RegexReplacer( + (contentBeforeReplacement, replacement) -> MODULE_EXPORT.matcher(contentBeforeReplacement).find(), + MODULE_EXPORT + ); + + private static final ElementReplacer NOT_EXISTING_ESLINT_CONFIGURATION = new EndOfFileReplacer((contentBeforeReplacement, replacement) -> + !MODULE_EXPORT.matcher(contentBeforeReplacement).find() + ); + public JHipsterModule buildModule(JHipsterModuleProperties properties) { + String esLintReplacement = + "module.exports = \\{" + + LINE_BREAK + + properties.indentation().times(1) + + "'*.{md,json,yml,html,css,scss,java,xml,feature}': ['prettier --write'],"; //@formatter:off return moduleBuilder(properties) .context() @@ -22,24 +42,25 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) { .and() .files() .batch(SOURCE, DESTINATION) - .addFile(".lintstagedrc.cjs") .addFile(".prettierignore") .addTemplate(".prettierrc") .and() - .addExecutable(SOURCE.append(".husky").file("pre-commit"), DESTINATION.append(".husky/pre-commit")) .and() .packageJson() .addDevDependency(packageName("@prettier/plugin-xml"), COMMON) - .addDevDependency(packageName("husky"), COMMON) - .addDevDependency(packageName("lint-staged"), COMMON) .addDevDependency(packageName("prettier"), COMMON) .addDevDependency(packageName("prettier-plugin-gherkin"), COMMON) .addDevDependency(packageName("prettier-plugin-java"), COMMON) .addDevDependency(packageName("prettier-plugin-packagejson"), COMMON) - .addScript(scriptKey("prepare"), scriptCommand("husky")) .addScript(scriptKey("prettier:check"), scriptCommand("prettier --check .")) .addScript(scriptKey("prettier:format"), scriptCommand("prettier --write .")) .and() + .optionalReplacements() + .in(path(DESTINATION + "lintstagedrc.cjs")) + .add(EXISTING_ESLINT_CONFIGURATION, esLintReplacement) + .add(NOT_EXISTING_ESLINT_CONFIGURATION, esLintReplacement + LINE_BREAK + "};") + .and() + .and() .build(); //@formatter:on } diff --git a/src/main/java/tech/jhipster/lite/module/domain/file/JHipsterModuleFiles.java b/src/main/java/tech/jhipster/lite/module/domain/file/JHipsterModuleFiles.java index b6bd3b0f4fe..7520f859793 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/file/JHipsterModuleFiles.java +++ b/src/main/java/tech/jhipster/lite/module/domain/file/JHipsterModuleFiles.java @@ -123,6 +123,10 @@ public JHipsterModuleFileBatchBuilder addFile(String file) { return add(source.file(file), destination.append(file)); } + public JHipsterModuleFileBatchBuilder addFile(String sourceFile, String destinationFile) { + return add(source.file(sourceFile), destination.append(destinationFile)); + } + public JHipsterModuleFileBatchBuilder addExecutable(String file) { files.addExecutable(source.file(file), destination.append(file)); diff --git a/src/main/resources/generator/prettier/.husky/pre-commit b/src/main/resources/generator/init/.husky/pre-commit similarity index 100% rename from src/main/resources/generator/prettier/.husky/pre-commit rename to src/main/resources/generator/init/.husky/pre-commit diff --git a/src/main/resources/generator/init/.lintstagedrc.template.cjs b/src/main/resources/generator/init/.lintstagedrc.template.cjs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/main/resources/generator/prettier/.lintstagedrc.cjs b/src/main/resources/generator/prettier/.lintstagedrc.cjs deleted file mode 100644 index ce7f9a41cf2..00000000000 --- a/src/main/resources/generator/prettier/.lintstagedrc.cjs +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - '*.{md,json,yml,html,css,scss,java,xml,feature}': ['prettier --write'], -}; diff --git a/src/test/features/init.feature b/src/test/features/init.feature index 46f68f5b523..c7ca0776b93 100644 --- a/src/test/features/init.feature +++ b/src/test/features/init.feature @@ -6,14 +6,14 @@ Feature: Init | baseName | jhipster | | endOfLine | lf | Then I should have files in "" - | .gitignore | - | .gitattributes | - | .editorconfig | - | package.json | - | README.md | - And I should not have files in "" + | .gitignore | + | .gitattributes | + | .editorconfig | + | package.json | + | README.md | | .lintstagedrc.cjs | - | .prettierignore | - | .prettierrc | - And I should not have files in ".husky" + And I should not have files in "" + | .prettierignore | + | .prettierrc | + And I should have files in ".husky" | pre-commit | diff --git a/src/test/features/prettier.feature b/src/test/features/prettier.feature index 2ed93014074..8631c4c144b 100644 --- a/src/test/features/prettier.feature +++ b/src/test/features/prettier.feature @@ -6,8 +6,5 @@ Feature: Prettier | baseName | jhipster | | endOfLine | lf | Then I should have files in "" - | .lintstagedrc.cjs | - | .prettierignore | - | .prettierrc | - And I should have files in ".husky" - | pre-commit | + | .prettierignore | + | .prettierrc | diff --git a/src/test/java/tech/jhipster/lite/generator/init/domain/InitModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/init/domain/InitModuleFactoryTest.java index b82556ceb76..fb1a74c4fb3 100644 --- a/src/test/java/tech/jhipster/lite/generator/init/domain/InitModuleFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/init/domain/InitModuleFactoryTest.java @@ -1,6 +1,6 @@ package tech.jhipster.lite.generator.init.domain; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.when; import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.*; import org.junit.jupiter.api.Test; @@ -47,7 +47,13 @@ void shouldBuildModule() { .containing("test-project") .containing("Test Project") .containing("\"node\": \">=16\"") - .notContaining("scripts"); + .containing(nodeDependency("husky")) + .containing(nodeDependency("lint-staged")) + .containing(nodeScript("prepare", "husky")) + .and() + .hasFile(".lintstagedrc.cjs") + .and() + .hasExecutableFiles(".husky/pre-commit"); } private JHipsterModuleProperties properties(String folder) { diff --git a/src/test/java/tech/jhipster/lite/generator/prettier/domain/PrettierModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/prettier/domain/PrettierModuleFactoryTest.java index fc66ce4e61b..27ff4f8b1cf 100644 --- a/src/test/java/tech/jhipster/lite/generator/prettier/domain/PrettierModuleFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/prettier/domain/PrettierModuleFactoryTest.java @@ -20,16 +20,16 @@ class PrettierModuleFactoryTest { private PrettierModuleFactory factory; @Test - void shouldBuildModule() { + void shouldBuildModuleWithoutPrettierLintStaged() { String folder = TestFileUtils.tmpDirForTest(); JHipsterModuleProperties properties = properties(folder); JHipsterModule module = factory.buildModule(properties); - assertThatModuleWithFiles(module, packageJsonFile()) + assertThatModuleWithFiles(module, packageJsonFile(), withoutPrettierLintStagedConfigFile()) .hasFiles(".prettierignore") .hasFile(".lintstagedrc.cjs") - .containing("*.{md,json,yml,html,css,scss,java,xml,feature}") + .containing("'*.{md,json,yml,html,css,scss,java,xml,feature}': ['prettier --write']") .and() .hasFile(".prettierrc") .containing("tabWidth: 4") @@ -39,20 +39,28 @@ void shouldBuildModule() { .containing("prettier-plugin-java") .containing("prettier-plugin-packagejson") .and() - .hasExecutableFiles(".husky/pre-commit") .hasFile("package.json") .containing(nodeDependency("@prettier/plugin-xml")) - .containing(nodeDependency("husky")) - .containing(nodeDependency("lint-staged")) .containing(nodeDependency("prettier")) .containing(nodeDependency("prettier-plugin-gherkin")) .containing(nodeDependency("prettier-plugin-java")) .containing(nodeDependency("prettier-plugin-packagejson")) - .containing(nodeScript("prepare", "husky")) .containing(nodeScript("prettier:check", "prettier --check .")) .containing(nodeScript("prettier:format", "prettier --write .")); } + @Test + void shouldBuildModuleWithEmptyLintStaged() { + String folder = TestFileUtils.tmpDirForTest(); + JHipsterModuleProperties properties = properties(folder); + + JHipsterModule module = factory.buildModule(properties); + + assertThatModuleWithFiles(module, packageJsonFile(), emptyLintStagedConfigFile()) + .hasFile(".lintstagedrc.cjs") + .containing("'*.{md,json,yml,html,css,scss,java,xml,feature}': ['prettier --write']"); + } + private JHipsterModuleProperties properties(String folder) { return JHipsterModulesFixture.propertiesBuilder(folder) .projectBaseName("testProject") diff --git a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java index 95f7c1396bb..2bbe71178e4 100644 --- a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java +++ b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java @@ -58,7 +58,7 @@ public static JHipsterModule module() { .and() .files() .add(from("init/gitignore"), to(".gitignore")) - .addExecutable(from("prettier/.husky/pre-commit"), to(".husky/pre-commit")) + .addExecutable(from("init/.husky/pre-commit"), to(".husky/pre-commit")) .batch(from("server/javatool/base/main/error"), to("src/main/java/com/company/myapp/errors")) .addTemplate("Assert.java.mustache") .addTemplate("AssertionException.java.mustache") diff --git a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/JHipsterModulesAssertions.java b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/JHipsterModulesAssertions.java index 67587d5766a..a2ef1ce84a2 100644 --- a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/JHipsterModulesAssertions.java +++ b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/JHipsterModulesAssertions.java @@ -60,6 +60,14 @@ public static ModuleFile lintStagedConfigFile() { return file("src/test/resources/projects/init/.lintstagedrc.cjs", ".lintstagedrc.cjs"); } + public static ModuleFile emptyLintStagedConfigFile() { + return file("src/test/resources/projects/init/.lintstagedrc.empty.cjs", ".lintstagedrc.cjs"); + } + + public static ModuleFile withoutPrettierLintStagedConfigFile() { + return file("src/test/resources/projects/init/.lintstagedrc.withoutPrettier.cjs", ".lintstagedrc.cjs"); + } + public static ModuleFile readmeFile() { return file("src/test/resources/projects/README.md", "README.md"); } diff --git a/src/test/resources/projects/init/.lintstagedrc.empty.cjs b/src/test/resources/projects/init/.lintstagedrc.empty.cjs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/test/resources/projects/init/.lintstagedrc.withoutPrettier.cjs b/src/test/resources/projects/init/.lintstagedrc.withoutPrettier.cjs new file mode 100644 index 00000000000..03227b138ba --- /dev/null +++ b/src/test/resources/projects/init/.lintstagedrc.withoutPrettier.cjs @@ -0,0 +1,3 @@ +module.exports = { + '*.pug': ['eslint --fix', 'prettier --write'], +};