Skip to content

Commit

Permalink
feat(js): create nestjs-specific files
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Aug 27, 2023
1 parent cb4b485 commit d343fa6
Show file tree
Hide file tree
Showing 29 changed files with 747 additions and 9 deletions.
8 changes: 8 additions & 0 deletions js/hooks/post_gen_project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

set -e

# Include hidden files
# @link https://askubuntu.com/questions/259383/how-can-i-get-mv-or-the-wildcard-to-move-hidden-files
shopt -s dotglob

cp -rf {{ cookiecutter.type }}/* ./

rm -Rf nestjs svelte

git init -b main
git add .
git commit -m "chore: initial commit"
3 changes: 3 additions & 0 deletions js/{{ cookiecutter.project_name }}/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ Thumbs.db
.commit
.devbox
.envrc

coverage
.nyc_output
7 changes: 1 addition & 6 deletions js/{{ cookiecutter.project_name }}/.licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ header:
- "go.*"
- "**/*.{json,md,yml,yaml}"
comment: on-failure
language:
Go:
extensions:
- ".go"
comment_style_id: SlashAsterisk

dependency:
files:
- go.mod # If this is a Go project.
- package.json # If this is a npm project.
4 changes: 4 additions & 0 deletions js/{{ cookiecutter.project_name }}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ repos:
rev: v0.9.2
hooks:
- id: markdownlint-cli2
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.0.2"
hooks:
- id: prettier
4 changes: 4 additions & 0 deletions js/{{ cookiecutter.project_name }}/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.md
*.yml
*.yaml
*.json
8 changes: 8 additions & 0 deletions js/{{ cookiecutter.project_name }}/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}
25 changes: 25 additions & 0 deletions js/{{ cookiecutter.project_name }}/nestjs/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
127 changes: 127 additions & 0 deletions js/{{ cookiecutter.project_name }}/nestjs/.github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
name: Build
on:
push:
branches:
- main
tags:
- "v*.*.*"
pull_request:
branches:
- main
workflow_dispatch:
permissions:
contents: write
packages: write
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: go.mod

- uses: actions/setup-python@v4
with:
python-version: 3.x

- name: Install dependencies
run: go install ./...

- name: go-vet
run: go vet -v ./...

- uses: pre-commit/[email protected]

build:
runs-on: ubuntu-latest
needs:
- test
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Required for goreleaser changelog to work correctly

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: {{ '${{ github.actor }}' }}
password: {{ '${{ secrets.GITHUB_TOKEN }}' }}

- name: Get branch names
id: branch-name
uses: tj-actions/branch-names@v6
with:
strip_tag_prefix: v

- name: Generate Docker tag
id: docker
run: |
if [ "{{ '${{ steps.branch-name.outputs.is_tag }}' }}" = "true" ];
then
# Latest tag
IMG_NAME="ghcr.io/${GITHUB_REPOSITORY,,}:latest"
# Tag name (usually vX.Y.Z)
IMG_NAME="${IMG_NAME},ghcr.io/${GITHUB_REPOSITORY,,}:{{ '${{ steps.branch-name.outputs.tag }}' }}"
echo "image_name=${IMG_NAME}" >> "$GITHUB_OUTPUT"
echo "platforms=linux/amd64,linux/arm64,linux/arm/v7" >> "$GITHUB_OUTPUT"
else
# Use branch naming convention
TAG="branch-{{ '${{ steps.branch-name.outputs.current_branch }}' }}"
# Change "/" for "-"
TAG="${TAG//\//-}"
# Set to lowercase
TAG="${TAG,,}"
echo "image_name=ghcr.io/${GITHUB_REPOSITORY,,}:${TAG}" >> "$GITHUB_OUTPUT"
echo "platforms=linux/amd64" >> "$GITHUB_OUTPUT"
fi
if [ "{{ '${{ steps.branch-name.outputs.is_tag }}' }}" = "true" ];
then
echo "version={{ '${{ steps.branch-name.outputs.tag }}' }}" >> "$GITHUB_OUTPUT"
else
echo "version=development" >> "$GITHUB_OUTPUT"
fi
echo "container_tagged_image=ghcr.io/${GITHUB_REPOSITORY,,}:${GITHUB_SHA}" >> "$GITHUB_OUTPUT"
echo "commit_id=${GITHUB_SHA}" >> "$GITHUB_OUTPUT"
echo "gitRepo=github.com/${GITHUB_REPOSITORY}" >> "$GITHUB_OUTPUT"
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
build-args: |
GIT_COMMIT={{ '${{ steps.docker.outputs.commit_id }}' }}
GIT_REPO={{ '${{ steps.docker.outputs.gitRepo }}' }}
VERSION={{ '${{ steps.docker.outputs.version }}' }}
platforms: {{ '${{ steps.docker.outputs.platforms }}' }}
push: {{ '${{ github.ref == \'refs/heads/main\' }}' }}
tags: {{ '${{ steps.docker.outputs.image_name }},${{ steps.docker.outputs.container_tagged_image }}' }}

- name: Set up Go
if: steps.branch-name.outputs.is_tag == 'true'
uses: actions/setup-go@v3
with:
go-version: '>=1.20.0'

- name: Run GoReleaser
if: steps.branch-name.outputs.is_tag == 'true'
uses: goreleaser/goreleaser-action@v4
with:
version: latest
args: release --clean
env:
GIT_REPO: {{ '${{ steps.docker.outputs.gitRepo }}' }}
GITHUB_TOKEN: {{ '${{ secrets.GITHUB_TOKEN }}' }}
5 changes: 5 additions & 0 deletions js/{{ cookiecutter.project_name }}/nestjs/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 80
}
17 changes: 17 additions & 0 deletions js/{{ cookiecutter.project_name }}/nestjs/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"editor.formatOnSave": true,
"editor.rulers": [
80
],
"yaml.schemas": {
"https://json.schemastore.org/github-workflow.json": [
".github/workflows/*.{yml,yaml}"
]
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
42 changes: 42 additions & 0 deletions js/{{ cookiecutter.project_name }}/nestjs/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM node:lts-alpine AS dev
ARG GIT_COMMIT
ARG GIT_REPO
ARG VERSION
USER node
WORKDIR /home/node/app
ENV GIT_REPO="${GIT_REPO}"
ENV GIT_COMMIT="${GIT_COMMIT}"
ENV VERSION="${VERSION}"
COPY --chown=node:node . .
ENV PORT=3000
EXPOSE 3000
CMD [ "npm", "run", "start:dev" ]

FROM node:lts-alpine AS builder
USER node
USER node
WORKDIR /home/node/app
COPY --from=dev /home/node/app .
RUN npm ci \
&& npm run build

FROM node:lts-alpine
ARG GIT_COMMIT
ARG GIT_REPO
ARG VERSION
WORKDIR /opt/app
ENV GIT_REPO="${GIT_REPO}"
ENV GIT_COMMIT="${GIT_COMMIT}"
ENV VERSION="${VERSION}"
ENV SERVER_PORT=3000
COPY --from=builder /home/node/app/dist dist
COPY --from=builder /home/node/app/node_modules node_modules
COPY --from=builder /home/node/app/package.json package.json
COPY --from=builder /home/node/app/package-lock.json package-lock.json
RUN npm prune --production \
&& npm rebuild \
&& npm dedupe \
&& npm version ${VERSION} --no-git-tag-version --allow-same-version || true
USER node
EXPOSE 3000
CMD [ "npm", "run", "start:prod" ]
32 changes: 32 additions & 0 deletions js/{{ cookiecutter.project_name }}/nestjs/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
services:
app:
build:
context: .
target: dev
environment:
DB_TYPE: mysql
DB_HOST: mysql
DB_USERNAME: app
DB_PASSWORD: password
DB_NAME: app
DB_PORT: "3306"
DB_MIGRATIONS_RUN: "true"
DB_SYNC: "false"
volumes:
- .:/home/node/app
ports:
- 9000:3000
links:
- mysql
restart: on-failure

# Third-party services
mysql:
image: mysql:8
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "true"
MYSQL_USER: app
MYSQL_PASSWORD: password
MYSQL_DATABASE: app
ports:
- 4000:3306
54 changes: 54 additions & 0 deletions js/{{ cookiecutter.project_name }}/nestjs/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Module } from '@nestjs/common';
import { LoggerModule, Params, PinoLogger } from 'nestjs-pino';
import config from './config';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { PinoTypeOrmLogger } from './lib/pinoTypeOrmLogger';
import { LoggerOptions } from 'typeorm';
import { HealthModule } from './health/health.module';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: config,
}),
LoggerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService): Promise<Params> =>
configService.get<Params>('logger'),
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule, LoggerModule],
inject: [ConfigService, PinoLogger],
useFactory: async (
configService: ConfigService,
pinoLogger: PinoLogger,
): Promise<TypeOrmModuleOptions> => {
const db = configService.get<TypeOrmModuleOptions>('db');

/* Replace the default TypeOrm logger with PinoTypeOrmLogger */
let logger: 'debug' | PinoTypeOrmLogger = 'debug';
let logging = false;

if (configService.get<LoggerOptions>('db.logging', false)) {
logger = new PinoTypeOrmLogger(pinoLogger);
logging = true;
}

return {
...db,
logging,
logger,
};
},
}),

// Load the internal modules
HealthModule,
],
controllers: [],
providers: [],
})
export class AppModule {}
Loading

0 comments on commit d343fa6

Please sign in to comment.