From ac3c6e674307d2c5301307e6530b2da1b94ab01e Mon Sep 17 00:00:00 2001 From: Maciek Sitkowski Date: Sun, 26 Nov 2023 20:20:18 +0100 Subject: [PATCH] fix lint and typecheck commands across monorepo --- apps/docs/package.json | 4 +++- apps/nestjs/jest.config.js | 3 --- apps/nestjs/package.json | 10 ++++++--- apps/nestjs/src/app.controller.spec.ts | 3 ++- apps/nestjs/src/app.controller.ts | 2 +- apps/nestjs/src/cats/cats.controller.spec.ts | 6 ++++-- apps/nestjs/src/cats/cats.controller.ts | 4 +--- apps/nestjs/src/cats/cats.service.spec.ts | 7 ++++--- apps/nestjs/src/cats/cats.service.ts | 2 +- apps/nestjs/src/cats/schemas/cat.schema.ts | 4 ++-- apps/nestjs/src/config/app.config.ts | 2 +- apps/nestjs/src/dogs/dogs.controller.spec.ts | 3 ++- apps/nestjs/src/dogs/dogs.controller.ts | 4 ++-- apps/nestjs/src/dogs/dogs.service.spec.ts | 3 ++- apps/nestjs/src/dogs/dogs.service.ts | 2 +- apps/nestjs/src/main.ts | 9 +++++--- apps/nestjs/test/dogs.e2e-spec.ts | 5 +++-- apps/nestjs/tsconfig.json | 17 ++------------- libs/eslint-config/_base.js | 10 +++++++++ libs/eslint-config/nest.js | 2 +- libs/tsconfig/nest.json | 22 ++++++++++++++++++++ libs/types/index.ts | 4 ++-- libs/types/package.json | 6 ++++++ libs/types/tsconfig.json | 10 +++++++++ libs/ui/package.json | 1 + package.json | 2 +- pnpm-lock.yaml | 6 ++++++ turbo.json | 7 ++++--- 28 files changed, 107 insertions(+), 53 deletions(-) create mode 100644 libs/tsconfig/nest.json create mode 100644 libs/types/tsconfig.json diff --git a/apps/docs/package.json b/apps/docs/package.json index e2b782e..30fdb44 100644 --- a/apps/docs/package.json +++ b/apps/docs/package.json @@ -6,7 +6,9 @@ "build": "storybook build -o build", "start": "serve build", "clean": "rm -rf .turbo && rm -rf node_modules", - "lint": "eslint ." + "lint": "eslint .", + "lint:fix": "pnpm lint --fix", + "typecheck": "tsc --noEmit" }, "dependencies": { "@repo/ui": "workspace:*", diff --git a/apps/nestjs/jest.config.js b/apps/nestjs/jest.config.js index 1df1ad6..ef0a05f 100644 --- a/apps/nestjs/jest.config.js +++ b/apps/nestjs/jest.config.js @@ -11,9 +11,6 @@ module.exports = { testEnvironment: 'node', rootDir: './src', testRegex: '.*\\.spec\\.ts$', - transform: { - '^.+\\.(t|j)s$': 'ts-jest', - }, collectCoverageFrom: ['**/*.(t|j)s'], coverageDirectory: './coverage', } diff --git a/apps/nestjs/package.json b/apps/nestjs/package.json index ae7df76..3349404 100644 --- a/apps/nestjs/package.json +++ b/apps/nestjs/package.json @@ -5,12 +5,15 @@ "build": "nest build", "develop": "nest start --watch", "develop:debug": "nest start --debug --watch", + "lint": "eslint .", + "lint:fix": "pnpm lint --fix", "start": "node build/main", "test": "jest", "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config ./test/jest-e2e.config.js" + "test:e2e": "jest --config ./test/jest-e2e.config.js", + "typecheck": "tsc --noEmit" }, "dependencies": { "@nestjs/common": "^10.0.0", @@ -23,11 +26,12 @@ "rxjs": "^7.8.1" }, "devDependencies": { - "@repo/eslint-config": "workspace:*", - "@repo/types": "workspace:*", "@nestjs/cli": "^10.0.0", "@nestjs/schematics": "^10.0.0", "@nestjs/testing": "^10.0.0", + "@repo/eslint-config": "workspace:*", + "@repo/tsconfig": "workspace:^", + "@repo/types": "workspace:*", "@types/express": "^4.17.17", "@types/jest": "^29.5.2", "@types/node": "^20.3.1", diff --git a/apps/nestjs/src/app.controller.spec.ts b/apps/nestjs/src/app.controller.spec.ts index 9dc5096..ae0ea53 100644 --- a/apps/nestjs/src/app.controller.spec.ts +++ b/apps/nestjs/src/app.controller.spec.ts @@ -1,4 +1,5 @@ -import { Test, TestingModule } from '@nestjs/testing' +import type { TestingModule } from '@nestjs/testing' +import { Test } from '@nestjs/testing' import { AppController } from './app.controller' import { AppService } from './app.service' diff --git a/apps/nestjs/src/app.controller.ts b/apps/nestjs/src/app.controller.ts index 229cda7..94899f7 100644 --- a/apps/nestjs/src/app.controller.ts +++ b/apps/nestjs/src/app.controller.ts @@ -1,6 +1,6 @@ import { Controller, Get } from '@nestjs/common' -import { AppService } from './app.service' +import type { AppService } from './app.service' @Controller() export class AppController { diff --git a/apps/nestjs/src/cats/cats.controller.spec.ts b/apps/nestjs/src/cats/cats.controller.spec.ts index 7d72301..a2ef015 100644 --- a/apps/nestjs/src/cats/cats.controller.spec.ts +++ b/apps/nestjs/src/cats/cats.controller.spec.ts @@ -1,8 +1,9 @@ -import { Test, TestingModule } from '@nestjs/testing' +import type { TestingModule } from '@nestjs/testing' +import { Test } from '@nestjs/testing' import { CatsController } from './cats.controller' import { CatsService } from './cats.service' -import { CreateCatDto } from './dto/create-cat.dto' +import type { CreateCatDto } from './dto/create-cat.dto' describe('Cats Controller', () => { let controller: CatsController @@ -84,6 +85,7 @@ describe('Cats Controller', () => { age: 2, }, ]) + expect(service.findAll).toHaveBeenCalled() }) }) diff --git a/apps/nestjs/src/cats/cats.controller.ts b/apps/nestjs/src/cats/cats.controller.ts index 4e76bad..fefde95 100644 --- a/apps/nestjs/src/cats/cats.controller.ts +++ b/apps/nestjs/src/cats/cats.controller.ts @@ -2,7 +2,7 @@ import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common' import { CatsService } from './cats.service' import { CreateCatDto } from './dto/create-cat.dto' -import { Cat } from './schemas/cat.schema' +import type { Cat } from './schemas/cat.schema' @Controller('cats') export class CatsController { @@ -25,8 +25,6 @@ export class CatsController { @Delete(':id') async delete(@Param('id') id: string) { - console.log('id', id) - return this.catsService.delete(id) } } diff --git a/apps/nestjs/src/cats/cats.service.spec.ts b/apps/nestjs/src/cats/cats.service.spec.ts index 0158fe3..7e13ee9 100644 --- a/apps/nestjs/src/cats/cats.service.spec.ts +++ b/apps/nestjs/src/cats/cats.service.spec.ts @@ -1,9 +1,10 @@ import { getModelToken } from '@nestjs/mongoose' -import { Test, TestingModule } from '@nestjs/testing' -import { Model } from 'mongoose' +import type { TestingModule } from '@nestjs/testing'; +import { Test } from '@nestjs/testing' +import type { Model } from 'mongoose' import { CatsService } from './cats.service' -import { Cat } from './schemas/cat.schema' +import type { Cat } from './schemas/cat.schema' const mockCat = { name: 'Cat #1', diff --git a/apps/nestjs/src/cats/cats.service.ts b/apps/nestjs/src/cats/cats.service.ts index 55591e8..9425b65 100644 --- a/apps/nestjs/src/cats/cats.service.ts +++ b/apps/nestjs/src/cats/cats.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common' import { InjectModel } from '@nestjs/mongoose' import { Model } from 'mongoose' -import { CreateCatDto } from './dto/create-cat.dto' +import type { CreateCatDto } from './dto/create-cat.dto' import { Cat } from './schemas/cat.schema' @Injectable() diff --git a/apps/nestjs/src/cats/schemas/cat.schema.ts b/apps/nestjs/src/cats/schemas/cat.schema.ts index 39d09a7..3b8e4e0 100644 --- a/apps/nestjs/src/cats/schemas/cat.schema.ts +++ b/apps/nestjs/src/cats/schemas/cat.schema.ts @@ -1,6 +1,6 @@ -import type { Cat as CatInterface } from '@repo/types' import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' -import { HydratedDocument } from 'mongoose' +import type { Cat as CatInterface } from '@repo/types' +import type { HydratedDocument } from 'mongoose' export type CatDocument = HydratedDocument diff --git a/apps/nestjs/src/config/app.config.ts b/apps/nestjs/src/config/app.config.ts index 5dcc704..e85f890 100644 --- a/apps/nestjs/src/config/app.config.ts +++ b/apps/nestjs/src/config/app.config.ts @@ -1,6 +1,6 @@ import { registerAs } from '@nestjs/config' -export type AppConfig = { +export interface AppConfig { env: 'development' | 'production' | 'test' port: number } diff --git a/apps/nestjs/src/dogs/dogs.controller.spec.ts b/apps/nestjs/src/dogs/dogs.controller.spec.ts index e20b0e1..17760da 100644 --- a/apps/nestjs/src/dogs/dogs.controller.spec.ts +++ b/apps/nestjs/src/dogs/dogs.controller.spec.ts @@ -1,4 +1,5 @@ -import { Test, TestingModule } from '@nestjs/testing' +import type { TestingModule } from '@nestjs/testing'; +import { Test } from '@nestjs/testing' import { DogsController } from './dogs.controller' import { DogsService } from './dogs.service' diff --git a/apps/nestjs/src/dogs/dogs.controller.ts b/apps/nestjs/src/dogs/dogs.controller.ts index 133a621..510a315 100644 --- a/apps/nestjs/src/dogs/dogs.controller.ts +++ b/apps/nestjs/src/dogs/dogs.controller.ts @@ -1,7 +1,7 @@ -import { Dog } from '@repo/types' import { Controller, Get } from '@nestjs/common' +import type { Dog } from '@repo/types' -import { DogsService } from './dogs.service' +import type { DogsService } from './dogs.service' @Controller('dogs') export class DogsController { diff --git a/apps/nestjs/src/dogs/dogs.service.spec.ts b/apps/nestjs/src/dogs/dogs.service.spec.ts index 4227523..6f3ea2e 100644 --- a/apps/nestjs/src/dogs/dogs.service.spec.ts +++ b/apps/nestjs/src/dogs/dogs.service.spec.ts @@ -1,4 +1,5 @@ -import { Test, TestingModule } from '@nestjs/testing' +import type { TestingModule } from '@nestjs/testing'; +import { Test } from '@nestjs/testing' import { DogsService } from './dogs.service' diff --git a/apps/nestjs/src/dogs/dogs.service.ts b/apps/nestjs/src/dogs/dogs.service.ts index 0c62671..35c675f 100644 --- a/apps/nestjs/src/dogs/dogs.service.ts +++ b/apps/nestjs/src/dogs/dogs.service.ts @@ -1,5 +1,5 @@ -import { Dog } from '@repo/types' import { Injectable } from '@nestjs/common' +import type { Dog } from '@repo/types' @Injectable() export class DogsService { diff --git a/apps/nestjs/src/main.ts b/apps/nestjs/src/main.ts index 9ddf551..2c4f809 100644 --- a/apps/nestjs/src/main.ts +++ b/apps/nestjs/src/main.ts @@ -8,12 +8,15 @@ async function bootstrap() { const logger = new Logger('NestApplication') const app = await NestFactory.create(AppModule) const configService = app.get(ConfigService) - const port = configService.get('app.port') - const host = configService.get('app.host') + const port = configService.get('app.port') + const host = configService.get('app.host') await app.listen(port, host) logger.log(`Nest application running on port ${port}`) } -bootstrap() +bootstrap().catch(error => { + console.error(error) + process.exit(1) +}) diff --git a/apps/nestjs/test/dogs.e2e-spec.ts b/apps/nestjs/test/dogs.e2e-spec.ts index 0ef757a..c94579f 100644 --- a/apps/nestjs/test/dogs.e2e-spec.ts +++ b/apps/nestjs/test/dogs.e2e-spec.ts @@ -1,5 +1,6 @@ -import { INestApplication } from '@nestjs/common' -import { Test, TestingModule } from '@nestjs/testing' +import type { INestApplication } from '@nestjs/common' +import type { TestingModule } from '@nestjs/testing'; +import { Test } from '@nestjs/testing' import request from 'supertest' import { DogsModule } from '~/dogs/dogs.module' diff --git a/apps/nestjs/tsconfig.json b/apps/nestjs/tsconfig.json index 7b3cb3e..3f3e327 100644 --- a/apps/nestjs/tsconfig.json +++ b/apps/nestjs/tsconfig.json @@ -1,21 +1,8 @@ { - "extends": "../../tsconfig.json", + "extends": "@repo/tsconfig/nest.json", "include": ["**/*.ts", "jest.config.js"], + "exclude": ["node_modules", "build"], "compilerOptions": { - "module": "commonjs", - "declaration": true, - "removeComments": true, - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "allowSyntheticDefaultImports": true, - "sourceMap": true, - "outDir": "./build", - "incremental": true, - "strictNullChecks": false, - "noImplicitAny": false, - "strictBindCallApply": false, - "noFallthroughCasesInSwitch": false, - "resolveJsonModule": true, "baseUrl": "./", "paths": { "~/*": ["./src/*"] diff --git a/libs/eslint-config/_base.js b/libs/eslint-config/_base.js index aab52f6..a771162 100644 --- a/libs/eslint-config/_base.js +++ b/libs/eslint-config/_base.js @@ -24,11 +24,21 @@ module.exports = { // Following rules are disabled during setup, but should probably be enabled later: // Errors as unknown, etc. + 'no-console': 'off', '@typescript-eslint/no-explicit-any': 'off', // Some weird type issues when calling `catsApi` in Remix index route '@typescript-eslint/no-unsafe-assignment': 'off', '@typescript-eslint/no-unsafe-member-access': 'off', '@typescript-eslint/no-unsafe-call': 'off', + + '@typescript-eslint/require-await': 'off', + '@typescript-eslint/no-unnecessary-condition': 'off', + '@typescript-eslint/no-extraneous-class': 'off', + '@typescript-eslint/unbound-method': 'off', + '@typescript-eslint/consistent-type-imports': 'off', + '@typescript-eslint/no-floating-promises': 'off', + '@typescript-eslint/no-unsafe-argument': 'off', + '@typescript-eslint/no-unsafe-return': 'off', }, } diff --git a/libs/eslint-config/nest.js b/libs/eslint-config/nest.js index 762d1e3..9947695 100644 --- a/libs/eslint-config/nest.js +++ b/libs/eslint-config/nest.js @@ -31,7 +31,7 @@ module.exports = { }, }, overrides: [ - // Jest + // Jest configs { files: ['jest.config.js', 'jest-e2e.config.js'], rules: { diff --git a/libs/tsconfig/nest.json b/libs/tsconfig/nest.json new file mode 100644 index 0000000..3c4a735 --- /dev/null +++ b/libs/tsconfig/nest.json @@ -0,0 +1,22 @@ +{ + "extends": "./base.json", + "$schema": "https://json.schemastore.org/tsconfig", + "display": "NestJS", + "compilerOptions": { + "module": "commonjs", + "declaration": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, + "sourceMap": true, + "outDir": "./build", + "incremental": true, + "strictNullChecks": false, + "noImplicitAny": false, + "strictBindCallApply": false, + "noFallthroughCasesInSwitch": false, + "resolveJsonModule": true, + "baseUrl": "." + } +} diff --git a/libs/types/index.ts b/libs/types/index.ts index c91a68f..8309e28 100644 --- a/libs/types/index.ts +++ b/libs/types/index.ts @@ -1,11 +1,11 @@ -export type Cat = { +export interface Cat { _id: string name: string age: number breed: string } -export type Dog = { +export interface Dog { _id: string name: string } diff --git a/libs/types/package.json b/libs/types/package.json index fe216fa..ff8a33b 100644 --- a/libs/types/package.json +++ b/libs/types/package.json @@ -2,8 +2,14 @@ "name": "@repo/types", "private": true, "main": "index.ts", + "scripts": { + "lint": "eslint .", + "lint:fix": "pnpm lint --fix", + "typecheck": "tsc --noEmit" + }, "devDependencies": { "@repo/eslint-config": "workspace:*", + "@repo/tsconfig": "workspace:^", "typescript": "^5.3.2" } } diff --git a/libs/types/tsconfig.json b/libs/types/tsconfig.json new file mode 100644 index 0000000..be12885 --- /dev/null +++ b/libs/types/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "@repo/tsconfig/base.json", + "include": ["."], + "exclude": ["dist", "node_modules"], + "compilerOptions": { + "lib": ["ES2015"], + "module": "CommonJS", + "outDir": "./dist" + } +} diff --git a/libs/ui/package.json b/libs/ui/package.json index f68b162..e9e53a6 100644 --- a/libs/ui/package.json +++ b/libs/ui/package.json @@ -20,6 +20,7 @@ "scripts": { "build": "tsup", "lint": "eslint .", + "lint:fix": "pnpm lint --fix", "develop": "tsup --watch", "typecheck": "tsc --noEmit" }, diff --git a/package.json b/package.json index d23a25a..08c1d64 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "test": "pnpm -r test", "test:e2e": "pnpm -r test:e2e", "test:nestjs": "pnpm -F nestjs test", - "typecheck": "tsc -b ." + "typecheck": "turbo run typecheck" }, "devDependencies": { "@flydotio/dockerfile": "^0.4.11", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7a97aaf..62799bd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -145,6 +145,9 @@ importers: '@repo/eslint-config': specifier: workspace:* version: link:../../libs/eslint-config + '@repo/tsconfig': + specifier: workspace:^ + version: link:../../libs/tsconfig '@repo/types': specifier: workspace:* version: link:../../libs/types @@ -286,6 +289,9 @@ importers: '@repo/eslint-config': specifier: workspace:* version: link:../eslint-config + '@repo/tsconfig': + specifier: workspace:^ + version: link:../tsconfig typescript: specifier: ^5.3.2 version: 5.3.2 diff --git a/turbo.json b/turbo.json index 511207f..6ef26a5 100644 --- a/turbo.json +++ b/turbo.json @@ -5,7 +5,7 @@ "dependsOn": ["^build"], // note: output globs are relative to each package's `package.json` // (and not the monorepo root) - "outputs": [".next/**", "!.next/cache/**"] + "outputs": [".next/**", "!.next/cache/**", ".cache"] }, "deploy": { // A package's `deploy` script depends on the `build`, @@ -21,11 +21,12 @@ // either a `.tsx` or `.ts` file has changed in `src` or `test` folders. "inputs": ["src/**/*.tsx", "src/**/*.ts", "test/**/*.ts", "test/**/*.tsx"] }, - // A package's `lint` script has no dependencies and - // can be run whenever. It also has no filesystem outputs. "lint": { "dependsOn": ["^build"] }, + "typecheck": { + "dependsOn": ["^build"] + }, "develop": { "dependsOn": ["build"], "cache": false,