Skip to content

Commit

Permalink
Merge pull request #10468 from murdos/modernize-typescript-module
Browse files Browse the repository at this point in the history
Modernize typescript module
  • Loading branch information
murdos authored Aug 1, 2024
2 parents afab7ea + 7b27a4f commit 225e0e4
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package tech.jhipster.lite.generator.typescript.core.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.from;
import static tech.jhipster.lite.module.domain.JHipsterModule.moduleBuilder;
import static tech.jhipster.lite.module.domain.JHipsterModule.packageName;
import static tech.jhipster.lite.module.domain.JHipsterModule.scriptCommand;
import static tech.jhipster.lite.module.domain.JHipsterModule.scriptKey;
import static tech.jhipster.lite.module.domain.JHipsterModule.to;
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;
Expand All @@ -24,19 +19,28 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
return moduleBuilder(properties)
.packageJson()
.addDevDependency(packageName("typescript"), COMMON)
.addDevDependency(packageName("@tsconfig/recommended"), COMMON)
.addDevDependency(packageName("@typescript-eslint/eslint-plugin"), COMMON)
.addDevDependency(packageName("@typescript-eslint/parser"), COMMON)
.addDevDependency(packageName("@vitest/coverage-istanbul"), COMMON)
.addDevDependency(packageName("eslint"), COMMON)
.addDevDependency(packageName("eslint-import-resolver-typescript"), COMMON)
.addDevDependency(packageName("eslint-plugin-import"), COMMON)
.addDevDependency(packageName("eslint-plugin-prettier"), COMMON)
.addScript(scriptKey("test"), scriptCommand("echo 'Error: no test specified'"))
.addScript(scriptKey("eslint:ci"), scriptCommand("eslint './**/*.{ts,js}'"))
.addScript(scriptKey("eslint"), scriptCommand("eslint './**/*.{ts,js}' --fix"))
.addDevDependency(packageName("vite-tsconfig-paths"), COMMON)
.addDevDependency(packageName("vitest"), COMMON)
.addDevDependency(packageName("vitest-sonar-reporter"), COMMON)
.addScript(scriptKey("lint"), scriptCommand("eslint --ext .js,.ts,.tsx src/"))
.addScript(scriptKey("test"), scriptCommand("npm run test:watch"))
.addScript(scriptKey("test:coverage"), scriptCommand("vitest run --coverage"))
.addScript(scriptKey("test:watch"), scriptCommand("vitest --"))
.and()
.files()
.add(SOURCE.file(".eslintrc.js"), to(".eslintrc.js"))
.add(SOURCE.file("tsconfig.json"), to("tsconfig.json"))
.batch(SOURCE, to("."))
.addFile("tsconfig.json")
.addTemplate("vitest.config.ts")
.addFile(".eslintrc.cjs")
.and()
.and()
.build();
//@formatter:on
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
package tech.jhipster.lite.generator.typescript.optional.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.from;
import static tech.jhipster.lite.module.domain.JHipsterModule.moduleBuilder;
import static tech.jhipster.lite.module.domain.JHipsterModule.packageName;
import static tech.jhipster.lite.module.domain.JHipsterModule.scriptCommand;
import static tech.jhipster.lite.module.domain.JHipsterModule.scriptKey;
import static tech.jhipster.lite.module.domain.JHipsterModule.to;
import static tech.jhipster.lite.module.domain.packagejson.VersionSource.COMMON;
import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.shared.error.domain.Assert;

