Skip to content

Commit

Permalink
move husky and lintstaged setup from 'prettier' module to 'init'
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienpuissant committed Sep 2, 2024
1 parent 6f77e5a commit 34f989f
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -35,10 +36,17 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.addTemplate("README.md")
.addTemplate("package.json")
.addTemplate(".editorconfig")
.addFile(".lintstagedrc.template.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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) //line en trop apres '*.{md,json,yml,html,css,scss,java,xml,feature}': ['prettier --write'],
.add(NOT_EXISTING_ESLINT_CONFIGURATION, esLintReplacement + LINE_BREAK + "}") //lingne en trop au debut du fichier
.and()
.and()
.build();
//@formatter:on
}
Expand Down
Empty file.
3 changes: 0 additions & 3 deletions src/main/resources/generator/prettier/.lintstagedrc.cjs

This file was deleted.

18 changes: 9 additions & 9 deletions src/test/features/init.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
7 changes: 2 additions & 5 deletions src/test/features/prettier.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ class PrettierModuleFactoryTest {
private PrettierModuleFactory factory;

@Test
void shouldBuildModule() {
void shouldBuildModuleWithLintStaged() {
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")
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/.emptyLintstagedrc.cjs", ".lintstagedrc.cjs");
}

public static ModuleFile withoutPrettierLintStagedConfigFile() {
return file("src/test/resources/projects/init/.withoutPrettierlintstagedrc.cjs", ".lintstagedrc.cjs");
}

public static ModuleFile readmeFile() {
return file("src/test/resources/projects/README.md", "README.md");
}
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};

0 comments on commit 34f989f

Please sign in to comment.