readme.md

Node template setup

Working Dir

mkdir fp-ts 
cd ft-tp
pn init

pn stand for pnpm it's a bash alias

Typescript

Add typescript

pn add -D typescript @types/node
pn exec tsc --init \
  --rootDir src --outDir build\
  --esModuleInterop --resolveJsonModule\
  --lib es2018 --module commonjs\
  allowJs true --noImplicitAny true

First run

mkdir src
echo "console.log('hello')" > src/index.ts
pn exec tsc

tree build
build/
├── index.d.ts
├── index.d.ts.map
├── index.js
└── index.js.map

0 directories, 4 files

bat build/index.js

"use strict";
console.log('Hello world!');

Configure sripts

pn add -D ts-node nodemon rimraf

indside nodemon.json

{
  "watch": ["src"],
  "ext": ".ts,.js",
  "ignore": [],
  "exec": "ts-node ./src/index.ts"
}

indside package.json

{
...
  "scripts": {
    "build": "rimraf ./build && tsc",
    "dev": "nodemon",
    "start": "pnpm run build && node build/index.js"
  },
...
}

Run the server pn dev

Eslint

Install and config

pn add -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
pn exec eslint --init
You can also run this command directly using 'npm init @eslint/config'.
✔ How would you like to use ESLint? · style
✔ What type of modules does your project use? · esm
✔ Which framework does your project use? · none
✔ Does your project use TypeScript? · No / Yes
✔ Where does your code run? · browser
✔ How would you like to define a style for your project? · prompt
✔ What format do you want your config file to be in? · JSON
✔ What style of indentation do you use? · 4
✔ What quotes do you use for strings? · single
✔ What line endings do you use? · unix
✔ Do you require semicolons? · No / Yes
The config that you've selected requires the following dependencies:

@typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest
✔ Would you like to install them now? · No / Yes
✔ Which package manager do you want to use? · pnpm
Installing @typescript-eslint/eslint-plugin@latest, @typescript-eslint/parser@latest
...
Successfully created .eslintrc.json file in /home/zu/workspace/fp-ts/project-template

Ignore specific asset

inside .eslintignore

node_modules
build
dist
.git/**/*
.next/**/*
.pijul/**/*

script pn lint

...
  "scripts": {
    ...
    "lint": "eslint . --ext .ts",
    "lint-and-fix": "eslint . --ext .ts --fix"
  }
...

Prettier

Install and config

pn add -D prettier
touch .prettierrc

inside .prettierrc

{
  "semi": false,
  "trailingComma": "none",
  "singleQuote": true,
  "printWidth": 80
}

inside package.json

{
  "scripts": {
    ...
    "prettier-format": "prettier --config .prettierrc 'src/**/*.ts' --write"
  }
}

vscode setup

mkdir .vscode
touch .vscode/settings

inside .vscode/settings

{
  "editor.tabSize": 4,
  "editor.insertSpaces": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnPaste": true,
  "editor.formatOnSave": true,
  "editor.minimap.enabled": false,
  "files.exclude": {
    "node_modules": true,
    "archives": false,
    ".anchor": true,
    ".next": true,
    "build": true,
    "dist": true,
    "pnpm-lock.yaml": true
  }
}

CMD + SHIFT + P select format

Configure with eslint

pn add -D eslint-config-prettier eslint-plugin-prettier
  • eslint-config-prettier: Turns off all ESLint rules that have the potential to interfere with Prettier rules.
  • eslint-plugin-prettier: Turns Prettier rules into ESLint rules.

inside .eslintrc

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "plugins": [
    "@typescript-eslint",
    "prettier"
  ],
  "extends": [
    ...
    "prettier"
  ],
  "rules": {
    ...
    "prettier/prettier": 2 // Means error
  }
}

Husky

Install and auto-config

pn dlx husky-init && pn install 

Configure hook

inside .husky/pre-commit

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

pnpm run format
pnpm run lint

lint-staged

pn add -D lint-staged

inside package.json

"lint-staged": {
  "*.{ts,js}": [
    "pnpm run format",
    "pnpm run lint"
  ]
}

inside _husky/pre-commit

...
pnpm exec lint-staged

Git

git init
git add .
git commit -m "first commit"
git remote add origin https://github.com/user/repo.git
git push -u origin main

Pijul

pijul init
pijul add .
pijul record -m "first commit"
pijul push zurgl@ssh.pijul.com:node-ts-template

References