This document describes how to develop microservices living in loopback4-microservice-catalog monorepo.
- Setting up development environment
- Setup Codebase
- Building the project
- File naming convention
- Develop a new microservice
- How to upgrade to new LB4 dependencies
- Sonar Setup in VS Code
- Commit message guidelines
- Husky setup for commit hooks
We recommend our contributors to use VisualStudio Code with the following extensions installed:
- Prettier - Code Formatter for automatic formatting of source files on save.
- ESLint to highlight and auto-fix linting problems directly in the editor.
- SonarLint for Visual Studio Code
- TypeScript Hero
- Open root folder of this repo in VS Code.
- Install lerna globally
npm i -g lerna
- Run
lerna bootstrap
- Run
npm i
- Create .env files for all the micro service packages.
- Run DB migrations using
lerna run db:migrate
to run the migrations locally. - Build all microservices in one go -
lerna run build
. - Run
lerna run start
to start all the micro services in one go.
Whenever you pull updates from GitHub or switch between feature branches, make sure to updated installed dependencies in all monorepo packages. The following command will install npm dependencies for all packages and create symbolic links for intra-dependencies:
lerna bootstrap
As part of lerna bootstrap
, TypeScript project references are automatically
updated for each package with in the monorepo.
The next step is to compile all packages from TypeScript to JavaScript:
lerna run build
To force a clean build:
lerna clean && lerna run build
Please note that npm run clean
removes dist
, *.tsbuildinfo
, and other
generated files from each package to clean the state for builds.
To build an individual package:
cd <package-dir> // For example, cd `packages/core`.
npm run build
We use two tools to keep our codebase healthy:
- ESLint to statically analyse our source code and detect common problems.
- Prettier to keep our code always formatted the same way, avoid style discussions in code reviews, and save everybody's time an energy.
You can run both linters via the following npm script, just keep in mind that
lerna run test
is already running them for you.
lerna run lint
Many problems (especially formatting) can be automatically fixed by running the
npm script lint:fix
.
lerna run lint:fix
Use the following command to add or update dependency dep
in a package name
:
$ npx lerna add --scope ${name} ${dep}
For example:
$ npx lerna add --scope arc-auth debug
See lerna add for more details.
For consistency, we follow
Angular's file naming convention.
It helps to derive the usage of files by inspecting the names. Besides the
LoopBack 4 codebase, we also follow this naming convention in our generated
artifacts from the CLI tooling: {name}
.{artifact-type}
.ts
Examples are:
src/decorators/authenticate.decorator.ts
src/boot.component.ts
In addition, files under test
folder are categorized according to the type of
tests (unit, acceptance and integration), with the convention
{name}.{test-type}.ts
.
Examples are:
src/__tests__/acceptance/application.acceptance.ts
src/__tests__/integration/user.controller.integration.ts
src/__tests__/unit/application.unit.ts
- Upgrade LB4 CLI version first. Use
npm i -g @loopback/cli@latest
. - Run
npm i
at project root. This step is mandatory if node.js or npm version support is also needed to upgrade. - Upgrade any deps, if needed, at project root. Verify it in the package.json at root. If not needed, skip this step.
- Navigate to core package.
cd packages/core
. - Run
lb4 update
. It will prompt you to confirm the deps upgrade. Verify it carefully and confirm. Let it overwrite package.json. - There may be some errors in previous step if there are any breaking changes coming in. If that's so, then, delete package-lock.json and run
npm i
again. - Upgrade any other package related, like, loopback4 extensions - loopback4-authentication, etc. In order to ensure upgrade works, all the extensions should support the upgraded LB4 version. For example,
npm i loopback4-authentication@latest loopback4-authorization@latest loopback4-soft-delete@latest tslib@latest loopback-connector-postgresql@latest
. - Run
npm audit fix
, if needed. - Run
npm run test
to validate upgrade. - Now, navigate to individual services.
cd ../../services/audit-service
. - Re-execute steps 5-8 again.
- Before doing test, we need to update the
@sourceloop/core
with local symlinked package. Runlerna bootstrap --force-local --scope=@sourceloop/audit-service
. - Now run
npm run test
to validate upgrade. - Repeat steps 10-13 for each service.
After all the steps above done for all services and core package, all your deps are upgraded and tested. You can commit and push them to be released. Make sure to mention any breaking changes if any of the deps upgrade had breaking change.
- Go to Sonar Cloud
- Login with your SF Github ID
- Go to
My Account
from top-right user profile menu. - Move to
Security
tab. - Add a recognizable token name and click on
Generate token
- Install Sonarlint extension in VS Code IDE
- Open Settings in VS Code IDE
- Search for
sonarlint
using search box at the top - Look for
Sonarlint › Connected Mode: Servers
- Click on
Edit in settings.json
. settings.json under system user directory will open. - Add the below configuration
"sonarlint.connectedMode.servers": [
{
"serverId": "sf_sonar", // Connection identifier
"serverUrl": "https://sonarcloud.io/", // SonarQube/SonarCloud URL - https//sonarcloud.io for SonarCloud
"token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"organizationKey": "sourcefuse-cloud"
}
],
Replace value of token with your own user token generated in step 5 from sonar cloud.
Note - Sonarlint requires latest java runtime. Please install if not done already.
Close and reopen VS Code.
A good commit message should describe what changed and why.
Our commit messages are formatted according to Conventional Commits, we use commitlint to verify and enforce this convention. These rules lead to more readable messages that are easy to follow when looking through the project history. But also, we use the git commit messages to generate change logs when publishing new versions.
Each commit message consists of a header, a body and a footer. The header has a special format that includes a type, an optional scope and a subject:
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
The type must be one of the following:
- feat: A new feature
- fix: A bug fix
- docs: Documentation only changes
- style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
- refactor: A code change that neither fixes a bug nor adds a feature
- perf: A code change that improves performance
- test: Adding missing or correcting existing tests
- build: Changes that affect the build system or external dependencies
- ci: Changes to our CI configuration files and scripts
- chore: Changes to the auxiliary tools and libraries such as documentation generation
- revert: Reverts a previous commit
The scope must be a list of one or more packages contained in this monorepo.
Each scope name must match a directory name in
packages/,
e.g. core
.
Note: If multiple packages are affected by a pull request, don't list the scopes as the commit linter currently only supports only one scope being listed at most.
The subject contains succinct description of the change:
- use the imperative, present tense: "change" not "changed" nor "changes"
- don't capitalize first letter
- no dot (.) at the end
The body provides more details, it should include the motivation for the change and contrast this with previous behavior.
Just as in the subject, use the imperative, present tense: "change" not "changed" nor "changes"a
Paragraphs or bullet points are ok (must not exceed 100 characters per line). Typically a hyphen or asterisk is used for the bullet, followed by a single space, with blank lines in between.
Its mandatory to add references to JIRA ticket you are resolving as part of the commit.
The footer should contain any information about Breaking Changes introduced by this commit.
This section must start with the upper case text BREAKING CHANGE
followed by a
colon (:
) and a space (``). A description must be provided, describing what
has changed and how to migrate from older versions.
- run
npm i
at the root of the project - run
npx husky install
ornpm run prepare
at the root of the project
This repository has commitizen support enabled. Commitizen can help you generate your commit messages automatically.
And to use it, simply call git commit
. The tool will help
you generate a commit message that follows the above guidelines.