|
| 1 | +# Developing LoopBack |
| 2 | + |
| 3 | +This document describes how to develop modules living in loopback-next monorepo. |
| 4 | + |
| 5 | + - [Setting up development environment](#setting-up-development-environment) |
| 6 | + - [Building the project](#building-the-project) |
| 7 | + - [Running tests](#running-tests) |
| 8 | + - [Coding rules](#coding-rules) |
| 9 | + - [API documentation](#api-documentation) |
| 10 | + - [Commit message guidelines](#commit-message-guidelines) |
| 11 | + - [How to test infrastructure changes](#how-to-test-infrastructure-changes) |
| 12 | + |
| 13 | +## Setting up development environment |
| 14 | + |
| 15 | +Before you can start developing LoopBack, you need to install and configure few |
| 16 | +dependencies. |
| 17 | + |
| 18 | + - [git](https://git-scm.com/): Github's [Set Up Git](https://help.github.com/articles/set-up-git/) guide is a good source of information. |
| 19 | + - [Node.js 8.x (LTS)](https://nodejs.org/en/download/) |
| 20 | + |
| 21 | +You may want to configure your IDE or editor to get better support for |
| 22 | +TypeScript too. |
| 23 | + |
| 24 | + - [VisualStudio Code](./VSCODE.md) |
| 25 | + - _Missing your favorite IDE/editor here? Pull requests are welcome!_ |
| 26 | + |
| 27 | +Before getting started, it is recommended to configure `git` so that it knows who you are: |
| 28 | + |
| 29 | +```sh |
| 30 | +$ git config --global user.name "J. Random User" |
| 31 | +$ git config --global user.email "[email protected]" |
| 32 | +``` |
| 33 | + |
| 34 | +Please make sure this local email is also added to your [GitHub email list](https://github.com/settings/emails) so that your commits will be properly associated with your account and you will be promoted to Contributor once your first commit is landed. |
| 35 | + |
| 36 | +## Building the project |
| 37 | + |
| 38 | +Whenever you pull updates from GitHub or switch between feature branches, |
| 39 | +we recommend to do a full reinstall of all npm dependencies: |
| 40 | + |
| 41 | +```sh |
| 42 | +$ npm run clean:lerna && npm run bootstrap |
| 43 | +``` |
| 44 | + |
| 45 | +The next step is to compile all packages from TypeScript to JavaScript: |
| 46 | + |
| 47 | +```sh |
| 48 | +$ npm run build |
| 49 | +``` |
| 50 | + |
| 51 | +Please note that we are automatically running the build from `pretest` |
| 52 | +script, therefore you should not need to run this command as part of your |
| 53 | +[red-green-refactor cycle](http://www.jamesshore.com/Blog/Red-Green-Refactor.html). |
| 54 | + |
| 55 | +## Running tests |
| 56 | + |
| 57 | +This is the only command you should need while developing LoopBack: |
| 58 | + |
| 59 | +```sh |
| 60 | +$ npm test |
| 61 | +``` |
| 62 | + |
| 63 | +It does all you need: |
| 64 | + |
| 65 | + - Compile TypeScript |
| 66 | + - Run all tests |
| 67 | + - Check code formatting using [Prettier](https://prettier.io/) |
| 68 | + - Lint the code using [TSLint](https://palantir.github.io/tslint/) |
| 69 | + |
| 70 | +## Coding rules |
| 71 | + |
| 72 | +- All features and bug fixes must be covered by one or more automated tests. |
| 73 | + |
| 74 | +- All public methods must be documented with typedoc comments (see [API Documentation](#api-documentation) below). |
| 75 | + |
| 76 | +- Code should be formatted using [Prettier](https://prettier.io/), see our [prettierrc](../prettierrc). |
| 77 | + |
| 78 | +- Follow our style guide as documented on loopback.io: [Code style guide](http://loopback.io/doc/en/contrib/style-guide.html). |
| 79 | + |
| 80 | +## API Documentation |
| 81 | + |
| 82 | +We use [strong-docs](https://github.com/strongloop/strong-docs) to generate API documentation for all our packages. This documentation is generated when publishing new releases to npmjs.org and it's picked up by http://apidocs.loopback.io/. |
| 83 | + |
| 84 | +You can preview API docs locally by opening the file `docs/apidocs.html` in your browser. |
| 85 | + |
| 86 | +## Commit message guidelines |
| 87 | + |
| 88 | +A good commit message should describe what changed and why. |
| 89 | + |
| 90 | +Our commit messages are formatted according to [Conventional Commits](https://conventionalcommits.org/), we use [commitlint](https://github.com/marionebl/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. |
| 91 | + |
| 92 | +### Commit Message Format |
| 93 | + |
| 94 | +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**: |
| 95 | + |
| 96 | +``` |
| 97 | +<type>(<scope>): <subject> |
| 98 | +<BLANK LINE> |
| 99 | +<body> |
| 100 | +<BLANK LINE> |
| 101 | +<footer> |
| 102 | +``` |
| 103 | + |
| 104 | +#### type |
| 105 | + |
| 106 | +The **type** must be one of the following: |
| 107 | + |
| 108 | + - **feat**: A new feature |
| 109 | + - **fix**: A bug fix |
| 110 | + - **docs**: Documentation only changes |
| 111 | + - **style**: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc) |
| 112 | + - **refactor**: A code change that neither fixes a bug nor adds a feature |
| 113 | + - **perf**: A code change that improves performance |
| 114 | + - **test**: Adding missing or correcting existing tests |
| 115 | + - **build**: Changes that affect the build system or external dependencies |
| 116 | + - **ci**: Changes to our CI configuration files and scripts |
| 117 | + - **chore**: Changes to the auxiliary tools and libraries such as documentation generation |
| 118 | + - **revert**: Reverts a previous commit |
| 119 | + |
| 120 | +#### scope |
| 121 | + |
| 122 | +The **scope** must be a list of one or more packages contained in this monorepo. Eeach scope name must match a directory name in [packages/](../packages), e.g. `core` or `context`. |
| 123 | + |
| 124 | + |
| 125 | +#### subject |
| 126 | + |
| 127 | +The **subject** contains succinct description of the change: |
| 128 | + |
| 129 | + - use the imperative, present tense: "change" not "changed" nor "changes" |
| 130 | + - don't capitalize first letter |
| 131 | + - no dot (.) at the end |
| 132 | + |
| 133 | +#### body |
| 134 | + |
| 135 | +The **body** provides more details, it should include the motivation for the change and contrast this with previous behavior. |
| 136 | + |
| 137 | +Just as in the subject, use the imperative, present tense: "change" not "changed" nor "changes"a |
| 138 | + |
| 139 | +#### footer (optional) |
| 140 | + |
| 141 | +The **footer** should contain any information about Breaking Changes introduced by this commit. |
| 142 | + |
| 143 | +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. |
| 144 | + |
| 145 | +## How to test infrastructure changes |
| 146 | + |
| 147 | +When making changes to project infrastructure, e.g. modifying `tsc` or `tslint` |
| 148 | +configuration, it's important to verify that all usage scenarios keep working. |
| 149 | + |
| 150 | +### Verify TypeScript setup |
| 151 | + |
| 152 | +1. Open any existing TypeScript file, e.g. `packages/src/index.ts` |
| 153 | + |
| 154 | +2. Add a small bit of code to break TypeScript's type checks, for example: |
| 155 | + |
| 156 | + ```ts |
| 157 | + const foo: number = 'bar'; |
| 158 | + ``` |
| 159 | + |
| 160 | +3. Run `npm test` |
| 161 | + |
| 162 | +4. Verify that the build failed and the compiler error message shows a path |
| 163 | + relative to monorepo root, e.g. `packages/src/index.ts`. |
| 164 | + |
| 165 | + _(2018-02-82: this is does not work now, `tsc` is reporting paths relative to individual package directories.)_ |
| 166 | + |
| 167 | +5. Test integration with supported IDEs: |
| 168 | + - [VS Code](./VSCode.md#how-to-verify-typescript-setup) |
| 169 | + |
| 170 | +### Verify TSLint setup |
| 171 | + |
| 172 | +1. Open any existing TypeScript file, e.g. `packages/src/index.ts` |
| 173 | + |
| 174 | +2. Introduce two kinds linting problems - one that does and another that does not require type information to be detected. For example, you can add the following line at the end of the opened `index.ts`: |
| 175 | + |
| 176 | + ```ts |
| 177 | + const foo: any = 'bar'; |
| 178 | + ``` |
| 179 | + |
| 180 | +3. Run `npm test` |
| 181 | + |
| 182 | +4. Verify that the build failed and both linting problems are reported: |
| 183 | + |
| 184 | + ```text |
| 185 | + ERROR: /Users/(...)/packages/core/src/index.ts[16, 7]: 'foo' is declared but its value is never read. |
| 186 | + ERROR: /Users/(...)/packages/core/src/index.ts[16, 12]: Type declaration of 'any' loses type-safety. Consider replacing it with a more precise type. |
| 187 | + ``` |
| 188 | + |
| 189 | +5. Test integration with supported IDEs: |
| 190 | + |
| 191 | + - [VS Code](./VSCode.md#how-to-verify-tslint-setup) |
| 192 | + |
0 commit comments