Skip to content

Instantly share code, notes, and snippets.

@ferdiebergado
Last active October 2, 2022 10:44
Show Gist options
  • Save ferdiebergado/9503af2ede54b9af79f1e903d8b77c69 to your computer and use it in GitHub Desktop.
Save ferdiebergado/9503af2ede54b9af79f1e903d8b77c69 to your computer and use it in GitHub Desktop.
Bash script to scaffold a new express typescript application
#!/bin/bash
# define license details
AUTHOR="Ferdinand Saporas Bergado"
EMAIL=ferdiebergado@gmail.com
LICENSE=MIT
# declare dependencies
DEPS=(express dotenv)
DEV_DEPS=(typescript \@types/node @types/express nodemon ts-node jest ts-jest @types/jest supertest @types/supertest @faker-js/faker)
# initialize a git repository
git init
# create package.json
npm init -y
# configure package.json
npm pkg set author="$AUTHOR"
# add gitignore and LICENSE files
npm install -D gitignore license --ignore-scripts
npx gitignore node
npx license -n "$AUTHOR" -e "$EMAIL" $LICENSE
# install dependencies
npm install ${DEPS[@]}
npm install -D ${DEV_DEPS[@]}
# generate tsconfig
npx tsc --init
sed -i 's/ \/\/ "rootDir": ".\/",/ "rootDir": "src",/' tsconfig.json
sed -i 's/ \/\/ "outDir": ".\/",/ "outDir": "dist",/' tsconfig.json
# generate jest config
# npx ts-jest config:init
cat <<EOF >jest.config.ts
import type { Config } from '@jest/types'
export default async (): Promise<Config.InitialOptions> => {
return {
preset: 'ts-jest',
testEnvironment: 'node',
setupFilesAfterEnv: ['<rootDir>/setupTests.ts'],
verbose: true,
testTimeout: 10000,
}
}
EOF
# add npm scripts
#sed -i 's/ "test": .*/ "dev": "nodemon src\/server.ts",\n "test": "jest",\n "docker:dev": "docker compose up",\n "docker:test": "docker exec -t express_api npm test"/' package.json
npm pkg set scripts.test="jest"
npm pkg set scripts.dev="nodemon src/server.ts"
npm pkg set scripts.docker:dev="docker compose up"
npm pkg set scripts.docker:test="docker exec -it app npx jest"
npm pkg set scripts.lint='eslint \".src/**\"'
npm pkg set scripts.build="tsc"
npm pkg set scripts.start="node dist/server.js"
npm pkg set main="dist/server.js"
# nodemon config
cat <<EOF >nodemon.json
{
"ignore": ["tests/*"],
"delay": 2500
}
EOF
# install eslint with selected plugins
npm install -D eslint eslint-config-prettier prettier @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-plugin-security eslint-plugin-sonarjs --ignore-scripts
cat <<EOF >.eslintrc.json
module.exports = {
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:security/recommended",
"plugin:sonarjs/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"sonarjs"
],
"rules": {
}
}
EOF
# create prettier config
cat <<EOF >.prettierrc.json
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true
}
EOF
# create Dockerfile
cat <<EOF >Dockerfile
FROM node:16
WORKDIR /usr/src/app
COPY package*.json ./
#RUN npm install
RUN npm ci --only=production
COPY --chown=node:node . .
USER node
CMD [ "nodemon", "src/server.ts" ]
EOF
# add dockerignore
cat <<EOF >.dockerignore
node_modules
npm-debug.log
Dockerfile
docker-compose.yml
EOF
# create .env file
cat <<EOF >.env
NODE_ENV=development
HOST=localhost
PORT=3000
KEY=
DB_HOST=mysql
DB_PORT=3306
DB_ROOT_PASSWORD=abc@123
DB_NAME=test1
DB_USER=user1
DB_PASSWORD=user1@test1
EOF
# create src and tests directories
mkdir src tests
# create app stub
cat <<EOF >src/app.ts
import 'dotenv/config'
import express from 'express'
const app = express()
app.set('port', process.env.PORT || 3000)
app.get('/', (req, res) => {
res.json({status: "up"})
})
export default app
EOF
# create server stub
cat <<EOF >src/server.ts
import http from 'http'
import app from './app'
const PORT = app.get('port')
const server = http.createServer(app).listen(PORT)
server.on('listening', () => console.log('Server listening on port %s...', PORT))
EOF
touch README.md
git add .
git commit -m "Initial commit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment