emiscode / nodejs-typescript-full-setup

nodejs-typescript-full-setup

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Node.js + TypeScript

  • typescript
  • eslint
  • prettier
  • commitlint
  • jest
  • nodemon
  • husky
git init
touch .gitignore
build
.husky
coverage
yarn.lock
node_modules
yarn-error.log
yarn init -y
yarn add -D typescript @tsconfig/recommended
yarn add -D ts-node @types/node
touch tsconfig.json
{
  "extends": "@tsconfig/recommended/tsconfig.json",
  "compilerOptions": {
    "preserveConstEnums": true,
    "outDir": "build"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "test/**/*"]
}
mkdir src && cd src
touch index.ts
interface User {
  name: string
}

const user: User = {
  name: 'Emilio',
}

console.log(`LOG => ${JSON.stringify(user)}`)

export default user
yarn ts-node index.ts
cd .. && mkdir test && cd test
touch index.test.ts
import user from '../src'

describe('test', () => {
  test('user', () => {
    const name = user.name
    expect(name).toBe('Emilio')
  })
})
yarn add -D jest ts-jest @types/jest
cd .. && touch jest.config.ts
import type { Config } from 'jest'

const config: Config = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  testMatch: ['**/**/*.test.ts'],
  collectCoverageFrom: ['src/**'],
}

export default config
yarn jest --coverage
yarn add -D prettier
touch .pretierrc.yaml
semi: false
tabWidth: 2
singleQuote: true
touch .prettierignore
build
.husky
coverage
yarn.lock
node_modules
yarn-error.log
yarn prettier --write .
yarn add -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-prettier
touch .eslintrc.json
{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "prettier"
  ]
}
touch .eslintignore
build
.husky
coverage
yarn.lock
node_modules
yarn-error.log
yarn eslint src --max-warnings=0
yarn add -D nodemon
touch nodemon.json
{
  "watch": ["src"],
  "ext": ".ts",
  "exec": "yarn ts-node ./src/index.ts"
}
yarn nodemon
yarn add -D husky lint-staged rimraf

add the content below to package.json

"scripts": {
  "start:dev": "yarn nodemon",
  "build": "yarn rimraf ./build && yarn tsc",
  "lint": "yarn eslint src --max-warnings=0",
  "start": "yarn build && node ./build/index.js",
  "test": "yarn jest --watchAll=false --coverage",
  "format": "yarn prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
  "prepare": "yarn husky install && yarn husky add .husky/pre-commit 'yarn lint-staged' && yarn husky add .husky/commit-msg 'yarn commitlint --edit $1'"
  },
"husky": {
  "hooks": {
    "pre-commit": "yarn lint staged",
    "commit-msg": "yarn commitlint --edit $1"
  }
},
"lint-staged": {
  "**/*.ts": [
    "yarn format",
    "yarn lint",
    "yarn test --findRelatedTests --passWithNoTests"
  ]
},
yarn add --dev  @commitlint/cli @commitlint/config-conventional
touch commitlint.config.ts
import type { UserConfig } from '@commitlint/types'

const Configuration: UserConfig = {
  extends: ['@commitlint/config-conventional'],
  formatter: '@commitlint/format',
  helpUrl:
    'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
}

module.exports = Configuration
echo "fox: teste" | yarn commitlint
echo "fix: teste" | yarn commitlint
yarn start:Dev

About

nodejs-typescript-full-setup


Languages

Language:TypeScript 100.0%