Skip to content

Commit

Permalink
Merge pull request #10840 from murdos/vue-depends-on-typescript
Browse files Browse the repository at this point in the history
Vue module now depends on typescript module
  • Loading branch information
murdos authored Sep 15, 2024
2 parents 2837c6d + b52e17c commit 3500a22
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 209 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package tech.jhipster.lite.generator.client.vue.core.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.packagejson.NodeModuleFormat.MODULE;
import static tech.jhipster.lite.module.domain.packagejson.VersionSource.COMMON;
import static tech.jhipster.lite.module.domain.packagejson.VersionSource.VUE;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notContainingReplacement;

import java.util.function.Consumer;
import tech.jhipster.lite.module.domain.Indentation;
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.MandatoryReplacer;
import tech.jhipster.lite.module.domain.replacement.TextReplacer;
import tech.jhipster.lite.shared.error.domain.Assert;

public class VueModulesFactory {
Expand All @@ -17,7 +21,6 @@ public class VueModulesFactory {
private static final JHipsterSource APP_SOURCE = from("client/vue/webapp/app");
private static final JHipsterSource DOCUMENTATION_SOURCE = SOURCE.append("documentation");
private static final JHipsterSource IMAGE_SOURCE = SOURCE.append("webapp/content/images");
private static final JHipsterSource SOURCE_COMMON = from("client/common");

private static final JHipsterDestination MAIN_DESTINATION = to("src/main/webapp/app");
private static final JHipsterDestination TEST_DESTINATION = to("src/test/webapp");
Expand All @@ -42,55 +45,33 @@ public JHipsterModule buildVueModule(JHipsterModuleProperties properties) {
.preCommitActions(stagedFilesFilter("{src/**/,}*.{ts,vue}"), preCommitCommands("eslint --fix", "prettier --write"))
.documentation(documentationTitle("Vue"), DOCUMENTATION_SOURCE.file("vue.md"))
.packageJson()
.type(MODULE)
.removeDevDependency(packageName("@tsconfig/recommended"))
.addDependency(packageName("vue"), VUE)
.addDependency(packageName("axios"), VUE)
.addDependency(packageName("vue-router"), VUE)
.addDevDependency(packageName("@typescript-eslint/parser"), COMMON)
.addDependency(packageName("piqure"), VUE)
.addDevDependency(packageName("@vitejs/plugin-vue"), VUE)
.addDevDependency(packageName("typescript-eslint"), COMMON)
.addDevDependency(packageName("globals"), COMMON)
.addDevDependency(packageName("@vue/test-utils"), VUE)
.addDevDependency(packageName("@vue/tsconfig"), VUE)
.addDevDependency(packageName("@vitest/coverage-istanbul"), COMMON)
.addDevDependency(packageName("eslint"), COMMON)
.addDevDependency(packageName("eslint-config-prettier"), COMMON)
.addDevDependency(packageName("eslint-plugin-vue"), VUE)
.addDevDependency(packageName("jsdom"), COMMON)
.addDevDependency(packageName("typescript"), COMMON)
.addDevDependency(packageName("vite"), COMMON)
.addDevDependency(packageName("vite-tsconfig-paths"), COMMON)
.addDevDependency(packageName("vitest"), COMMON)
.addDevDependency(packageName("vitest-sonar-reporter"), COMMON)
.addDevDependency(packageName("vue-tsc"), VUE)
.addDevDependency(packageName("@types/sinon"), VUE)
.addDevDependency(packageName("sinon"), VUE)
.addDevDependency(packageName("npm-run-all2"), COMMON)
.addScript(scriptKey("build"), scriptCommand("npm-run-all build:*"))
.addScript(scriptKey("build:tsc"), scriptCommand("vue-tsc -p tsconfig.build.json --noEmit"))
.addScript(scriptKey("build:vite"), scriptCommand("vite build --emptyOutDir"))
.addScript(scriptKey("dev"), scriptCommand("npm-run-all --parallel dev:*"))
.addScript(scriptKey("dev:vite"), scriptCommand("vite"))
.addScript(scriptKey("watch"), scriptCommand("npm-run-all --parallel watch:*"))
.addDevDependency(packageName("piqure"), VUE)
.addScript(scriptKey("lint"), scriptCommand("eslint ."))
.addScript(scriptKey("preview"), scriptCommand("vite preview"))
.addScript(scriptKey("start"), scriptCommand("vite"))
.addScript(scriptKey("watch:tsc"), scriptCommand("npm run build:tsc -- --watch"))
.addScript(scriptKey("test"), scriptCommand("npm run watch:test"))
.addScript(scriptKey("watch:test"), scriptCommand("vitest --"))
.addScript(scriptKey("test:coverage"), scriptCommand("vitest run --coverage"))
.and()
.files()
.add(SOURCE.template("eslint.config.js.mustache"), to("eslint.config.js"))
.add(SOURCE.file("tsconfig.json"), to("tsconfig.json"))
.add(SOURCE.file("tsconfig.build.json"), to("tsconfig.build.json"))
.batch(SOURCE, to("."))
.addTemplate("vite.config.ts")
.addTemplate("vitest.config.ts")
.and()
.batch(SOURCE_COMMON, to("."))
.addFile(".npmrc")
.and()
.add(SOURCE.template("webapp/index.html"), to("src/main/webapp/index.html"))
.batch(APP_SOURCE, MAIN_DESTINATION)
Expand All @@ -115,10 +96,84 @@ public JHipsterModule buildVueModule(JHipsterModuleProperties properties) {
.add(APP_SOURCE.template("test/webapp/unit/shared/http/infrastructure/secondary/AxiosStub.ts.mustache"), TEST_DESTINATION.append("unit/shared/http/infrastructure/secondary/AxiosStub.ts"))
.add(APP_SOURCE.template("test/webapp/unit/router/infrastructure/primary/HomeRouter.spec.ts.mustache"), TEST_DESTINATION.append("unit/router/infrastructure/primary/HomeRouter.spec.ts"))
.and()
.apply(patchEslintConfig(properties))
.apply(patchTsConfig(properties))
.apply(patchVitestConfig(properties))
.build();
//@formatter:on
}

private Consumer<JHipsterModuleBuilder> patchEslintConfig(JHipsterModuleProperties properties) {
String vuePluginConfig =
"""
\t...vue.configs['flat/recommended'],
\t{
\t\tfiles: ['**/*.vue'],
\t\tlanguageOptions: {
\t\t\tparserOptions: { parser: '@typescript-eslint/parser' },
\t\t\tglobals: { ...globals.browser },
\t\t},
\t},\
""".replace("\t", properties.indentation().spaces());
//@formatter:off
return moduleBuilder -> moduleBuilder
.mandatoryReplacements()
.in(path("eslint.config.js"))
.add(lineAfterRegex("from 'typescript-eslint'"), "import vue from 'eslint-plugin-vue';")
.add(lineAfterRegex("...typescript.configs.recommended"), vuePluginConfig)
.add(text("files: ['src/*/webapp/**/*.ts']"), "files: ['src/*/webapp/**/*.vue', 'src/*/webapp/**/*.ts']")
.add(eslintTypescriptVueRule("'vue/html-self-closing': 'off',", properties.indentation()))
.add(eslintTypescriptVueRule("'@typescript-eslint/no-explicit-any': 'off',", properties.indentation()))
.add(eslintTypescriptVueRule("'@typescript-eslint/no-empty-object-type': 'off',", properties.indentation()))
.and()
.and();
//@formatter:on
}

private static MandatoryReplacer eslintTypescriptVueRule(String rule, Indentation indentation) {
return new MandatoryReplacer(lineAfterRegex("quotes: \\['error', 'single'"), indentation.times(3) + rule);
}

private Consumer<JHipsterModuleBuilder> patchTsConfig(JHipsterModuleProperties properties) {
//@formatter:off
return moduleBuilder -> moduleBuilder
.mandatoryReplacements()
.in(path("tsconfig.json"))
.add(text("@tsconfig/recommended/tsconfig.json"), "@vue/tsconfig/tsconfig.dom.json")
.add(tsConfigCompilerOption("sourceMap", true, properties.indentation()))
.add(tsConfigCompilerOption("allowJs", true, properties.indentation()))
.add(new TextReplacer(notContainingReplacement(), "\"types\": ["), "\"types\": [\"vite/client\", ")
.and()
.and();
//@formatter:on
}

private static MandatoryReplacer tsConfigCompilerOption(String optionName, boolean optionValue, Indentation indentation) {
String compilerOption = indentation.times(2) + "\"%s\": %s,".formatted(optionName, optionValue);
return new MandatoryReplacer(lineAfterRegex("\"compilerOptions\":"), compilerOption);
}

private Consumer<JHipsterModuleBuilder> patchVitestConfig(JHipsterModuleProperties properties) {
//@formatter:off
return moduleBuilder -> moduleBuilder
.mandatoryReplacements()
.in(path("vitest.config.ts"))
.add(lineAfterRegex("from 'vitest/config';"), "import vue from '@vitejs/plugin-vue';")
.add(new TextReplacer(notContainingReplacement(), "plugins: ["), "plugins: [vue(), ")
.add(text("environment: 'node',"), "environment: 'jsdom',")
.add(vitestCoverageExclusion(properties,"src/main/webapp/**/*.component.ts"))
.add(vitestCoverageExclusion(properties,"src/main/webapp/app/router.ts"))
.add(vitestCoverageExclusion(properties,"src/main/webapp/app/injections.ts"))
.add(vitestCoverageExclusion(properties,"src/main/webapp/app/main.ts"))
.and();
//@formatter:on
}

private static MandatoryReplacer vitestCoverageExclusion(JHipsterModuleProperties properties, String filePattern) {
Indentation indentation = properties.indentation();
return new MandatoryReplacer(lineAfterRegex("configDefaults.coverage.exclude"), indentation.times(4) + "'" + filePattern + "',");
}

public JHipsterModule buildPiniaModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ JHipsterModuleResource vueCoreModule(VueApplicationService vue) {
.slug(VUE_CORE)
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().addIndentation().build())
.apiDoc("Frontend - Vue", "Add Vue+Vite")
.organization(JHipsterModuleOrganization.builder().feature(CLIENT_CORE).addDependency(INIT).addDependency(PRETTIER).build())
.organization(JHipsterModuleOrganization.builder().feature(CLIENT_CORE).addDependency(TYPESCRIPT).addDependency(PRETTIER).build())
.tags("client", "init", "vue")
.factory(vue::buildVueModule);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class TypescriptModuleFactory {

private static final JHipsterSource SOURCE = from("typescript");
private static final JHipsterSource SOURCE_COMMON = from("client/common");

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);
Expand All @@ -27,7 +28,6 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.addDevDependency(packageName("@vitest/coverage-istanbul"), COMMON)
.addDevDependency(packageName("eslint"), COMMON)
.addDevDependency(packageName("eslint-config-prettier"), COMMON)
.addDevDependency(packageName("eslint-plugin-import-x"), COMMON)
.addDevDependency(packageName("globals"), COMMON)
.addDevDependency(packageName("npm-run-all2"), COMMON)
.addDevDependency(packageName("typescript-eslint"), COMMON)
Expand All @@ -42,6 +42,9 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.addScript(scriptKey("watch:test"), scriptCommand("vitest --"))
.and()
.files()
.batch(SOURCE_COMMON, to("."))
.addFile(".npmrc")
.and()
.batch(SOURCE, to("."))
.addFile("tsconfig.json")
.addTemplate("vitest.config.ts")
Expand Down
38 changes: 16 additions & 22 deletions src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package tech.jhipster.lite.module.domain;

import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.*;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.always;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notContainingReplacement;

import java.nio.file.Paths;
import java.util.Collection;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Stream;
Expand All @@ -22,41 +21,29 @@
import tech.jhipster.lite.module.domain.gitignore.JHipsterModuleGitIgnore.JHipsterModuleGitIgnoreBuilder;
import tech.jhipster.lite.module.domain.gradleconfiguration.JHipsterModuleGradleConfigurations;
import tech.jhipster.lite.module.domain.gradleconfiguration.JHipsterModuleGradleConfigurations.JHipsterModuleGradleConfigurationBuilder;
import tech.jhipster.lite.module.domain.gradleplugin.GradleCommunityPlugin;
import tech.jhipster.lite.module.domain.gradleplugin.*;
import tech.jhipster.lite.module.domain.gradleplugin.GradleCommunityPlugin.GradleCommunityPluginIdBuilder;
import tech.jhipster.lite.module.domain.gradleplugin.GradleCommunityProfilePlugin;
import tech.jhipster.lite.module.domain.gradleplugin.GradleCommunityProfilePlugin.GradleCommunityProfilePluginIdBuilder;
import tech.jhipster.lite.module.domain.gradleplugin.GradleCorePlugin;
import tech.jhipster.lite.module.domain.gradleplugin.GradleCorePlugin.GradleCorePluginIdBuilder;
import tech.jhipster.lite.module.domain.gradleplugin.JHipsterModuleGradlePlugins;
import tech.jhipster.lite.module.domain.gradleplugin.JHipsterModuleGradlePlugins.JHipsterModuleGradlePluginBuilder;
import tech.jhipster.lite.module.domain.javabuild.*;
import tech.jhipster.lite.module.domain.javabuild.JHipsterModuleMavenBuildExtensions.JHipsterModuleMavenBuildExtensionsBuilder;
import tech.jhipster.lite.module.domain.javabuild.MavenBuildExtension.MavenBuildExtensionGroupIdBuilder;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileActivation;
import tech.jhipster.lite.module.domain.javabuildprofile.*;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileActivation.BuildProfileActivationBuilder;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId;
import tech.jhipster.lite.module.domain.javabuildprofile.JHipsterModuleJavaBuildProfiles;
import tech.jhipster.lite.module.domain.javabuildprofile.JHipsterModuleJavaBuildProfiles.JHipsterModuleJavaBuildProfilesBuilder;
import tech.jhipster.lite.module.domain.javadependency.DependencyId;
import tech.jhipster.lite.module.domain.javadependency.JHipsterModuleJavaDependencies;
import tech.jhipster.lite.module.domain.javadependency.*;
import tech.jhipster.lite.module.domain.javadependency.JHipsterModuleJavaDependencies.JHipsterModuleJavaDependenciesBuilder;
import tech.jhipster.lite.module.domain.javadependency.JavaDependency;
import tech.jhipster.lite.module.domain.javadependency.JavaDependency.JavaDependencyGroupIdBuilder;
import tech.jhipster.lite.module.domain.javaproperties.*;
import tech.jhipster.lite.module.domain.javaproperties.JHipsterModuleSpringFactories.JHipsterModuleSpringFactoriesBuilder;
import tech.jhipster.lite.module.domain.javaproperties.JHipsterModuleSpringProperties.JHipsterModuleSpringPropertiesBuilder;
import tech.jhipster.lite.module.domain.mavenplugin.JHipsterModuleMavenPlugins;
import tech.jhipster.lite.module.domain.mavenplugin.*;
import tech.jhipster.lite.module.domain.mavenplugin.JHipsterModuleMavenPlugins.JHipsterModuleMavenPluginsBuilder;
import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin;
import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin.MavenPluginGroupIdBuilder;
import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginExecution;
import tech.jhipster.lite.module.domain.mavenplugin.MavenPluginExecution.MavenPluginExecutionGoalsBuilder;
import tech.jhipster.lite.module.domain.packagejson.JHipsterModulePackageJson;
import tech.jhipster.lite.module.domain.packagejson.*;
import tech.jhipster.lite.module.domain.packagejson.JHipsterModulePackageJson.JHipsterModulePackageJsonBuilder;
import tech.jhipster.lite.module.domain.packagejson.PackageName;
import tech.jhipster.lite.module.domain.packagejson.ScriptCommand;
import tech.jhipster.lite.module.domain.packagejson.ScriptKey;
import tech.jhipster.lite.module.domain.postaction.JHipsterModulePostActions;
import tech.jhipster.lite.module.domain.postaction.JHipsterModulePostActions.JHipsterModulePostActionsBuilder;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
Expand Down Expand Up @@ -526,6 +513,13 @@ JHipsterModuleProperties properties() {
return properties;
}

public JHipsterModuleBuilder apply(Consumer<JHipsterModuleBuilder> builderCustomizer) {
Assert.notNull("builderCustomizer", builderCustomizer);
builderCustomizer.accept(this);

return this;
}

public JHipsterModuleBuilder documentation(DocumentationTitle title, JHipsterSource source) {
shortcuts.documentation(title, source);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,18 @@ void springTestLogger(String name, LogLevel level) {
Assert.notBlank("name", name);
Assert.notNull("level", level);

builder.optionalReplacements().in(SPRING_TEST_LOG_FILE).add(JHIPSTER_LOGGER_NEEDLE, logger(name, level));
builder.optionalReplacements().in(SPRING_TEST_LOG_FILE).add(logConfigurationEntry(name, level));
}

void springMainLogger(String name, LogLevel level) {
Assert.notBlank("name", name);
Assert.notNull("level", level);

builder.optionalReplacements().in(SPRING_MAIN_LOG_FILE).add(JHIPSTER_LOGGER_NEEDLE, logger(name, level));
builder.optionalReplacements().in(SPRING_MAIN_LOG_FILE).add(logConfigurationEntry(name, level));
}

private OptionalReplacer logConfigurationEntry(String name, LogLevel level) {
return new OptionalReplacer(JHIPSTER_LOGGER_NEEDLE, logger(name, level));
}

private String logger(String name, LogLevel level) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ private JHipsterModuleFileMandatoryReplacementsFactoryBuilder(
super(replacements, file);
}

public JHipsterModuleFileMandatoryReplacementsFactoryBuilder add(MandatoryReplacer mandatoryReplacer) {
replacements().add(buildReplacer(file(), mandatoryReplacer.replacer(), mandatoryReplacer.updatedValue()));

return this;
}

@Override
protected ContentReplacer buildReplacer(JHipsterProjectFilePath file, ElementReplacer toReplace, String replacement) {
return new MandatoryFileReplacer(file, new MandatoryReplacer(toReplace, replacement));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ private JHipsterModuleFileOptionalReplacementsFactoryBuilder(
super(replacements, file);
}

public JHipsterModuleFileOptionalReplacementsFactoryBuilder add(OptionalReplacer mandatoryReplacer) {
replacements().add(buildReplacer(file(), mandatoryReplacer.replacer(), mandatoryReplacer.updatedValue()));

return this;
}

@Override
protected ContentReplacer buildReplacer(JHipsterProjectFilePath file, ElementReplacer toReplace, String replacement) {
return new OptionalFileReplacer(file, new OptionalReplacer(toReplace, replacement));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public Builder add(ElementReplacer elementToReplace, String replacement) {
return self();
}

protected ReplacementsBuilder replacements() {
return replacements;
}

protected JHipsterProjectFilePath file() {
return file;
}

@SuppressWarnings("unchecked")
private Builder self() {
return (Builder) this;
Expand Down
Loading

0 comments on commit 3500a22

Please sign in to comment.