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..30318a993ea 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.npm.NpmVersionSource.COMMON; import tech.jhipster.lite.module.domain.JHipsterModule; import tech.jhipster.lite.module.domain.file.JHipsterDestination; @@ -29,13 +30,17 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) { .put("dasherizedBaseName", properties.projectBaseName().kebabCase()) .put("nodeMajorVersion", npmVersions.nodeVersion().majorVersion()) .put("endOfLine", endOfLine(properties)) + .put("huskyVersion", npmVersions.get("husky", COMMON).version()) + .put("lintStagedVersion", npmVersions.get("lint-staged", COMMON).version()) .and() .files() .batch(SOURCE, DESTINATION) .addTemplate("README.md") .addTemplate("package.json") .addTemplate(".editorconfig") + .addFile(".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() 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..569ae4d8388 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 @@ -22,24 +22,24 @@ 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() + .mandatoryReplacements() + .in(path(DESTINATION + "lintstagedrc.cjs")) + .add(lineAfterRegex("module.exports = \\{"), properties.indentation().times(1) + "'*.{md,json,yml,html,css,scss,java,xml,feature}': ['prettier --write'],") + .and() + .and() .build(); //@formatter:on } 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.cjs b/src/main/resources/generator/init/.lintstagedrc.cjs new file mode 100644 index 00000000000..4103f7f28a6 --- /dev/null +++ b/src/main/resources/generator/init/.lintstagedrc.cjs @@ -0,0 +1,3 @@ +module.exports = { + '*': "echo 'Lint staged'", +}; diff --git a/src/main/resources/generator/init/package.json.mustache b/src/main/resources/generator/init/package.json.mustache index cefc833da15..0c5a0f06997 100644 --- a/src/main/resources/generator/init/package.json.mustache +++ b/src/main/resources/generator/init/package.json.mustache @@ -6,5 +6,12 @@ "license": "UNLICENSED", "engines": { "node": ">={{nodeMajorVersion}}" + }, + "scripts": { + "prepare": "husky" + }, + "devDependencies": { + "husky": "{{huskyVersion}}", + "lint-staged": "{{lintStagedVersion}}" } } 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..fda6ef5759a 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,7 @@ package tech.jhipster.lite.generator.init.domain; import static org.mockito.Mockito.*; +import static tech.jhipster.lite.module.domain.npm.NpmVersionSource.COMMON; import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.*; import org.junit.jupiter.api.Test; @@ -31,6 +32,8 @@ void shouldBuildModule() { String folder = TestFileUtils.tmpDirForTest(); JHipsterModuleProperties properties = properties(folder); when(npmVersions.nodeVersion()).thenReturn(new NpmPackageVersion("16.0.0")); + when(npmVersions.get("husky", COMMON)).thenReturn(new NpmPackageVersion("9.1.5")); + when(npmVersions.get("lint-staged", COMMON)).thenReturn(new NpmPackageVersion("15.2.9")); JHipsterModule module = factory.buildModule(properties); @@ -47,7 +50,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..1c0da89a9ba 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 @@ -26,9 +26,10 @@ void shouldBuildModule() { JHipsterModule module = factory.buildModule(properties); - assertThatModuleWithFiles(module, packageJsonFile()) + assertThatModuleWithFiles(module, packageJsonFile(), lintStagedConfigFile()) .hasFiles(".prettierignore") .hasFile(".lintstagedrc.cjs") + .containing("'*.{md,json,yml,html,css,scss,java,xml,feature}': ['prettier --write']") .containing("*.{md,json,yml,html,css,scss,java,xml,feature}") .and() .hasFile(".prettierrc") @@ -39,16 +40,12 @@ 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 .")); } 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/primary/ModulesSteps.java b/src/test/java/tech/jhipster/lite/module/infrastructure/primary/ModulesSteps.java index 4139e2daf01..7cca1107bdb 100644 --- a/src/test/java/tech/jhipster/lite/module/infrastructure/primary/ModulesSteps.java +++ b/src/test/java/tech/jhipster/lite/module/infrastructure/primary/ModulesSteps.java @@ -94,6 +94,7 @@ public void applyModuleForDefaultProjectWithPackageJson(String moduleSlug, Map