Project Setup


This video is all about initial project set up. It's a necessary evil, though far from enjoyable (in my personal opinion). We are going to zip through this as fast as we can, in order to get it all out of the way and onto the good stuff.

If you haven't already done so, I'd recommend giving the Koa Documentation at least a brief glance before continuing. We won't be covering many of it's cool features, but I wouldn't want you to miss out.

To begin, let's create a new project:

mkdir koa-ts-tutorial
cd koa-ts-tutorial

Then create a bare bones project shell:

npm init --force

npm WARN using --force I sure hope you know what you are doing.

Wrote to /home/chris/Development/koa-ts-tutorial/package.json:

{
  "name": "koa-ts-tutorial",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

You can opt to next > next > next through the npm init process, or just force it like I have.

Whilst I could take you through all the dependencies one by one, I'm going to show you mostly everything up front, and then add in specifics as we go through these next few lessons.

The video covers each of the dependencies in more detail.

Here's my package.json at this point:

{
  "name": "koa-ts-tutorial",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "koa": "^2.7.0",
    "koa-bodyparser": "^4.2.0",
    "koa-logger": "^3.1.0",
    "koa-router": "^7.3.0",
    "koa2-cors": "^2.0.5",
    "redis": "^2.8.0",
    "ts-node": "^3.3.0",
    "typescript": "^3.5.2"
  },
  "devDependencies": {
    "@types/redis": "^2.8.13",
    "@types/jest": "^23.3.14",
    "@types/koa": "^2.0.49",
    "@types/koa-bodyparser": "^4.3.0",
    "@types/koa-logger": "^3.1.1",
    "@types/koa-router": "^7.0.42",
    "@types/koa2-cors": "^2.0.1",
    "@types/node": "^10.14.12",
    "@types/supertest": "^2.0.8",
    "husky": "^1.3.1",
    "jest": "^24.8.0",
    "prettier": "1.15.1",
    "pretty-quick": "^1.11.1",
    "supertest": "^4.0.2",
    "ts-jest": "^23.10.5",
    "ts-node-dev": "^1.0.0-pre.40"
  },
  "scripts": {
    "build": "tsc --build",
    "start": "ts-node-dev src/server.ts",
    "test": "NODE_ENV=test PORT=7788 jest",
    "test:watch": "npm run test -- --watchAll"
  },
  "jest": {
    "verbose": true,
    "collectCoverage": true,
    "modulePathIgnorePatterns": [
      "<rootDir>/node_modules/"
    ],
    "roots": [
      "<rootDir>/__tests__"
    ],
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "testEnvironment": "node",
    "testPathIgnorePatterns": [
      "/node_modules/"
    ],
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  },
  "husky": {
    "hooks": {
      "pre-commit": "pretty-quick --staged"
    }
  }
}

Before going further, let's initialise our project to use git, as this will also tie in with husky to run prettier before any code gets added to our repo. This step is entirely optional, feel free to skip it. I like it though. Saves me a job formatting my code, and ensures everyone using the project adheres to the same standards.

I'll start by creating a .gitignore file, so node_modules and any other unwanted stuff doesn't end up in our git repository:

touch .gitignore

Into which I will add:

.env
/node_modules/*
.idea/*
/backup/*
*.log
build/
tmp/
temp/
coverage/*

Add whatever you like in there, of course.

Then:

➜  koa-ts-tutorial git init
Initialised empty Git repository in /home/chris/Development/koa-ts-tutorial/.git/

Before adding everything to git, I will do the npm install process as this creates the package-lock.json file, which is a file you really need to make sure you commit to git. This is akin to composer.lock in a PHP project.

Finally, let's get everything in git:

➜  koa-ts-tutorial git:(master) ✗ git add .

➜  koa-ts-tutorial git:(master) ✗ git commit -m "init"
[master (root-commit) 05c88ea] init
 3 files changed, 7257 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 package-lock.json
 create mode 100644 package.json

Ok, we're good here. Onwards to the next video.

Episodes