Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert to nx monorepo #36

Merged
merged 16 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .dockerignore

This file was deleted.

5 changes: 0 additions & 5 deletions .env.sample

This file was deleted.

42 changes: 42 additions & 0 deletions .eslintrc.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"root": true,
"ignorePatterns": ["**/*"],
"plugins": ["@nx"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {
"@nx/enforce-module-boundaries": [
"error",
{
"enforceBuildableLibDependency": true,
"allow": [],
"depConstraints": [
{
"sourceTag": "*",
"onlyDependOnLibsWithTags": ["*"]
}
]
}
]
}
},
{
"files": ["*.ts", "*.tsx"],
"extends": ["plugin:@nx/typescript"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"extends": ["plugin:@nx/javascript"],
"rules": {}
},
{
"files": ["*.spec.ts", "*.spec.tsx", "*.spec.js", "*.spec.jsx"],
"env": {
"jest": true
},
"rules": {}
}
]
}
25 changes: 0 additions & 25 deletions .eslintrc.js

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ jobs:
run: yarn install --production=false --only=dev

- name: Lint code
run: yarn lint
run: yarn nx run-many -t lint

- name: Run Unit tests
run: yarn test --colors
run: yarn nx run-many -t test

build-and-push:
name: Build & push to DockerHub
Expand All @@ -48,7 +48,7 @@ jobs:

- name: Build Docker image
run: |
docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/stockdog:v-${{ env.VERSION }} .
yarn nx run backend:docker-build --tag=${{ secrets.DOCKER_HUB_USERNAME }}/stockdog:v-${{ env.VERSION }}

- name: Login to DockerHub
uses: docker/login-action@v3
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/dist
/node_modules
/build
/tmp

# Logs
logs
Expand Down Expand Up @@ -54,3 +55,8 @@ pids

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json



.nx/cache
.nx/workspace-data
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Add files here to ignore them from prettier formatting
/dist
/coverage
/.nx/cache
/.nx/workspace-data
27 changes: 0 additions & 27 deletions Dockerfile

This file was deleted.

33 changes: 33 additions & 0 deletions apps/backend/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"extends": [
"../../.eslintrc.base.json"
],
"ignorePatterns": [
"!**/*"
],
"overrides": [
{
"files": [
"*.ts",
"*.tsx",
"*.js",
"*.jsx"
],
"rules": {}
},
{
"files": [
"*.ts",
"*.tsx"
],
"rules": {}
},
{
"files": [
"*.js",
"*.jsx"
],
"rules": {}
}
]
}
14 changes: 14 additions & 0 deletions apps/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Production stage
FROM node:21-alpine

WORKDIR /app

COPY package.json yarn.lock ./

RUN yarn install --production && yarn cache clean

COPY dist/apps/backend ./

EXPOSE 3000

CMD [ "node", "main.js" ]
2 changes: 2 additions & 0 deletions docker-compose.yml → apps/backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ version: '3.8'
services:
db:
image: postgres:13
env_file:
- .env
environment:
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
Expand Down
15 changes: 15 additions & 0 deletions apps/backend/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Config } from '@jest/types';

const config: Config.InitialOptions = {
displayName: 'backend',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageReporters: ['text', 'html'],
coverageDirectory: '../../coverage/apps/backend',
};

export default config;
5 changes: 5 additions & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "backend",
"version": "0.0.1",
"main": "./src/main.js"
}
44 changes: 44 additions & 0 deletions apps/backend/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "backend",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/backend/src",
"projectType": "application",
"tags": [],
"targets": {
"build": {
"executor": "@nx/webpack:webpack",
"outputs": ["{options.outputPath}"],
"options": {
"webpackConfig": "{projectRoot}/webpack.config.js",
"outputPath": "dist/{projectRoot}"
}
},
"serve": {
"executor": "@nx/js:node",
"options": {
"buildTarget": "{projectName}:build"
}
},
"docker-build": {
"dependsOn": ["build"],
"command": "docker build -f {projectRoot}/Dockerfile -t {projectName}:{args.tag} ."
},
"docker": {
"executor": "nx:run-commands",
"dependsOn": ["build"],
"options": {
"commands": [
"docker build -f {projectRoot}/Dockerfile -t {projectName}:dev .",
"docker run --rm --env-file {projectRoot}/.env --net=host -d {projectName}:dev"
],
"parallel": false
}
},
"docker-compose": {
"command": "docker-compose up -d",
"options": {
"cwd": "{projectRoot}"
}
}
}
}
File renamed without changes.
File renamed without changes.
19 changes: 6 additions & 13 deletions src/app.module.ts → apps/backend/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { AssetManagementModule } from './asset-management/asset-management.module';
import { DataSyncModule } from './data-sync/data-sync.module';
import { ScheduleModule } from '@nestjs/schedule';
import { TypeOrmModule } from '@nestjs/typeorm';
import { typeOrmModuleOptions } from 'database/data-source';
import { AssetManagementModule } from '@stockdog/asset-management';
import { DataSyncModule } from '@stockdog/data-sync';
import { typeOrmOptions } from '../config/typeorm.config';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
imports: [
ScheduleModule.forRoot(),
ConfigModule.forRoot({
isGlobal: true,
envFilePath: ['.env'],
}),
TypeOrmModule.forRootAsync({
useFactory: () => typeOrmModuleOptions,
}),
TypeOrmModule.forRoot(typeOrmOptions),
AssetManagementModule,
DataSyncModule,
],
Expand Down
File renamed without changes.
29 changes: 29 additions & 0 deletions apps/backend/src/config/typeorm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import * as entities from '@stockdog/typeorm';

const contexts = (require as any).context(
'../../../../libs/typeorm/src/lib/migrations/',
true,
/\.ts$/,
);
const migrations = contexts
.keys()
.map((modulePath) => contexts(modulePath))
.reduce((result, migrationModule) => {
return Object.assign(result, migrationModule);
});

const { DB_HOST, DB_USERNAME, DB_PASSWORD, DB_PORT, DB_NAME } = process.env;

export const typeOrmOptions: TypeOrmModuleOptions = {
type: 'postgres' as const,
host: DB_HOST,
port: +DB_PORT,
username: DB_USERNAME,
password: DB_PASSWORD,
database: DB_NAME,
synchronize: false,
entities: Object.values(entities),
migrations: Object.values(migrations),
migrationsRun: true,
};
15 changes: 15 additions & 0 deletions apps/backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';

import { AppModule } from './app/app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = process.env.PORT || 3000;
await app.listen(port);
Logger.log(`🚀 Application is running on: http://localhost:${port}/${globalPrefix}`);
}

bootstrap();
5 changes: 5 additions & 0 deletions apps/backend/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
13 changes: 13 additions & 0 deletions apps/backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
9 changes: 9 additions & 0 deletions apps/backend/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
Loading