Webux Lab - Blog
Webux Lab Logo

Webux Lab

By Studio Webux

Search

By Tommy Gingras

Last update 2023-08-23

Typescript

Create a new Typescript project

Bash script to setup a typescript project with eslint and prettier:

#!/bin/bash

npm init -y
npm install \
    typescript \
    @types/node \
    ts-node \
    nodemon \
    rimraf \
    eslint \
    @typescript-eslint/parser \
    @typescript-eslint/eslint-plugin \
    prettier \
    eslint-config-prettier \
    eslint-plugin-prettier \
    --save-dev

npx tsc --init --rootDir src --outDir build \
--esModuleInterop --resolveJsonModule --lib es6 \
--module commonjs --allowJs true --noImplicitAny true

# MAC OS
sed -i '' '/^.*\/\//d' tsconfig.json

mkdir src/
touch src/index.ts

cat <<EOF > nodemon.json
{
  "watch": ["src"],
  "ext": ".ts,.js",
  "ignore": [],
  "exec": "npx ts-node ./src/index.ts"
}
EOF

cat <<EOF > .eslintrc
{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint", "prettier"],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "prettier"
  ],
  "rules": {
    "prettier/prettier": 2
  }
}
EOF

cat <<EOF > .eslintignore
node_modules
dist
EOF

cat <<EOF > .prettierrc
{
  "semi": true,
  "trailingComma": "none",
  "singleQuote": true,
  "printWidth": 80
}
EOF

cat package.json \
    | jq -r '. + {
        "scripts": {
            "start:dev": "npx nodemon",
            "start": "npm run build && node build/index.js",
            "build": "rimraf ./build && tsc",
            "lint": "eslint . --ext .ts",
            "lint-and-fix": "eslint . --ext .ts --fix",
            "prettier-format": "prettier --config .prettierrc \"src/**/*.ts\" --write"
        }
    }' | tee package.json

Sources

Many Thanks 🎉