This document specifies structural and code-level standardization steps that should be done across all the Elevate micro-services.
./<service-name>/
├── .circleci
├── Dockerfile
├── .dockerignore
├── .git
├── .gitignore
├── README.md
└── src
├── app.js
├── jsconfig.json
├── node_modules
├── package.json
└── package-lock.json
- Prettier
- Eslint
- Module-Alias
- husky
- lint-staged
- JsConfig
-
Install Prettier & Eslint:
ELEVATE/<service-name>/src$ npm install prettier eslint --save-dev-
Add .prettierrc.json file to root of service directory:
./<service-name>/ ├── .prettierrc.json <---- ├── README.md └── src.prettierrc.json sample:
{ "trailingComma": "es5", "tabWidth": 4, "semi": false, "singleQuote": true, "printWidth": 120, "useTabs": true, "overrides": [ { "files": ["*.yml", "*.yaml"], "options": { "tabWidth": 2 } } ] } -
Add .eslintrc.json file to src directory of service:
./<service-name>/src ├── app.js ├── .eslintrc.json <---- └── validators.eslintrc.json sample:
{ "env": { "browser": false, "commonjs": true, "es2021": true, "node": true }, "extends": "eslint:recommended", "parserOptions": { "ecmaVersion": "latest" }, "rules": { "indent": ["error", "tab", { "SwitchCase": 1 }], "linebreak-style": ["error", "unix"], "quotes": ["error", "single", { "avoidEscape": true }], "semi": ["error", "never"] } }
-
-
Install Module-Alias:
ELEVATE/<service-name>/src$ npm install module-aliasFollow setup instructions at: https://www.npmjs.com/package/module-alias
Sample Module-Alias mapping in package.json:
"_moduleAliases": { "@root": ".", "@configs": "configs", "@constants": "constants", "@controllers": "controllers", "@db": "db", "@generics": "generics", "@health-checks": "health-checks", "@middlewares": "middlewares", "@public": "public", "@routes": "routes", "@services": "services", "@validators": "validators" }A jsconfig.json file is required in the src directory in order for IntelliSense features to work with module aliases:
jsconfig.json sample:
{ "compilerOptions": { "module": "commonjs", "target": "es6", "baseUrl": "./", "moduleResolution": "node", "resolveJsonModule": true, "paths": { "@configs*": ["./configs*"], "@constants*": ["./constants*"], "@controllers*": ["./controllers*"], "@db*": ["./db*"], "@generics*": ["./generics*"], "@health-checks*": ["./health-checks*"], "@middlewares*": ["./middlewares*"], "@public*": ["./public*"], "@routes*": ["./routes*"], "@services*": ["./services*"], "@validators*": ["./validators*"] } }, "exclude": ["node_modules"] } -
Install Husky & Lint-Staged:
ELEVATE/<service-name>/src$ npm install husky lint-staged --save-dev-
Add the following script to scripts config of package.json:
"prepare": "cd .. && husky install src/.husky" -
Run the prepare script to create .husky directory:
ELEVATE/<service-name>/src$ npm run prepare -
Run husky command to add pre-commit file:
ELEVATE/<service-name>/src$ npx husky add .husky/pre-commit "npm test" -
Above two steps should create .husky directory in src:
./<service-name>/src/.husky/ ├── _ │ ├── .gitignore │ └── husky.sh └── pre-commit -
Modify the contents of pre-commit file as shown:
#!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" cd src npx lint-staged npm testNote: Any command specified in the pre-commit hook file will be run before every commit.
-
Add following lint-staged configuration to package.json:
"lint-staged": { "../*": "prettier --ignore-unknown --write", "*": "prettier --ignore-unknown --write" }
Note: If no tests are specified, configure test script to exit with value 0 in package.json (Scripts).
"test": "echo \"Error: no test specified\" && exit 0" -
- Prettier: https://www.npmjs.com/package/prettier
- Eslint: https://www.npmjs.com/package/eslint
- Lint-staged: https://www.npmjs.com/package/lint-staged
- Husky: https://www.npmjs.com/package/husky
- Module-alias: https://www.npmjs.com/package/module-alias
- Prettier Pre-commit: https://prettier.io/docs/en/precommit.html
- Eslint Rules: https://eslint.org/docs/rules/
- Husky Documentation: https://typicode.github.io/husky/#/