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 Aug 31, 2024
1 parent 799ff6f commit 330cbd7
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 29 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.npm.NpmVersionSource.COMMON;

import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/generator/init/.lintstagedrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'*': "echo 'Lint staged'",
};
7 changes: 7 additions & 0 deletions src/main/resources/generator/init/package.json.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@
"license": "UNLICENSED",
"engines": {
"node": ">={{nodeMajorVersion}}"
},
"scripts": {
"prepare": "husky"
},
"devDependencies": {
"husky": "{{huskyVersion}}",
"lint-staged": "{{lintStagedVersion}}"
}
}
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,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;
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 ."));
}
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 @@ -94,6 +94,7 @@ public void applyModuleForDefaultProjectWithPackageJson(String moduleSlug, Map<S
String projectFolder = newTestFolder();

addPackageJsonToProject(projectFolder);
addLintStagedToProject(projectFolder);

post(applyModuleUrl(moduleSlug), buildModuleQuery(projectFolder, parameters));
}
Expand Down Expand Up @@ -232,6 +233,10 @@ private static void addPackageJsonToProject(String folder) {
addFileToProject(folder, "src/test/resources/projects/empty-node/package.json", "package.json");
}

private static void addLintStagedToProject(String folder) {
addFileToProject(folder, "src/test/resources/projects/init/.lintstagedrc.cjs", ".lintstagedrc.cjs");
}

private static void addFileToProject(String folder, String source, String destination) {
Path folderPath = Paths.get(folder);
try {
Expand Down

0 comments on commit 330cbd7

Please sign in to comment.