public class OptionalTypescriptModuleFactory {

private static final JHipsterSource SOURCE = from("typescript");

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

//@formatter:off
return moduleBuilder(properties)
.packageJson()
.addDevDependency(packageName("jest"), COMMON)
.addDevDependency(packageName("@types/jest"), COMMON)
.addDevDependency(packageName("ts-jest"), COMMON)
.addScript(scriptKey("jest"), scriptCommand("jest src/test/webapp/unit --logHeapUsage --maxWorkers=2 --no-cache"))
.addScript(scriptKey("test"), scriptCommand("npm run jest --"))
.addScript(scriptKey("test:watch"), scriptCommand("jest --watch"))
.addScript(scriptKey("test:watch:all"), scriptCommand("jest --watchAll"))
.and()
.files()
.add(SOURCE.file("jest.config.js"), to("jest.config.js"))
.add(from("typescript/webapp/common/domain/optional/").file("Optional.ts"), to("src/main/webapp/app/common/domain/Optional.ts"))
.add(from("typescript/test/webapp/unit/common/domain/optional/").file("Optional.spec.ts"), to("src/test/webapp/unit/common/domain/Optional.spec.ts"))
.and()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@babel/cli": "7.24.8",
"@playwright/test": "1.45.3",
"@prettier/plugin-xml": "3.4.1",
"@tsconfig/recommended": "1.0.7",
"@types/jest": "29.5.12",
"@types/node": "20.14.13",
"@typescript-eslint/eslint-plugin": "7.17.0",
Expand Down
12 changes: 3 additions & 9 deletions src/main/resources/generator/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
{
"extends": "@tsconfig/recommended/tsconfig.json",
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"outFile": "dist/main.js",
"sourceMap": true,
"types": ["vitest/globals"],
"baseUrl": ".",
"paths": {
"@/*": ["src/main/webapp/app/*"]
}
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
"include": ["src/main/webapp/**/*", "src/test/webapp/unit/**/*"]
}
47 changes: 47 additions & 0 deletions src/main/resources/generator/typescript/vitest.config.ts.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/// <reference types="vitest" />

import tsconfigPaths from 'vite-tsconfig-paths';
import { defineConfig, configDefaults } from 'vitest/config';

export default defineConfig({
plugins: [tsconfigPaths()],
test: {
reporters: ['verbose', 'vitest-sonar-reporter'],
outputFile: {
'vitest-sonar-reporter': '{{projectBuildDirectory}}/test-results/TESTS-results-sonar.xml',
},
globals: true,
logHeapUsage: true,
poolOptions: {
threads: {
minThreads: 1,
maxThreads: 2,
},
},
environment: 'node',
cache: false,
include: ['src/test/webapp/unit/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
coverage: {
all: true,
thresholds: {
perFile: true,
autoUpdate: true,
100: true,
},
include: ['src/main/webapp/**/*.ts?(x)'],
exclude: [
...configDefaults.coverage.exclude as string[],
],
clean: true,
provider: 'istanbul',
reportsDirectory: '{{projectBuildDirectory}}/test-results/',
reporter: ['html', 'json-summary', 'text', 'text-summary', 'lcov', 'clover'],
watermarks: {
statements: [100, 100],
branches: [100, 100],
functions: [100, 100],
lines: [100, 100],
},
},
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ void shouldCreateTypescriptModule() {
assertThatModuleWithFiles(module, packageJsonFile())
.hasFile("package.json")
.containing(nodeDependency("typescript"))
.containing(nodeDependency("@tsconfig/recommended"))
.containing(nodeDependency("@typescript-eslint/eslint-plugin"))
.containing(nodeDependency("@typescript-eslint/parser"))
.containing(nodeDependency("@vitest/coverage-istanbul"))
.containing(nodeDependency("eslint"))
.containing(nodeDependency("eslint-import-resolver-typescript"))
.containing(nodeDependency("eslint-plugin-import"))
.containing(nodeDependency("eslint-plugin-prettier"))
.containing("\"test\": \"echo 'Error: no test specified'\"")
.containing("\"eslint:ci\": \"eslint './**/*.{ts,js}'\"")
.containing("\"eslint\": \"eslint './**/*.{ts,js}' --fix\"")
.containing(nodeDependency("vite-tsconfig-paths"))
.containing(nodeDependency("vitest"))
.containing(nodeDependency("vitest-sonar-reporter"))
.containing(nodeScript("test", "npm run test:watch"))
.containing(nodeScript("test:coverage", "vitest run --coverage"))
.containing(nodeScript("test:watch", "vitest --"))
.containing(nodeScript("lint", "eslint --ext .js,.ts,.tsx src/"))
.and()
.hasPrefixedFiles("", ".eslintrc.js", "tsconfig.json");
.hasPrefixedFiles("", ".eslintrc.cjs", "tsconfig.json");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.jhipster.lite.generator.typescript.domain.optional;

import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.*;
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleWithFiles;

import org.junit.jupiter.api.Test;
import tech.jhipster.lite.TestFileUtils;
Expand All @@ -21,16 +21,7 @@ void shouldCreateOptionalTypescriptModule() {

JHipsterModule module = factory.buildModule(properties);

assertThatModuleWithFiles(module, packageJsonFile())
.hasFile("package.json")
.containing(nodeDependency("jest"))
.containing(nodeDependency("@types/jest"))
.containing(nodeDependency("ts-jest"))
.containing("\"jest\": \"jest src/test/webapp/unit --logHeapUsage --maxWorkers=2 --no-cache\"")
.containing("\"test\": \"npm run jest --\"")
.containing("\"test:watch\": \"jest --watch\"")
.containing("\"test:watch:all\": \"jest --watchAll\"")
.and()
assertThatModuleWithFiles(module)
.hasFile("src/main/webapp/app/common/domain/Optional.ts")
.and()
.hasFile("src/test/webapp/unit/common/domain/Optional.spec.ts");
Expand Down

0 comments on commit 225e0e4

Please sign in to comment.